smath 1.5.1 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/bin.js +45 -35
- package/dist/index.js +238 -80
- package/package.json +1 -1
- package/types/index.d.ts +131 -64
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|

|
|
5
|
-

|
|
6
6
|

|
|
7
7
|

|
|
8
8
|

|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
smath can be installed from the official [npm package repository](https://www.npmjs.com/package/smath). It is highly recommended to install the latest version, which is installed by default with the following command.
|
|
13
13
|
|
|
14
14
|
```shell
|
|
15
|
-
npm i smath@1.
|
|
15
|
+
npm i smath@1.6.1
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
## Bugs and Requests
|
package/dist/bin.js
CHANGED
|
@@ -17,13 +17,6 @@ if (func.includes('help')) {
|
|
|
17
17
|
console.log('Key: <required> [optional]');
|
|
18
18
|
console.log('Arguments:');
|
|
19
19
|
console.log(' help : Show this page');
|
|
20
|
-
console.log(' sum <c0> [c1] ... [cn] : Compute a total of `n` numbers');
|
|
21
|
-
console.log(' prod <c0> [c1] ... [cn] : Compute a product of `n` numbers');
|
|
22
|
-
console.log(' avg <c0> [c1] ... [cn] : Take an average of `n` numbers');
|
|
23
|
-
console.log(' varp <c0> [c1] ... [cn] : Compute the population variance of `n` numbers');
|
|
24
|
-
console.log(' vars <c0> [c1] ... [cn] : Compute the sample variance of `n` numbers');
|
|
25
|
-
console.log(' stdevp <c0> [c1] ... [cn]: Compute the population standard deviation of `n` numbers');
|
|
26
|
-
console.log(' stdevs <c0> [c1] ... [cn]: Compute the sample standard deviation of `n` numbers');
|
|
27
20
|
console.log(' approx <a> <b> [eps] : Check if `a` and `b` are approximately equal');
|
|
28
21
|
console.log(' clamp <n> <min> <max> : Clamp `n` between `min` and `max`');
|
|
29
22
|
console.log(' expand <n> <min> <max> : Expand normalized `n` between `min` and `max`');
|
|
@@ -33,38 +26,19 @@ if (func.includes('help')) {
|
|
|
33
26
|
console.log(' translate <n> <min1> <max1> <min2> <max2>');
|
|
34
27
|
console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`');
|
|
35
28
|
console.log(' factorial <n> : Compute `n!` (factorial)');
|
|
29
|
+
console.log(' factors <n> : List the prime factors of `n`');
|
|
36
30
|
console.log(' error <exp> <act> : Calculate the normaized percent error between `exp` and `act`');
|
|
31
|
+
console.log(' sum <c0> [c1] ... [cn] : Compute a total of `n` numbers');
|
|
32
|
+
console.log(' prod <c0> [c1] ... [cn] : Compute a product of `n` numbers');
|
|
33
|
+
console.log(' avg <c0> [c1] ... [cn] : Take an average of `n` numbers');
|
|
34
|
+
console.log(' median <c0> [c1] ... [cn]: Take the median of `n` numbers');
|
|
35
|
+
console.log(' varp <c0> [c1] ... [cn] : Compute the population variance of `n` numbers');
|
|
36
|
+
console.log(' vars <c0> [c1] ... [cn] : Compute the sample variance of `n` numbers');
|
|
37
|
+
console.log(' stdevp <c0> [c1] ... [cn]: Compute the population standard deviation of `n` numbers');
|
|
38
|
+
console.log(' stdevs <c0> [c1] ... [cn]: Compute the sample standard deviation of `n` numbers');
|
|
37
39
|
process.exit(1);
|
|
38
40
|
}
|
|
39
41
|
switch (func) {
|
|
40
|
-
case ('sum'): {
|
|
41
|
-
console.log(_1.SMath.sum(nums));
|
|
42
|
-
break;
|
|
43
|
-
}
|
|
44
|
-
case ('prod'): {
|
|
45
|
-
console.log(_1.SMath.prod(nums));
|
|
46
|
-
break;
|
|
47
|
-
}
|
|
48
|
-
case ('avg'): {
|
|
49
|
-
console.log(_1.SMath.avg(nums));
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
case ('varp'): {
|
|
53
|
-
console.log(_1.SMath.varp(nums));
|
|
54
|
-
break;
|
|
55
|
-
}
|
|
56
|
-
case ('vars'): {
|
|
57
|
-
console.log(_1.SMath.vars(nums));
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
case ('stdevp'): {
|
|
61
|
-
console.log(_1.SMath.stdevp(nums));
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
case ('stdevs'): {
|
|
65
|
-
console.log(_1.SMath.stdevs(nums));
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
42
|
case ('approx'): {
|
|
69
43
|
console.log(_1.SMath.approx(nums[0], nums[1], (_b = nums[2]) !== null && _b !== void 0 ? _b : 1e-6));
|
|
70
44
|
break;
|
|
@@ -97,10 +71,46 @@ switch (func) {
|
|
|
97
71
|
console.log(_1.SMath.factorial(nums[0]));
|
|
98
72
|
break;
|
|
99
73
|
}
|
|
74
|
+
case ('factors'): {
|
|
75
|
+
console.log(_1.SMath.factors(nums[0]));
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
100
78
|
case ('error'): {
|
|
101
79
|
console.log(_1.SMath.error(nums[0], nums[1]));
|
|
102
80
|
break;
|
|
103
81
|
}
|
|
82
|
+
case ('sum'): {
|
|
83
|
+
console.log(_1.SMath.sum(nums));
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case ('prod'): {
|
|
87
|
+
console.log(_1.SMath.prod(nums));
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
case ('avg'): {
|
|
91
|
+
console.log(_1.SMath.avg(nums));
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case ('median'): {
|
|
95
|
+
console.log(_1.SMath.median(nums));
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
case ('varp'): {
|
|
99
|
+
console.log(_1.SMath.varp(nums));
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
case ('vars'): {
|
|
103
|
+
console.log(_1.SMath.vars(nums));
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case ('stdevp'): {
|
|
107
|
+
console.log(_1.SMath.stdevp(nums));
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case ('stdevs'): {
|
|
111
|
+
console.log(_1.SMath.stdevs(nums));
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
104
114
|
case (''): {
|
|
105
115
|
console.error('Missing argument. Use with "help" for a list of commands.');
|
|
106
116
|
process.exit(1);
|
package/dist/index.js
CHANGED
|
@@ -14,86 +14,6 @@ exports.SMath = void 0;
|
|
|
14
14
|
var SMath = /** @class */ (function () {
|
|
15
15
|
function SMath() {
|
|
16
16
|
}
|
|
17
|
-
/**
|
|
18
|
-
* Add up all the inputs.
|
|
19
|
-
* If none are present, returns 0.
|
|
20
|
-
* @param n An array of numeric inputs
|
|
21
|
-
* @returns The sum total
|
|
22
|
-
* @example
|
|
23
|
-
* ```js
|
|
24
|
-
* const y = SMath.sum([1, 2, 3]); // 6
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
SMath.sum = function (n) {
|
|
28
|
-
return n.reduce(function (a, b) { return a + b; }, 0);
|
|
29
|
-
};
|
|
30
|
-
/**
|
|
31
|
-
* Multiply all the inputs.
|
|
32
|
-
* If none are present, returns 1.
|
|
33
|
-
* @param n An array of numeric inputs
|
|
34
|
-
* @returns The product
|
|
35
|
-
* @example
|
|
36
|
-
* ```js
|
|
37
|
-
* const y = SMath.prod([2, 2, 3, 5]); // 60
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
SMath.prod = function (n) {
|
|
41
|
-
return n.reduce(function (a, b) { return a * b; }, 1);
|
|
42
|
-
};
|
|
43
|
-
/**
|
|
44
|
-
* Compute the average, or mean, of a set of numbers.
|
|
45
|
-
* @param n An array of numeric inputs
|
|
46
|
-
* @returns The average, or mean
|
|
47
|
-
* @example
|
|
48
|
-
* ```js
|
|
49
|
-
* const y = SMath.avg([1, 2, 4, 4]); // 2.75
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
SMath.avg = function (n) {
|
|
53
|
-
return this.sum(n) / n.length;
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* Compute the variance of a **complete population**.
|
|
57
|
-
* @param n An array of numeric inputs
|
|
58
|
-
* @returns The population variance
|
|
59
|
-
* @example
|
|
60
|
-
* ```js
|
|
61
|
-
* const y = SMath.varp([1, 2, 4, 4]); // 1.6875
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
SMath.varp = function (n) {
|
|
65
|
-
var mean = this.avg(n), squares = n.map(function (x) { return Math.pow((x - mean), 2); });
|
|
66
|
-
return this.sum(squares) / n.length;
|
|
67
|
-
};
|
|
68
|
-
/**
|
|
69
|
-
* Compute the variance of a **sample**.
|
|
70
|
-
* @param n An array of numeric inputs
|
|
71
|
-
* @returns The sample variance
|
|
72
|
-
* @example
|
|
73
|
-
* ```js
|
|
74
|
-
* const y = SMath.vars([1, 2, 4, 4]); // 2.25
|
|
75
|
-
* ```
|
|
76
|
-
*/
|
|
77
|
-
SMath.vars = function (n) {
|
|
78
|
-
var mean = this.avg(n), squares = n.map(function (x) { return Math.pow((x - mean), 2); });
|
|
79
|
-
return this.sum(squares) / (n.length - 1);
|
|
80
|
-
};
|
|
81
|
-
/**
|
|
82
|
-
* Compute the standard deviation of a **complete population**.
|
|
83
|
-
* @param n An array of numeric inputs
|
|
84
|
-
* @returns The population standard deviation
|
|
85
|
-
*/
|
|
86
|
-
SMath.stdevp = function (n) {
|
|
87
|
-
return Math.sqrt(this.varp(n));
|
|
88
|
-
};
|
|
89
|
-
/**
|
|
90
|
-
* Compute the standard deviation of a **sample**.
|
|
91
|
-
* @param n An array of numeric inputs
|
|
92
|
-
* @returns The sample standard deviation
|
|
93
|
-
*/
|
|
94
|
-
SMath.stdevs = function (n) {
|
|
95
|
-
return Math.sqrt(this.vars(n));
|
|
96
|
-
};
|
|
97
17
|
/**
|
|
98
18
|
* Check if two numbers are approximately equal with a maximum abolute error.
|
|
99
19
|
* @param a Any number
|
|
@@ -236,6 +156,35 @@ var SMath = /** @class */ (function () {
|
|
|
236
156
|
return n * this.factorial(n - 1);
|
|
237
157
|
}
|
|
238
158
|
};
|
|
159
|
+
/**
|
|
160
|
+
* Factorize `n` into its prime factors.
|
|
161
|
+
* @param n Any positive integer
|
|
162
|
+
* @returns The array of prime factors
|
|
163
|
+
* @example
|
|
164
|
+
* ```js
|
|
165
|
+
* const y = SMath.factors(12); // [ 2, 2, 3 ]
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
SMath.factors = function (n) {
|
|
169
|
+
if (n < 0 || (n | 0) !== n) {
|
|
170
|
+
throw new Error('Input must be a positive integer!');
|
|
171
|
+
}
|
|
172
|
+
if (n <= 3) {
|
|
173
|
+
return [n];
|
|
174
|
+
}
|
|
175
|
+
var f = [];
|
|
176
|
+
var i = 2;
|
|
177
|
+
while (n > 1 && i <= n) {
|
|
178
|
+
if ((n / i) === ((n / i) | 0)) {
|
|
179
|
+
n /= i;
|
|
180
|
+
f.push(i);
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
i++;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return f;
|
|
187
|
+
};
|
|
239
188
|
/**
|
|
240
189
|
* Calculate the relative normalized error or deviation from any
|
|
241
190
|
* value to an accepted value. An error of 0 indicates that the
|
|
@@ -254,6 +203,215 @@ var SMath = /** @class */ (function () {
|
|
|
254
203
|
SMath.error = function (experimental, actual) {
|
|
255
204
|
return (experimental - actual) / actual;
|
|
256
205
|
};
|
|
206
|
+
/**
|
|
207
|
+
* Add up all the inputs.
|
|
208
|
+
* If none are present, returns 0.
|
|
209
|
+
* @param data An array of numeric inputs
|
|
210
|
+
* @returns The sum total
|
|
211
|
+
* @example
|
|
212
|
+
* ```js
|
|
213
|
+
* const y = SMath.sum([1, 2, 3]); // 6
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
SMath.sum = function (data) {
|
|
217
|
+
return data.reduce(function (a, b) { return a + b; }, 0);
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
* Multiply all the inputs.
|
|
221
|
+
* If none are present, returns 1.
|
|
222
|
+
* @param data An array of numeric inputs
|
|
223
|
+
* @returns The product
|
|
224
|
+
* @example
|
|
225
|
+
* ```js
|
|
226
|
+
* const y = SMath.prod([2, 2, 3, 5]); // 60
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
SMath.prod = function (data) {
|
|
230
|
+
return data.reduce(function (a, b) { return a * b; }, 1);
|
|
231
|
+
};
|
|
232
|
+
/**
|
|
233
|
+
* Compute the average, or mean, of a set of numbers.
|
|
234
|
+
* @param data An array of numeric inputs
|
|
235
|
+
* @returns The average, or mean
|
|
236
|
+
* @example
|
|
237
|
+
* ```js
|
|
238
|
+
* const y = SMath.avg([1, 2, 4, 4]); // 2.75
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
241
|
+
SMath.avg = function (data) {
|
|
242
|
+
return this.sum(data) / data.length;
|
|
243
|
+
};
|
|
244
|
+
/**
|
|
245
|
+
* Compute the median of a set of numbers.
|
|
246
|
+
* @param data An array of numeric inputs
|
|
247
|
+
* @returns The median of the dataset
|
|
248
|
+
* @example
|
|
249
|
+
* ```js
|
|
250
|
+
* const y = SMath.median([2, 5, 3, 1]); // 2.5
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
SMath.median = function (data) {
|
|
254
|
+
data.sort();
|
|
255
|
+
if (data.length % 2) {
|
|
256
|
+
return data[(data.length - 1) / 2];
|
|
257
|
+
}
|
|
258
|
+
return this.avg([data[data.length / 2 - 1], data[data.length / 2]]);
|
|
259
|
+
};
|
|
260
|
+
/**
|
|
261
|
+
* Compute the variance of a **complete population**.
|
|
262
|
+
* @param data An array of numeric inputs
|
|
263
|
+
* @returns The population variance
|
|
264
|
+
* @example
|
|
265
|
+
* ```js
|
|
266
|
+
* const y = SMath.varp([1, 2, 4, 4]); // 1.6875
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
SMath.varp = function (data) {
|
|
270
|
+
var mean = this.avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
|
|
271
|
+
return this.sum(squares) / data.length;
|
|
272
|
+
};
|
|
273
|
+
/**
|
|
274
|
+
* Compute the variance of a **sample**.
|
|
275
|
+
* @param data An array of numeric inputs
|
|
276
|
+
* @returns The sample variance
|
|
277
|
+
* @example
|
|
278
|
+
* ```js
|
|
279
|
+
* const y = SMath.vars([1, 2, 4, 4]); // 2.25
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
SMath.vars = function (data) {
|
|
283
|
+
var mean = this.avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
|
|
284
|
+
return this.sum(squares) / (data.length - 1);
|
|
285
|
+
};
|
|
286
|
+
/**
|
|
287
|
+
* Compute the standard deviation of a **complete population**.
|
|
288
|
+
* @param data An array of numeric inputs
|
|
289
|
+
* @returns The population standard deviation
|
|
290
|
+
* @example
|
|
291
|
+
* ```js
|
|
292
|
+
* const y = SMath.stdevp([1, 2, 3, 4]); // 1.118...
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
SMath.stdevp = function (data) {
|
|
296
|
+
return Math.sqrt(this.varp(data));
|
|
297
|
+
};
|
|
298
|
+
/**
|
|
299
|
+
* Compute the standard deviation of a **sample**.
|
|
300
|
+
* @param data An array of numeric inputs
|
|
301
|
+
* @returns The sample standard deviation
|
|
302
|
+
* @example
|
|
303
|
+
* ```js
|
|
304
|
+
* const y = SMath.stdevs([1, 2, 3, 4]); // 1.29...
|
|
305
|
+
* ```
|
|
306
|
+
*/
|
|
307
|
+
SMath.stdevs = function (data) {
|
|
308
|
+
return Math.sqrt(this.vars(data));
|
|
309
|
+
};
|
|
310
|
+
/**
|
|
311
|
+
* Take the limit of a function. A return value of `NaN` indicates
|
|
312
|
+
* that no limit exists either due to a discontinuity or imaginary value.
|
|
313
|
+
* @param f Function `f(x)`
|
|
314
|
+
* @param x The x-value where to take the limit
|
|
315
|
+
* @param h The approach distance
|
|
316
|
+
* @param discontinuity_cutoff The discontinuity cutoff
|
|
317
|
+
* @returns `lim(f(x->x))`
|
|
318
|
+
* @example
|
|
319
|
+
* ```js
|
|
320
|
+
* const y = SMath.lim(Math.log, 0); // -Infinity
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
SMath.lim = function (f, x, h, discontinuity_cutoff) {
|
|
324
|
+
if (h === void 0) { h = 1e-3; }
|
|
325
|
+
if (discontinuity_cutoff === void 0) { discontinuity_cutoff = 1; }
|
|
326
|
+
var center = f(x), left1 = f(x - h), left2 = f(x - h / 2), right1 = f(x + h), right2 = f(x + h / 2);
|
|
327
|
+
var left, right;
|
|
328
|
+
if (Number.isFinite(center)) {
|
|
329
|
+
return center;
|
|
330
|
+
}
|
|
331
|
+
// Check the limit approaching from the left
|
|
332
|
+
if (Number.isFinite(left1) && Number.isFinite(left2)) {
|
|
333
|
+
if (left2 > left1 + 2 * h) {
|
|
334
|
+
left = Infinity;
|
|
335
|
+
}
|
|
336
|
+
else if (left2 < left1 - 2 * h) {
|
|
337
|
+
left = -Infinity;
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
left = this.avg([left1, left2]);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
else if (left1 === left2) { // Handles +/-Infinity case
|
|
344
|
+
left = left1;
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
left = NaN;
|
|
348
|
+
}
|
|
349
|
+
// Check the limit approaching from the right
|
|
350
|
+
if (Number.isFinite(right1) && Number.isFinite(right2)) {
|
|
351
|
+
if (right2 > right1 + 2 * h) {
|
|
352
|
+
right = Infinity;
|
|
353
|
+
}
|
|
354
|
+
else if (right2 < right1 - 2 * h) {
|
|
355
|
+
right = -Infinity;
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
right = this.avg([right1, right2]);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
else if (right1 === right2) { // Handles +/-Infinity case
|
|
362
|
+
right = right1;
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
right = NaN;
|
|
366
|
+
}
|
|
367
|
+
// Check if limits match or are close
|
|
368
|
+
if (left === right) { // Handles +/-Infinity case
|
|
369
|
+
return left;
|
|
370
|
+
}
|
|
371
|
+
else if (SMath.approx(left, right, discontinuity_cutoff)) {
|
|
372
|
+
return this.avg([left, right]);
|
|
373
|
+
}
|
|
374
|
+
else if (!Number.isNaN(left) && Number.isNaN(right)) {
|
|
375
|
+
return left;
|
|
376
|
+
}
|
|
377
|
+
else if (Number.isNaN(left) && !Number.isNaN(right)) {
|
|
378
|
+
return right;
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
return NaN;
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
/**
|
|
385
|
+
* Take the derivative of a function.
|
|
386
|
+
* @param f Function `f(x)`
|
|
387
|
+
* @param x The x-value where to evaluate the derivative
|
|
388
|
+
* @param h Small step value
|
|
389
|
+
* @returns `f'(x)`
|
|
390
|
+
* @example
|
|
391
|
+
* ```js
|
|
392
|
+
* const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
|
|
393
|
+
* ```
|
|
394
|
+
*/
|
|
395
|
+
SMath.differentiate = function (f, x, h) {
|
|
396
|
+
if (h === void 0) { h = 1e-3; }
|
|
397
|
+
return (f(x + h) - f(x - h)) / (2 * h);
|
|
398
|
+
};
|
|
399
|
+
/**
|
|
400
|
+
* Compute the definite integral of a function.
|
|
401
|
+
* @param f Function `f(x)`
|
|
402
|
+
* @param a The miminum integral bound
|
|
403
|
+
* @param b The maximum integral bound
|
|
404
|
+
* @param Ndx The number of rectangles to compute
|
|
405
|
+
* @returns `F(b)-F(a)`
|
|
406
|
+
* @example
|
|
407
|
+
* ```js
|
|
408
|
+
* const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
411
|
+
SMath.integrate = function (f, a, b, Ndx) {
|
|
412
|
+
if (Ndx === void 0) { Ndx = 1e3; }
|
|
413
|
+
return ((b - a) / Ndx) * this.sum(this.linspace(a, b, Ndx).map(function (x) { return f(x); }));
|
|
414
|
+
};
|
|
257
415
|
return SMath;
|
|
258
416
|
}());
|
|
259
417
|
exports.SMath = SMath;
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -9,70 +9,6 @@
|
|
|
9
9
|
* useful interpolation and extrapolation functions.
|
|
10
10
|
*/
|
|
11
11
|
export declare abstract class SMath {
|
|
12
|
-
/**
|
|
13
|
-
* Add up all the inputs.
|
|
14
|
-
* If none are present, returns 0.
|
|
15
|
-
* @param n An array of numeric inputs
|
|
16
|
-
* @returns The sum total
|
|
17
|
-
* @example
|
|
18
|
-
* ```js
|
|
19
|
-
* const y = SMath.sum([1, 2, 3]); // 6
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
static sum(n: Array<number>): number;
|
|
23
|
-
/**
|
|
24
|
-
* Multiply all the inputs.
|
|
25
|
-
* If none are present, returns 1.
|
|
26
|
-
* @param n An array of numeric inputs
|
|
27
|
-
* @returns The product
|
|
28
|
-
* @example
|
|
29
|
-
* ```js
|
|
30
|
-
* const y = SMath.prod([2, 2, 3, 5]); // 60
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
static prod(n: Array<number>): number;
|
|
34
|
-
/**
|
|
35
|
-
* Compute the average, or mean, of a set of numbers.
|
|
36
|
-
* @param n An array of numeric inputs
|
|
37
|
-
* @returns The average, or mean
|
|
38
|
-
* @example
|
|
39
|
-
* ```js
|
|
40
|
-
* const y = SMath.avg([1, 2, 4, 4]); // 2.75
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
static avg(n: Array<number>): number;
|
|
44
|
-
/**
|
|
45
|
-
* Compute the variance of a **complete population**.
|
|
46
|
-
* @param n An array of numeric inputs
|
|
47
|
-
* @returns The population variance
|
|
48
|
-
* @example
|
|
49
|
-
* ```js
|
|
50
|
-
* const y = SMath.varp([1, 2, 4, 4]); // 1.6875
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
|
-
static varp(n: Array<number>): number;
|
|
54
|
-
/**
|
|
55
|
-
* Compute the variance of a **sample**.
|
|
56
|
-
* @param n An array of numeric inputs
|
|
57
|
-
* @returns The sample variance
|
|
58
|
-
* @example
|
|
59
|
-
* ```js
|
|
60
|
-
* const y = SMath.vars([1, 2, 4, 4]); // 2.25
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
static vars(n: Array<number>): number;
|
|
64
|
-
/**
|
|
65
|
-
* Compute the standard deviation of a **complete population**.
|
|
66
|
-
* @param n An array of numeric inputs
|
|
67
|
-
* @returns The population standard deviation
|
|
68
|
-
*/
|
|
69
|
-
static stdevp(n: Array<number>): number;
|
|
70
|
-
/**
|
|
71
|
-
* Compute the standard deviation of a **sample**.
|
|
72
|
-
* @param n An array of numeric inputs
|
|
73
|
-
* @returns The sample standard deviation
|
|
74
|
-
*/
|
|
75
|
-
static stdevs(n: Array<number>): number;
|
|
76
12
|
/**
|
|
77
13
|
* Check if two numbers are approximately equal with a maximum abolute error.
|
|
78
14
|
* @param a Any number
|
|
@@ -174,6 +110,16 @@ export declare abstract class SMath {
|
|
|
174
110
|
* ```
|
|
175
111
|
*/
|
|
176
112
|
static factorial(n: number): number;
|
|
113
|
+
/**
|
|
114
|
+
* Factorize `n` into its prime factors.
|
|
115
|
+
* @param n Any positive integer
|
|
116
|
+
* @returns The array of prime factors
|
|
117
|
+
* @example
|
|
118
|
+
* ```js
|
|
119
|
+
* const y = SMath.factors(12); // [ 2, 2, 3 ]
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
static factors(n: number): Array<number>;
|
|
177
123
|
/**
|
|
178
124
|
* Calculate the relative normalized error or deviation from any
|
|
179
125
|
* value to an accepted value. An error of 0 indicates that the
|
|
@@ -190,4 +136,125 @@ export declare abstract class SMath {
|
|
|
190
136
|
* ```
|
|
191
137
|
*/
|
|
192
138
|
static error(experimental: number, actual: number): number;
|
|
139
|
+
/**
|
|
140
|
+
* Add up all the inputs.
|
|
141
|
+
* If none are present, returns 0.
|
|
142
|
+
* @param data An array of numeric inputs
|
|
143
|
+
* @returns The sum total
|
|
144
|
+
* @example
|
|
145
|
+
* ```js
|
|
146
|
+
* const y = SMath.sum([1, 2, 3]); // 6
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
static sum(data: Array<number>): number;
|
|
150
|
+
/**
|
|
151
|
+
* Multiply all the inputs.
|
|
152
|
+
* If none are present, returns 1.
|
|
153
|
+
* @param data An array of numeric inputs
|
|
154
|
+
* @returns The product
|
|
155
|
+
* @example
|
|
156
|
+
* ```js
|
|
157
|
+
* const y = SMath.prod([2, 2, 3, 5]); // 60
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
static prod(data: Array<number>): number;
|
|
161
|
+
/**
|
|
162
|
+
* Compute the average, or mean, of a set of numbers.
|
|
163
|
+
* @param data An array of numeric inputs
|
|
164
|
+
* @returns The average, or mean
|
|
165
|
+
* @example
|
|
166
|
+
* ```js
|
|
167
|
+
* const y = SMath.avg([1, 2, 4, 4]); // 2.75
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
static avg(data: Array<number>): number;
|
|
171
|
+
/**
|
|
172
|
+
* Compute the median of a set of numbers.
|
|
173
|
+
* @param data An array of numeric inputs
|
|
174
|
+
* @returns The median of the dataset
|
|
175
|
+
* @example
|
|
176
|
+
* ```js
|
|
177
|
+
* const y = SMath.median([2, 5, 3, 1]); // 2.5
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
static median(data: Array<number>): number;
|
|
181
|
+
/**
|
|
182
|
+
* Compute the variance of a **complete population**.
|
|
183
|
+
* @param data An array of numeric inputs
|
|
184
|
+
* @returns The population variance
|
|
185
|
+
* @example
|
|
186
|
+
* ```js
|
|
187
|
+
* const y = SMath.varp([1, 2, 4, 4]); // 1.6875
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
static varp(data: Array<number>): number;
|
|
191
|
+
/**
|
|
192
|
+
* Compute the variance of a **sample**.
|
|
193
|
+
* @param data An array of numeric inputs
|
|
194
|
+
* @returns The sample variance
|
|
195
|
+
* @example
|
|
196
|
+
* ```js
|
|
197
|
+
* const y = SMath.vars([1, 2, 4, 4]); // 2.25
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
static vars(data: Array<number>): number;
|
|
201
|
+
/**
|
|
202
|
+
* Compute the standard deviation of a **complete population**.
|
|
203
|
+
* @param data An array of numeric inputs
|
|
204
|
+
* @returns The population standard deviation
|
|
205
|
+
* @example
|
|
206
|
+
* ```js
|
|
207
|
+
* const y = SMath.stdevp([1, 2, 3, 4]); // 1.118...
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
static stdevp(data: Array<number>): number;
|
|
211
|
+
/**
|
|
212
|
+
* Compute the standard deviation of a **sample**.
|
|
213
|
+
* @param data An array of numeric inputs
|
|
214
|
+
* @returns The sample standard deviation
|
|
215
|
+
* @example
|
|
216
|
+
* ```js
|
|
217
|
+
* const y = SMath.stdevs([1, 2, 3, 4]); // 1.29...
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
static stdevs(data: Array<number>): number;
|
|
221
|
+
/**
|
|
222
|
+
* Take the limit of a function. A return value of `NaN` indicates
|
|
223
|
+
* that no limit exists either due to a discontinuity or imaginary value.
|
|
224
|
+
* @param f Function `f(x)`
|
|
225
|
+
* @param x The x-value where to take the limit
|
|
226
|
+
* @param h The approach distance
|
|
227
|
+
* @param discontinuity_cutoff The discontinuity cutoff
|
|
228
|
+
* @returns `lim(f(x->x))`
|
|
229
|
+
* @example
|
|
230
|
+
* ```js
|
|
231
|
+
* const y = SMath.lim(Math.log, 0); // -Infinity
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
static lim(f: (x: number) => number, x: number, h?: number, discontinuity_cutoff?: number): number;
|
|
235
|
+
/**
|
|
236
|
+
* Take the derivative of a function.
|
|
237
|
+
* @param f Function `f(x)`
|
|
238
|
+
* @param x The x-value where to evaluate the derivative
|
|
239
|
+
* @param h Small step value
|
|
240
|
+
* @returns `f'(x)`
|
|
241
|
+
* @example
|
|
242
|
+
* ```js
|
|
243
|
+
* const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
static differentiate(f: (x: number) => number, x: number, h?: number): number;
|
|
247
|
+
/**
|
|
248
|
+
* Compute the definite integral of a function.
|
|
249
|
+
* @param f Function `f(x)`
|
|
250
|
+
* @param a The miminum integral bound
|
|
251
|
+
* @param b The maximum integral bound
|
|
252
|
+
* @param Ndx The number of rectangles to compute
|
|
253
|
+
* @returns `F(b)-F(a)`
|
|
254
|
+
* @example
|
|
255
|
+
* ```js
|
|
256
|
+
* const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
static integrate(f: (x: number) => number, a: number, b: number, Ndx?: number): number;
|
|
193
260
|
}
|