smath 1.7.0 → 1.8.0
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/index.js +97 -75
- package/package.json +5 -5
- package/types/index.d.ts +26 -26
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.8.0
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
## Bugs and Requests
|
package/dist/index.js
CHANGED
|
@@ -11,9 +11,8 @@ exports.SMath = void 0;
|
|
|
11
11
|
* Contains a small math function library including
|
|
12
12
|
* useful interpolation and extrapolation functions.
|
|
13
13
|
*/
|
|
14
|
-
var SMath
|
|
15
|
-
|
|
16
|
-
}
|
|
14
|
+
var SMath;
|
|
15
|
+
(function (SMath) {
|
|
17
16
|
/**
|
|
18
17
|
* Check if two numbers are approximately equal with a maximum abolute error.
|
|
19
18
|
* @param a Any number
|
|
@@ -26,10 +25,11 @@ var SMath = /** @class */ (function () {
|
|
|
26
25
|
* b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
|
|
27
26
|
* ```
|
|
28
27
|
*/
|
|
29
|
-
|
|
28
|
+
function approx(a, b, epsilon) {
|
|
30
29
|
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
31
30
|
return a - b < epsilon && b - a < epsilon;
|
|
32
|
-
}
|
|
31
|
+
}
|
|
32
|
+
SMath.approx = approx;
|
|
33
33
|
/**
|
|
34
34
|
* Clamp a number within a range.
|
|
35
35
|
* @param n The number to clamp
|
|
@@ -42,7 +42,7 @@ var SMath = /** @class */ (function () {
|
|
|
42
42
|
* n2 = SMath.clamp(-2, 0, 10); // 0
|
|
43
43
|
* ```
|
|
44
44
|
*/
|
|
45
|
-
|
|
45
|
+
function clamp(n, min, max) {
|
|
46
46
|
if (n < min) {
|
|
47
47
|
return min;
|
|
48
48
|
}
|
|
@@ -50,7 +50,8 @@ var SMath = /** @class */ (function () {
|
|
|
50
50
|
return max;
|
|
51
51
|
}
|
|
52
52
|
return n;
|
|
53
|
-
}
|
|
53
|
+
}
|
|
54
|
+
SMath.clamp = clamp;
|
|
54
55
|
/**
|
|
55
56
|
* Normalize the number `n` from the range `min, max` to the range `0, 1`
|
|
56
57
|
* @param n The number to normalize
|
|
@@ -62,12 +63,13 @@ var SMath = /** @class */ (function () {
|
|
|
62
63
|
* const y = SMath.normalize(18, 9, 99); // 0.1
|
|
63
64
|
* ```
|
|
64
65
|
*/
|
|
65
|
-
|
|
66
|
+
function normalize(n, min, max) {
|
|
66
67
|
if (min === max) {
|
|
67
68
|
return 0;
|
|
68
69
|
}
|
|
69
70
|
return (n - min) / (max - min);
|
|
70
|
-
}
|
|
71
|
+
}
|
|
72
|
+
SMath.normalize = normalize;
|
|
71
73
|
/**
|
|
72
74
|
* Expand a normalized number `n` to the range `min, max`
|
|
73
75
|
* @param n A normalized number
|
|
@@ -79,9 +81,10 @@ var SMath = /** @class */ (function () {
|
|
|
79
81
|
* const y = SMath.expand(0.25, 4, 6); // 4.5
|
|
80
82
|
* ```
|
|
81
83
|
*/
|
|
82
|
-
|
|
84
|
+
function expand(n, min, max) {
|
|
83
85
|
return (max - min) * n + min;
|
|
84
|
-
}
|
|
86
|
+
}
|
|
87
|
+
SMath.expand = expand;
|
|
85
88
|
/**
|
|
86
89
|
* Translate a number `n` from the range `min1, max1` to the range `min2, max2`
|
|
87
90
|
* @param n The number to translate
|
|
@@ -96,9 +99,10 @@ var SMath = /** @class */ (function () {
|
|
|
96
99
|
* F = SMath.translate(C, 0, 100, 32, 212); // 68
|
|
97
100
|
* ```
|
|
98
101
|
*/
|
|
99
|
-
|
|
100
|
-
return
|
|
101
|
-
}
|
|
102
|
+
function translate(n, min1, max1, min2, max2) {
|
|
103
|
+
return expand(normalize(n, min1, max1), min2, max2);
|
|
104
|
+
}
|
|
105
|
+
SMath.translate = translate;
|
|
102
106
|
/**
|
|
103
107
|
* Generate an array of linearly spaced numbers.
|
|
104
108
|
* @param min The initial value of the linear space
|
|
@@ -111,13 +115,14 @@ var SMath = /** @class */ (function () {
|
|
|
111
115
|
* // [ 1, 1.8, 2.6, 3.4, 4.2, 5 ]
|
|
112
116
|
* ```
|
|
113
117
|
*/
|
|
114
|
-
|
|
118
|
+
function linspace(min, max, count) {
|
|
115
119
|
var space = [];
|
|
116
120
|
for (var i = 0; i < count; i++) {
|
|
117
|
-
space[i] =
|
|
121
|
+
space[i] = translate(i, 0, count - 1, min, max);
|
|
118
122
|
}
|
|
119
123
|
return space;
|
|
120
|
-
}
|
|
124
|
+
}
|
|
125
|
+
SMath.linspace = linspace;
|
|
121
126
|
/**
|
|
122
127
|
* Generate an array of logarithmically spaced numbers.
|
|
123
128
|
* @param min The initial magnitude of the space
|
|
@@ -130,9 +135,10 @@ var SMath = /** @class */ (function () {
|
|
|
130
135
|
* // [ 1, 3.2, 10, 31.6, 100 ]
|
|
131
136
|
* ```
|
|
132
137
|
*/
|
|
133
|
-
|
|
134
|
-
return
|
|
135
|
-
}
|
|
138
|
+
function logspace(min, max, count) {
|
|
139
|
+
return linspace(min, max, count).map(function (n) { return Math.pow(10, n); });
|
|
140
|
+
}
|
|
141
|
+
SMath.logspace = logspace;
|
|
136
142
|
/**
|
|
137
143
|
* Compute the factorial of `n`.
|
|
138
144
|
* @param n Any positive integer
|
|
@@ -142,7 +148,7 @@ var SMath = /** @class */ (function () {
|
|
|
142
148
|
* const y = SMath.factorial(5); // 120
|
|
143
149
|
* ```
|
|
144
150
|
*/
|
|
145
|
-
|
|
151
|
+
function factorial(n) {
|
|
146
152
|
if (n < 0 || (n | 0) !== n) {
|
|
147
153
|
throw new Error('Input must be a positive integer.');
|
|
148
154
|
}
|
|
@@ -153,9 +159,10 @@ var SMath = /** @class */ (function () {
|
|
|
153
159
|
return n;
|
|
154
160
|
}
|
|
155
161
|
else {
|
|
156
|
-
return n *
|
|
162
|
+
return n * factorial(n - 1);
|
|
157
163
|
}
|
|
158
|
-
}
|
|
164
|
+
}
|
|
165
|
+
SMath.factorial = factorial;
|
|
159
166
|
/**
|
|
160
167
|
* Factorize `n` into its prime factors.
|
|
161
168
|
* @param n Any positive integer
|
|
@@ -165,7 +172,7 @@ var SMath = /** @class */ (function () {
|
|
|
165
172
|
* const y = SMath.factors(12); // [ 2, 2, 3 ]
|
|
166
173
|
* ```
|
|
167
174
|
*/
|
|
168
|
-
|
|
175
|
+
function factors(n) {
|
|
169
176
|
if (n < 0 || (n | 0) !== n) {
|
|
170
177
|
throw new Error('Input must be a positive integer!');
|
|
171
178
|
}
|
|
@@ -184,7 +191,8 @@ var SMath = /** @class */ (function () {
|
|
|
184
191
|
}
|
|
185
192
|
}
|
|
186
193
|
return f;
|
|
187
|
-
}
|
|
194
|
+
}
|
|
195
|
+
SMath.factors = factors;
|
|
188
196
|
/**
|
|
189
197
|
* Calculate the relative normalized error or deviation from any
|
|
190
198
|
* value to an accepted value. An error of 0 indicates that the
|
|
@@ -200,9 +208,10 @@ var SMath = /** @class */ (function () {
|
|
|
200
208
|
* const e = SMath.error(22.5, 25); // -0.1
|
|
201
209
|
* ```
|
|
202
210
|
*/
|
|
203
|
-
|
|
211
|
+
function error(experimental, actual) {
|
|
204
212
|
return (experimental - actual) / actual;
|
|
205
|
-
}
|
|
213
|
+
}
|
|
214
|
+
SMath.error = error;
|
|
206
215
|
/**
|
|
207
216
|
* Add up all the inputs.
|
|
208
217
|
* If none are present, returns 0.
|
|
@@ -213,9 +222,10 @@ var SMath = /** @class */ (function () {
|
|
|
213
222
|
* const y = SMath.sum([1, 2, 3]); // 6
|
|
214
223
|
* ```
|
|
215
224
|
*/
|
|
216
|
-
|
|
225
|
+
function sum(data) {
|
|
217
226
|
return data.reduce(function (a, b) { return a + b; }, 0);
|
|
218
|
-
}
|
|
227
|
+
}
|
|
228
|
+
SMath.sum = sum;
|
|
219
229
|
/**
|
|
220
230
|
* Multiply all the inputs.
|
|
221
231
|
* If none are present, returns 1.
|
|
@@ -226,9 +236,10 @@ var SMath = /** @class */ (function () {
|
|
|
226
236
|
* const y = SMath.prod([2, 2, 3, 5]); // 60
|
|
227
237
|
* ```
|
|
228
238
|
*/
|
|
229
|
-
|
|
239
|
+
function prod(data) {
|
|
230
240
|
return data.reduce(function (a, b) { return a * b; }, 1);
|
|
231
|
-
}
|
|
241
|
+
}
|
|
242
|
+
SMath.prod = prod;
|
|
232
243
|
/**
|
|
233
244
|
* Compute the average, or mean, of a set of numbers.
|
|
234
245
|
* @param data An array of numeric inputs
|
|
@@ -238,9 +249,10 @@ var SMath = /** @class */ (function () {
|
|
|
238
249
|
* const y = SMath.avg([1, 2, 4, 4]); // 2.75
|
|
239
250
|
* ```
|
|
240
251
|
*/
|
|
241
|
-
|
|
242
|
-
return
|
|
243
|
-
}
|
|
252
|
+
function avg(data) {
|
|
253
|
+
return sum(data) / data.length;
|
|
254
|
+
}
|
|
255
|
+
SMath.avg = avg;
|
|
244
256
|
/**
|
|
245
257
|
* Compute the median of a set of numbers.
|
|
246
258
|
* @param data An array of numeric inputs
|
|
@@ -250,13 +262,14 @@ var SMath = /** @class */ (function () {
|
|
|
250
262
|
* const y = SMath.median([2, 5, 3, 1]); // 2.5
|
|
251
263
|
* ```
|
|
252
264
|
*/
|
|
253
|
-
|
|
265
|
+
function median(data) {
|
|
254
266
|
data.sort(function (a, b) { return a - b; });
|
|
255
267
|
if (data.length % 2) {
|
|
256
268
|
return data[(data.length - 1) / 2];
|
|
257
269
|
}
|
|
258
|
-
return
|
|
259
|
-
}
|
|
270
|
+
return avg([data[data.length / 2 - 1], data[data.length / 2]]);
|
|
271
|
+
}
|
|
272
|
+
SMath.median = median;
|
|
260
273
|
/**
|
|
261
274
|
* Compute the variance of a **complete population**.
|
|
262
275
|
* @param data An array of numeric inputs
|
|
@@ -266,10 +279,11 @@ var SMath = /** @class */ (function () {
|
|
|
266
279
|
* const y = SMath.varp([1, 2, 4, 4]); // 1.6875
|
|
267
280
|
* ```
|
|
268
281
|
*/
|
|
269
|
-
|
|
270
|
-
var mean =
|
|
271
|
-
return
|
|
272
|
-
}
|
|
282
|
+
function varp(data) {
|
|
283
|
+
var mean = avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
|
|
284
|
+
return sum(squares) / data.length;
|
|
285
|
+
}
|
|
286
|
+
SMath.varp = varp;
|
|
273
287
|
/**
|
|
274
288
|
* Compute the variance of a **sample**.
|
|
275
289
|
* @param data An array of numeric inputs
|
|
@@ -279,10 +293,11 @@ var SMath = /** @class */ (function () {
|
|
|
279
293
|
* const y = SMath.vars([1, 2, 4, 4]); // 2.25
|
|
280
294
|
* ```
|
|
281
295
|
*/
|
|
282
|
-
|
|
283
|
-
var mean =
|
|
284
|
-
return
|
|
285
|
-
}
|
|
296
|
+
function vars(data) {
|
|
297
|
+
var mean = avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
|
|
298
|
+
return sum(squares) / (data.length - 1);
|
|
299
|
+
}
|
|
300
|
+
SMath.vars = vars;
|
|
286
301
|
/**
|
|
287
302
|
* Compute the standard deviation of a **complete population**.
|
|
288
303
|
* @param data An array of numeric inputs
|
|
@@ -292,9 +307,10 @@ var SMath = /** @class */ (function () {
|
|
|
292
307
|
* const y = SMath.stdevp([1, 2, 3, 4]); // 1.118...
|
|
293
308
|
* ```
|
|
294
309
|
*/
|
|
295
|
-
|
|
296
|
-
return Math.sqrt(
|
|
297
|
-
}
|
|
310
|
+
function stdevp(data) {
|
|
311
|
+
return Math.sqrt(varp(data));
|
|
312
|
+
}
|
|
313
|
+
SMath.stdevp = stdevp;
|
|
298
314
|
/**
|
|
299
315
|
* Compute the standard deviation of a **sample**.
|
|
300
316
|
* @param data An array of numeric inputs
|
|
@@ -304,9 +320,10 @@ var SMath = /** @class */ (function () {
|
|
|
304
320
|
* const y = SMath.stdevs([1, 2, 3, 4]); // 1.29...
|
|
305
321
|
* ```
|
|
306
322
|
*/
|
|
307
|
-
|
|
308
|
-
return Math.sqrt(
|
|
309
|
-
}
|
|
323
|
+
function stdevs(data) {
|
|
324
|
+
return Math.sqrt(vars(data));
|
|
325
|
+
}
|
|
326
|
+
SMath.stdevs = stdevs;
|
|
310
327
|
/**
|
|
311
328
|
* Generate a uniformly-distributed floating-point number within the range.
|
|
312
329
|
* @param min The minimum bound
|
|
@@ -317,9 +334,10 @@ var SMath = /** @class */ (function () {
|
|
|
317
334
|
* const y = SMath.runif(-2, 2); // 0.376...
|
|
318
335
|
* ```
|
|
319
336
|
*/
|
|
320
|
-
|
|
321
|
-
return
|
|
322
|
-
}
|
|
337
|
+
function runif(min, max) {
|
|
338
|
+
return expand(Math.random(), min, max);
|
|
339
|
+
}
|
|
340
|
+
SMath.runif = runif;
|
|
323
341
|
/**
|
|
324
342
|
* Generate a uniformly-distributed integer within the range.
|
|
325
343
|
* @param min The minimum bound (inclusive)
|
|
@@ -330,7 +348,7 @@ var SMath = /** @class */ (function () {
|
|
|
330
348
|
* const y = SMath.rint(-4, 3); // -4
|
|
331
349
|
* ```
|
|
332
350
|
*/
|
|
333
|
-
|
|
351
|
+
function rint(min, max) {
|
|
334
352
|
min |= 0;
|
|
335
353
|
max |= 0;
|
|
336
354
|
if (min < 0) {
|
|
@@ -339,8 +357,9 @@ var SMath = /** @class */ (function () {
|
|
|
339
357
|
if (max < 0) {
|
|
340
358
|
max--;
|
|
341
359
|
}
|
|
342
|
-
return
|
|
343
|
-
}
|
|
360
|
+
return runif(min, max + 1) | 0; // `| 0` pulls toward 0
|
|
361
|
+
}
|
|
362
|
+
SMath.rint = rint;
|
|
344
363
|
/**
|
|
345
364
|
* Generate a normally-distributed floating-point number.
|
|
346
365
|
* @param mean The mean of the population distribution
|
|
@@ -351,11 +370,12 @@ var SMath = /** @class */ (function () {
|
|
|
351
370
|
* const y = SMath.rnorm(2, 3); // 1.627...
|
|
352
371
|
* ```
|
|
353
372
|
*/
|
|
354
|
-
|
|
373
|
+
function rnorm(mean, stdev) {
|
|
355
374
|
if (mean === void 0) { mean = 0; }
|
|
356
375
|
if (stdev === void 0) { stdev = 1; }
|
|
357
376
|
return mean + stdev * Math.sqrt(-2 * Math.log(Math.random())) * Math.cos(2 * Math.PI * Math.random());
|
|
358
|
-
}
|
|
377
|
+
}
|
|
378
|
+
SMath.rnorm = rnorm;
|
|
359
379
|
/**
|
|
360
380
|
* Generate a population of normally-distributed floating-point numbers.
|
|
361
381
|
* @param count The number of values to generate
|
|
@@ -367,15 +387,16 @@ var SMath = /** @class */ (function () {
|
|
|
367
387
|
* const dataset = SMath.rdist(3); // [ 1.051..., -0.779..., -2.254... ]
|
|
368
388
|
* ```
|
|
369
389
|
*/
|
|
370
|
-
|
|
390
|
+
function rdist(count, mean, stdev) {
|
|
371
391
|
if (mean === void 0) { mean = 0; }
|
|
372
392
|
if (stdev === void 0) { stdev = 1; }
|
|
373
393
|
var distribution = [];
|
|
374
394
|
for (var i = 0; i < count; i++) {
|
|
375
|
-
distribution[i] =
|
|
395
|
+
distribution[i] = rnorm(mean, stdev);
|
|
376
396
|
}
|
|
377
397
|
return distribution;
|
|
378
|
-
}
|
|
398
|
+
}
|
|
399
|
+
SMath.rdist = rdist;
|
|
379
400
|
/**
|
|
380
401
|
* Take the limit of a function. A return value of `NaN` indicates
|
|
381
402
|
* that no limit exists either due to a discontinuity or imaginary value.
|
|
@@ -389,7 +410,7 @@ var SMath = /** @class */ (function () {
|
|
|
389
410
|
* const y = SMath.lim(Math.log, 0); // -Infinity
|
|
390
411
|
* ```
|
|
391
412
|
*/
|
|
392
|
-
|
|
413
|
+
function lim(f, x, h, discontinuity_cutoff) {
|
|
393
414
|
if (h === void 0) { h = 1e-3; }
|
|
394
415
|
if (discontinuity_cutoff === void 0) { discontinuity_cutoff = 1; }
|
|
395
416
|
var center = f(x), left1 = f(x - h), left2 = f(x - h / 2), right1 = f(x + h), right2 = f(x + h / 2);
|
|
@@ -406,7 +427,7 @@ var SMath = /** @class */ (function () {
|
|
|
406
427
|
left = -Infinity;
|
|
407
428
|
}
|
|
408
429
|
else {
|
|
409
|
-
left =
|
|
430
|
+
left = avg([left1, left2]);
|
|
410
431
|
}
|
|
411
432
|
}
|
|
412
433
|
else if (left1 === left2) { // Handles +/-Infinity case
|
|
@@ -424,7 +445,7 @@ var SMath = /** @class */ (function () {
|
|
|
424
445
|
right = -Infinity;
|
|
425
446
|
}
|
|
426
447
|
else {
|
|
427
|
-
right =
|
|
448
|
+
right = avg([right1, right2]);
|
|
428
449
|
}
|
|
429
450
|
}
|
|
430
451
|
else if (right1 === right2) { // Handles +/-Infinity case
|
|
@@ -438,7 +459,7 @@ var SMath = /** @class */ (function () {
|
|
|
438
459
|
return left;
|
|
439
460
|
}
|
|
440
461
|
else if (SMath.approx(left, right, discontinuity_cutoff)) {
|
|
441
|
-
return
|
|
462
|
+
return avg([left, right]);
|
|
442
463
|
}
|
|
443
464
|
else if (!Number.isNaN(left) && Number.isNaN(right)) {
|
|
444
465
|
return left;
|
|
@@ -449,7 +470,8 @@ var SMath = /** @class */ (function () {
|
|
|
449
470
|
else {
|
|
450
471
|
return NaN;
|
|
451
472
|
}
|
|
452
|
-
}
|
|
473
|
+
}
|
|
474
|
+
SMath.lim = lim;
|
|
453
475
|
/**
|
|
454
476
|
* Take the derivative of a function.
|
|
455
477
|
* @param f Function `f(x)`
|
|
@@ -461,10 +483,11 @@ var SMath = /** @class */ (function () {
|
|
|
461
483
|
* const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
|
|
462
484
|
* ```
|
|
463
485
|
*/
|
|
464
|
-
|
|
486
|
+
function differentiate(f, x, h) {
|
|
465
487
|
if (h === void 0) { h = 1e-3; }
|
|
466
488
|
return (f(x + h) - f(x - h)) / (2 * h);
|
|
467
|
-
}
|
|
489
|
+
}
|
|
490
|
+
SMath.differentiate = differentiate;
|
|
468
491
|
/**
|
|
469
492
|
* Compute the definite integral of a function.
|
|
470
493
|
* @param f Function `f(x)`
|
|
@@ -477,10 +500,9 @@ var SMath = /** @class */ (function () {
|
|
|
477
500
|
* const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
|
|
478
501
|
* ```
|
|
479
502
|
*/
|
|
480
|
-
|
|
503
|
+
function integrate(f, a, b, Ndx) {
|
|
481
504
|
if (Ndx === void 0) { Ndx = 1e3; }
|
|
482
|
-
return ((b - a) / Ndx) *
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
}());
|
|
486
|
-
exports.SMath = SMath;
|
|
505
|
+
return ((b - a) / Ndx) * sum(linspace(a, b, Ndx).map(function (x) { return f(x); }));
|
|
506
|
+
}
|
|
507
|
+
SMath.integrate = integrate;
|
|
508
|
+
})(SMath || (exports.SMath = SMath = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smath",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Small math function library",
|
|
5
5
|
"homepage": "https://npm.nicfv.com/smath",
|
|
6
6
|
"bin": "dist/bin.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"build": "rm -rf dist types && tsc && node dist/test.js && rm dist/test.js",
|
|
15
15
|
"test": "tsc --noEmit",
|
|
16
16
|
"clean": "rm -rf node_modules package-lock.json dist types docs",
|
|
17
|
-
"docs": "rm -rf docs && typedoc --includeVersion --disableSources --hideGenerator src",
|
|
17
|
+
"docs": "rm -rf docs && typedoc --includeVersion --disableSources --hideGenerator --excludePrivate --excludeProtected src",
|
|
18
18
|
"prepack": "npm run build",
|
|
19
19
|
"postpack": "rm -rf dist types"
|
|
20
20
|
},
|
|
@@ -57,9 +57,9 @@
|
|
|
57
57
|
"repository": "github:nicfv/npm",
|
|
58
58
|
"license": "MIT",
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@types/node": "20.12.
|
|
60
|
+
"@types/node": "20.12.7",
|
|
61
61
|
"exray": "1.0.3",
|
|
62
|
-
"typedoc": "0.25.
|
|
63
|
-
"typescript": "5.4.
|
|
62
|
+
"typedoc": "0.25.13",
|
|
63
|
+
"typescript": "5.4.5"
|
|
64
64
|
}
|
|
65
65
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* Contains a small math function library including
|
|
9
9
|
* useful interpolation and extrapolation functions.
|
|
10
10
|
*/
|
|
11
|
-
export declare
|
|
11
|
+
export declare namespace SMath {
|
|
12
12
|
/**
|
|
13
13
|
* Check if two numbers are approximately equal with a maximum abolute error.
|
|
14
14
|
* @param a Any number
|
|
@@ -21,7 +21,7 @@ export declare abstract class SMath {
|
|
|
21
21
|
* b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
|
|
24
|
+
function approx(a: number, b: number, epsilon?: number): boolean;
|
|
25
25
|
/**
|
|
26
26
|
* Clamp a number within a range.
|
|
27
27
|
* @param n The number to clamp
|
|
@@ -34,7 +34,7 @@ export declare abstract class SMath {
|
|
|
34
34
|
* n2 = SMath.clamp(-2, 0, 10); // 0
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
|
-
|
|
37
|
+
function clamp(n: number, min: number, max: number): number;
|
|
38
38
|
/**
|
|
39
39
|
* Normalize the number `n` from the range `min, max` to the range `0, 1`
|
|
40
40
|
* @param n The number to normalize
|
|
@@ -46,7 +46,7 @@ export declare abstract class SMath {
|
|
|
46
46
|
* const y = SMath.normalize(18, 9, 99); // 0.1
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
|
-
|
|
49
|
+
function normalize(n: number, min: number, max: number): number;
|
|
50
50
|
/**
|
|
51
51
|
* Expand a normalized number `n` to the range `min, max`
|
|
52
52
|
* @param n A normalized number
|
|
@@ -58,7 +58,7 @@ export declare abstract class SMath {
|
|
|
58
58
|
* const y = SMath.expand(0.25, 4, 6); // 4.5
|
|
59
59
|
* ```
|
|
60
60
|
*/
|
|
61
|
-
|
|
61
|
+
function expand(n: number, min: number, max: number): number;
|
|
62
62
|
/**
|
|
63
63
|
* Translate a number `n` from the range `min1, max1` to the range `min2, max2`
|
|
64
64
|
* @param n The number to translate
|
|
@@ -73,7 +73,7 @@ export declare abstract class SMath {
|
|
|
73
73
|
* F = SMath.translate(C, 0, 100, 32, 212); // 68
|
|
74
74
|
* ```
|
|
75
75
|
*/
|
|
76
|
-
|
|
76
|
+
function translate(n: number, min1: number, max1: number, min2: number, max2: number): number;
|
|
77
77
|
/**
|
|
78
78
|
* Generate an array of linearly spaced numbers.
|
|
79
79
|
* @param min The initial value of the linear space
|
|
@@ -86,7 +86,7 @@ export declare abstract class SMath {
|
|
|
86
86
|
* // [ 1, 1.8, 2.6, 3.4, 4.2, 5 ]
|
|
87
87
|
* ```
|
|
88
88
|
*/
|
|
89
|
-
|
|
89
|
+
function linspace(min: number, max: number, count: number): Array<number>;
|
|
90
90
|
/**
|
|
91
91
|
* Generate an array of logarithmically spaced numbers.
|
|
92
92
|
* @param min The initial magnitude of the space
|
|
@@ -99,7 +99,7 @@ export declare abstract class SMath {
|
|
|
99
99
|
* // [ 1, 3.2, 10, 31.6, 100 ]
|
|
100
100
|
* ```
|
|
101
101
|
*/
|
|
102
|
-
|
|
102
|
+
function logspace(min: number, max: number, count: number): Array<number>;
|
|
103
103
|
/**
|
|
104
104
|
* Compute the factorial of `n`.
|
|
105
105
|
* @param n Any positive integer
|
|
@@ -109,7 +109,7 @@ export declare abstract class SMath {
|
|
|
109
109
|
* const y = SMath.factorial(5); // 120
|
|
110
110
|
* ```
|
|
111
111
|
*/
|
|
112
|
-
|
|
112
|
+
function factorial(n: number): number;
|
|
113
113
|
/**
|
|
114
114
|
* Factorize `n` into its prime factors.
|
|
115
115
|
* @param n Any positive integer
|
|
@@ -119,7 +119,7 @@ export declare abstract class SMath {
|
|
|
119
119
|
* const y = SMath.factors(12); // [ 2, 2, 3 ]
|
|
120
120
|
* ```
|
|
121
121
|
*/
|
|
122
|
-
|
|
122
|
+
function factors(n: number): Array<number>;
|
|
123
123
|
/**
|
|
124
124
|
* Calculate the relative normalized error or deviation from any
|
|
125
125
|
* value to an accepted value. An error of 0 indicates that the
|
|
@@ -135,7 +135,7 @@ export declare abstract class SMath {
|
|
|
135
135
|
* const e = SMath.error(22.5, 25); // -0.1
|
|
136
136
|
* ```
|
|
137
137
|
*/
|
|
138
|
-
|
|
138
|
+
function error(experimental: number, actual: number): number;
|
|
139
139
|
/**
|
|
140
140
|
* Add up all the inputs.
|
|
141
141
|
* If none are present, returns 0.
|
|
@@ -146,7 +146,7 @@ export declare abstract class SMath {
|
|
|
146
146
|
* const y = SMath.sum([1, 2, 3]); // 6
|
|
147
147
|
* ```
|
|
148
148
|
*/
|
|
149
|
-
|
|
149
|
+
function sum(data: Array<number>): number;
|
|
150
150
|
/**
|
|
151
151
|
* Multiply all the inputs.
|
|
152
152
|
* If none are present, returns 1.
|
|
@@ -157,7 +157,7 @@ export declare abstract class SMath {
|
|
|
157
157
|
* const y = SMath.prod([2, 2, 3, 5]); // 60
|
|
158
158
|
* ```
|
|
159
159
|
*/
|
|
160
|
-
|
|
160
|
+
function prod(data: Array<number>): number;
|
|
161
161
|
/**
|
|
162
162
|
* Compute the average, or mean, of a set of numbers.
|
|
163
163
|
* @param data An array of numeric inputs
|
|
@@ -167,7 +167,7 @@ export declare abstract class SMath {
|
|
|
167
167
|
* const y = SMath.avg([1, 2, 4, 4]); // 2.75
|
|
168
168
|
* ```
|
|
169
169
|
*/
|
|
170
|
-
|
|
170
|
+
function avg(data: Array<number>): number;
|
|
171
171
|
/**
|
|
172
172
|
* Compute the median of a set of numbers.
|
|
173
173
|
* @param data An array of numeric inputs
|
|
@@ -177,7 +177,7 @@ export declare abstract class SMath {
|
|
|
177
177
|
* const y = SMath.median([2, 5, 3, 1]); // 2.5
|
|
178
178
|
* ```
|
|
179
179
|
*/
|
|
180
|
-
|
|
180
|
+
function median(data: Array<number>): number;
|
|
181
181
|
/**
|
|
182
182
|
* Compute the variance of a **complete population**.
|
|
183
183
|
* @param data An array of numeric inputs
|
|
@@ -187,7 +187,7 @@ export declare abstract class SMath {
|
|
|
187
187
|
* const y = SMath.varp([1, 2, 4, 4]); // 1.6875
|
|
188
188
|
* ```
|
|
189
189
|
*/
|
|
190
|
-
|
|
190
|
+
function varp(data: Array<number>): number;
|
|
191
191
|
/**
|
|
192
192
|
* Compute the variance of a **sample**.
|
|
193
193
|
* @param data An array of numeric inputs
|
|
@@ -197,7 +197,7 @@ export declare abstract class SMath {
|
|
|
197
197
|
* const y = SMath.vars([1, 2, 4, 4]); // 2.25
|
|
198
198
|
* ```
|
|
199
199
|
*/
|
|
200
|
-
|
|
200
|
+
function vars(data: Array<number>): number;
|
|
201
201
|
/**
|
|
202
202
|
* Compute the standard deviation of a **complete population**.
|
|
203
203
|
* @param data An array of numeric inputs
|
|
@@ -207,7 +207,7 @@ export declare abstract class SMath {
|
|
|
207
207
|
* const y = SMath.stdevp([1, 2, 3, 4]); // 1.118...
|
|
208
208
|
* ```
|
|
209
209
|
*/
|
|
210
|
-
|
|
210
|
+
function stdevp(data: Array<number>): number;
|
|
211
211
|
/**
|
|
212
212
|
* Compute the standard deviation of a **sample**.
|
|
213
213
|
* @param data An array of numeric inputs
|
|
@@ -217,7 +217,7 @@ export declare abstract class SMath {
|
|
|
217
217
|
* const y = SMath.stdevs([1, 2, 3, 4]); // 1.29...
|
|
218
218
|
* ```
|
|
219
219
|
*/
|
|
220
|
-
|
|
220
|
+
function stdevs(data: Array<number>): number;
|
|
221
221
|
/**
|
|
222
222
|
* Generate a uniformly-distributed floating-point number within the range.
|
|
223
223
|
* @param min The minimum bound
|
|
@@ -228,7 +228,7 @@ export declare abstract class SMath {
|
|
|
228
228
|
* const y = SMath.runif(-2, 2); // 0.376...
|
|
229
229
|
* ```
|
|
230
230
|
*/
|
|
231
|
-
|
|
231
|
+
function runif(min: number, max: number): number;
|
|
232
232
|
/**
|
|
233
233
|
* Generate a uniformly-distributed integer within the range.
|
|
234
234
|
* @param min The minimum bound (inclusive)
|
|
@@ -239,7 +239,7 @@ export declare abstract class SMath {
|
|
|
239
239
|
* const y = SMath.rint(-4, 3); // -4
|
|
240
240
|
* ```
|
|
241
241
|
*/
|
|
242
|
-
|
|
242
|
+
function rint(min: number, max: number): number;
|
|
243
243
|
/**
|
|
244
244
|
* Generate a normally-distributed floating-point number.
|
|
245
245
|
* @param mean The mean of the population distribution
|
|
@@ -250,7 +250,7 @@ export declare abstract class SMath {
|
|
|
250
250
|
* const y = SMath.rnorm(2, 3); // 1.627...
|
|
251
251
|
* ```
|
|
252
252
|
*/
|
|
253
|
-
|
|
253
|
+
function rnorm(mean?: number, stdev?: number): number;
|
|
254
254
|
/**
|
|
255
255
|
* Generate a population of normally-distributed floating-point numbers.
|
|
256
256
|
* @param count The number of values to generate
|
|
@@ -262,7 +262,7 @@ export declare abstract class SMath {
|
|
|
262
262
|
* const dataset = SMath.rdist(3); // [ 1.051..., -0.779..., -2.254... ]
|
|
263
263
|
* ```
|
|
264
264
|
*/
|
|
265
|
-
|
|
265
|
+
function rdist(count: number, mean?: number, stdev?: number): Array<number>;
|
|
266
266
|
/**
|
|
267
267
|
* Take the limit of a function. A return value of `NaN` indicates
|
|
268
268
|
* that no limit exists either due to a discontinuity or imaginary value.
|
|
@@ -276,7 +276,7 @@ export declare abstract class SMath {
|
|
|
276
276
|
* const y = SMath.lim(Math.log, 0); // -Infinity
|
|
277
277
|
* ```
|
|
278
278
|
*/
|
|
279
|
-
|
|
279
|
+
function lim(f: (x: number) => number, x: number, h?: number, discontinuity_cutoff?: number): number;
|
|
280
280
|
/**
|
|
281
281
|
* Take the derivative of a function.
|
|
282
282
|
* @param f Function `f(x)`
|
|
@@ -288,7 +288,7 @@ export declare abstract class SMath {
|
|
|
288
288
|
* const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
|
|
289
289
|
* ```
|
|
290
290
|
*/
|
|
291
|
-
|
|
291
|
+
function differentiate(f: (x: number) => number, x: number, h?: number): number;
|
|
292
292
|
/**
|
|
293
293
|
* Compute the definite integral of a function.
|
|
294
294
|
* @param f Function `f(x)`
|
|
@@ -301,5 +301,5 @@ export declare abstract class SMath {
|
|
|
301
301
|
* const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
|
|
302
302
|
* ```
|
|
303
303
|
*/
|
|
304
|
-
|
|
304
|
+
function integrate(f: (x: number) => number, a: number, b: number, Ndx?: number): number;
|
|
305
305
|
}
|