smath 1.13.2 → 1.14.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/dist/bin.js +1 -5
- package/dist/index.js +30 -100
- package/package.json +2 -2
- package/types/index.d.ts +2 -71
package/dist/bin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
var _a, _b, _c, _d
|
|
3
|
+
var _a, _b, _c, _d;
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
var SMath = require(".");
|
|
6
6
|
var func = ((_a = process.argv[2]) !== null && _a !== void 0 ? _a : '').toLowerCase(), nums = process.argv.slice(3).map(function (arg, i) {
|
|
@@ -147,10 +147,6 @@ switch (func) {
|
|
|
147
147
|
console.log(SMath.mixed(nums[0], (_d = nums[1]) !== null && _d !== void 0 ? _d : 1e-6));
|
|
148
148
|
break;
|
|
149
149
|
}
|
|
150
|
-
case ('tohex'): {
|
|
151
|
-
console.log(SMath.toHex(nums[0], (_e = nums[1]) !== null && _e !== void 0 ? _e : 0));
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
150
|
case (''): {
|
|
155
151
|
console.error('Missing argument. Use with "help" for a list of commands.');
|
|
156
152
|
process.exit(1);
|
package/dist/index.js
CHANGED
|
@@ -40,7 +40,6 @@ exports.differentiate = differentiate;
|
|
|
40
40
|
exports.integrate = integrate;
|
|
41
41
|
exports.rat = rat;
|
|
42
42
|
exports.mixed = mixed;
|
|
43
|
-
exports.toHex = toHex;
|
|
44
43
|
/**
|
|
45
44
|
* @packageDocumentation
|
|
46
45
|
* Small math function library
|
|
@@ -55,10 +54,8 @@ exports.toHex = toHex;
|
|
|
55
54
|
* @param epsilon Maximum absolute error
|
|
56
55
|
* @returns True if `a` is approximately `b`
|
|
57
56
|
* @example
|
|
58
|
-
* ```js
|
|
59
57
|
* const b1 = SMath.approx(1 / 3, 0.33, 1e-6), // false
|
|
60
58
|
* b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
|
|
61
|
-
* ```
|
|
62
59
|
*/
|
|
63
60
|
function approx(a, b, epsilon) {
|
|
64
61
|
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
@@ -71,10 +68,8 @@ function approx(a, b, epsilon) {
|
|
|
71
68
|
* @param max The maximum value of the range
|
|
72
69
|
* @returns A clamped number
|
|
73
70
|
* @example
|
|
74
|
-
* ```js
|
|
75
71
|
* const n1 = SMath.clamp(5, 0, 10), // 5
|
|
76
72
|
* n2 = SMath.clamp(-2, 0, 10); // 0
|
|
77
|
-
* ```
|
|
78
73
|
*/
|
|
79
74
|
function clamp(n, min, max) {
|
|
80
75
|
if (n < min) {
|
|
@@ -92,9 +87,7 @@ function clamp(n, min, max) {
|
|
|
92
87
|
* @param max The maximum value in the range
|
|
93
88
|
* @returns A normalized value
|
|
94
89
|
* @example
|
|
95
|
-
* ```js
|
|
96
90
|
* const y = SMath.normalize(18, 9, 99); // 0.1
|
|
97
|
-
* ```
|
|
98
91
|
*/
|
|
99
92
|
function normalize(n, min, max) {
|
|
100
93
|
if (min === max) {
|
|
@@ -109,9 +102,7 @@ function normalize(n, min, max) {
|
|
|
109
102
|
* @param max The maximum value in the range
|
|
110
103
|
* @returns A value within the number range
|
|
111
104
|
* @example
|
|
112
|
-
* ```js
|
|
113
105
|
* const y = SMath.expand(0.25, 4, 6); // 4.5
|
|
114
|
-
* ```
|
|
115
106
|
*/
|
|
116
107
|
function expand(n, min, max) {
|
|
117
108
|
return (max - min) * n + min;
|
|
@@ -125,10 +116,8 @@ function expand(n, min, max) {
|
|
|
125
116
|
* @param max2 The maximum value for the final range
|
|
126
117
|
* @returns A translated number in the final range
|
|
127
118
|
* @example
|
|
128
|
-
* ```js
|
|
129
119
|
* const C = 20,
|
|
130
120
|
* F = SMath.translate(C, 0, 100, 32, 212); // 68
|
|
131
|
-
* ```
|
|
132
121
|
*/
|
|
133
122
|
function translate(n, min1, max1, min2, max2) {
|
|
134
123
|
return expand(normalize(n, min1, max1), min2, max2);
|
|
@@ -140,10 +129,8 @@ function translate(n, min1, max1, min2, max2) {
|
|
|
140
129
|
* @param count The number of values in the space
|
|
141
130
|
* @returns The linear space as an array of numbers
|
|
142
131
|
* @example
|
|
143
|
-
* ```js
|
|
144
132
|
* const space = SMath.linspace(1, 5, 6);
|
|
145
133
|
* // [ 1, 1.8, 2.6, 3.4, 4.2, 5 ]
|
|
146
|
-
* ```
|
|
147
134
|
*/
|
|
148
135
|
function linspace(min, max, count) {
|
|
149
136
|
var space = [];
|
|
@@ -159,10 +146,8 @@ function linspace(min, max, count) {
|
|
|
159
146
|
* @param count The number of values in the space
|
|
160
147
|
* @returns The logarithmic space as an array of numbers
|
|
161
148
|
* @example
|
|
162
|
-
* ```js
|
|
163
149
|
* const space = SMath.logspace(0, 2, 5);
|
|
164
150
|
* // [ 1, 3.2, 10, 31.6, 100 ]
|
|
165
|
-
* ```
|
|
166
151
|
*/
|
|
167
152
|
function logspace(min, max, count) {
|
|
168
153
|
return linspace(min, max, count).map(function (n) { return Math.pow(10, n); });
|
|
@@ -172,9 +157,7 @@ function logspace(min, max, count) {
|
|
|
172
157
|
* @param n Any positive integer
|
|
173
158
|
* @returns `n!`
|
|
174
159
|
* @example
|
|
175
|
-
* ```js
|
|
176
160
|
* const y = SMath.factorial(5); // 120
|
|
177
|
-
* ```
|
|
178
161
|
*/
|
|
179
162
|
function factorial(n) {
|
|
180
163
|
if (n < 0 || (n | 0) !== n) {
|
|
@@ -195,9 +178,7 @@ function factorial(n) {
|
|
|
195
178
|
* @param n Any positive integer
|
|
196
179
|
* @returns The array of prime factors
|
|
197
180
|
* @example
|
|
198
|
-
* ```js
|
|
199
181
|
* const y = SMath.factors(12); // [ 2, 2, 3 ]
|
|
200
|
-
* ```
|
|
201
182
|
*/
|
|
202
183
|
function factors(n) {
|
|
203
184
|
if (n < 0 || (n | 0) !== n) {
|
|
@@ -226,9 +207,7 @@ function factors(n) {
|
|
|
226
207
|
* @param base Any base to round to
|
|
227
208
|
* @returns `n` rounded to the nearest multiple of `base`
|
|
228
209
|
* @example
|
|
229
|
-
* ```js
|
|
230
210
|
* const y = SMath.round2(Math.PI, 0.2); // 3.2
|
|
231
|
-
* ```
|
|
232
211
|
*/
|
|
233
212
|
function round2(n, base) {
|
|
234
213
|
var rounded = base ? base * Math.round(n / base) : n;
|
|
@@ -246,9 +225,7 @@ function round2(n, base) {
|
|
|
246
225
|
* @param actual The accepted or theoretical value
|
|
247
226
|
* @returns The relative (normalized) error
|
|
248
227
|
* @example
|
|
249
|
-
* ```js
|
|
250
228
|
* const e = SMath.error(22.5, 25); // -0.1
|
|
251
|
-
* ```
|
|
252
229
|
*/
|
|
253
230
|
function error(experimental, actual) {
|
|
254
231
|
return (experimental - actual) / actual;
|
|
@@ -259,9 +236,7 @@ function error(experimental, actual) {
|
|
|
259
236
|
* @param data An array of numeric inputs
|
|
260
237
|
* @returns The sum total
|
|
261
238
|
* @example
|
|
262
|
-
* ```js
|
|
263
239
|
* const y = SMath.sum([1, 2, 3]); // 6
|
|
264
|
-
* ```
|
|
265
240
|
*/
|
|
266
241
|
function sum(data) {
|
|
267
242
|
return data.reduce(function (a, b) { return a + b; }, 0);
|
|
@@ -272,9 +247,7 @@ function sum(data) {
|
|
|
272
247
|
* @param data An array of numeric inputs
|
|
273
248
|
* @returns The product
|
|
274
249
|
* @example
|
|
275
|
-
* ```js
|
|
276
250
|
* const y = SMath.prod([2, 2, 3, 5]); // 60
|
|
277
|
-
* ```
|
|
278
251
|
*/
|
|
279
252
|
function prod(data) {
|
|
280
253
|
return data.reduce(function (a, b) { return a * b; }, 1);
|
|
@@ -284,9 +257,7 @@ function prod(data) {
|
|
|
284
257
|
* @param data An array of numeric inputs
|
|
285
258
|
* @returns The average, or mean
|
|
286
259
|
* @example
|
|
287
|
-
* ```js
|
|
288
260
|
* const y = SMath.avg([1, 2, 4, 4]); // 2.75
|
|
289
|
-
* ```
|
|
290
261
|
*/
|
|
291
262
|
function avg(data) {
|
|
292
263
|
return sum(data) / data.length;
|
|
@@ -296,9 +267,7 @@ function avg(data) {
|
|
|
296
267
|
* @param data An array of numeric inputs
|
|
297
268
|
* @returns The median of the dataset
|
|
298
269
|
* @example
|
|
299
|
-
* ```js
|
|
300
270
|
* const y = SMath.median([2, 5, 3, 1]); // 2.5
|
|
301
|
-
* ```
|
|
302
271
|
*/
|
|
303
272
|
function median(data) {
|
|
304
273
|
data.sort(function (a, b) { return a - b; });
|
|
@@ -312,9 +281,7 @@ function median(data) {
|
|
|
312
281
|
* @param data An array of numeric inputs
|
|
313
282
|
* @returns The population variance
|
|
314
283
|
* @example
|
|
315
|
-
* ```js
|
|
316
284
|
* const y = SMath.varp([1, 2, 4, 4]); // 1.6875
|
|
317
|
-
* ```
|
|
318
285
|
*/
|
|
319
286
|
function varp(data) {
|
|
320
287
|
var mean = avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
|
|
@@ -325,9 +292,7 @@ function varp(data) {
|
|
|
325
292
|
* @param data An array of numeric inputs
|
|
326
293
|
* @returns The sample variance
|
|
327
294
|
* @example
|
|
328
|
-
* ```js
|
|
329
295
|
* const y = SMath.vars([1, 2, 4, 4]); // 2.25
|
|
330
|
-
* ```
|
|
331
296
|
*/
|
|
332
297
|
function vars(data) {
|
|
333
298
|
var mean = avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
|
|
@@ -338,9 +303,7 @@ function vars(data) {
|
|
|
338
303
|
* @param data An array of numeric inputs
|
|
339
304
|
* @returns The population standard deviation
|
|
340
305
|
* @example
|
|
341
|
-
* ```js
|
|
342
306
|
* const y = SMath.stdevp([1, 2, 3, 4]); // 1.118...
|
|
343
|
-
* ```
|
|
344
307
|
*/
|
|
345
308
|
function stdevp(data) {
|
|
346
309
|
return Math.sqrt(varp(data));
|
|
@@ -350,9 +313,7 @@ function stdevp(data) {
|
|
|
350
313
|
* @param data An array of numeric inputs
|
|
351
314
|
* @returns The sample standard deviation
|
|
352
315
|
* @example
|
|
353
|
-
* ```js
|
|
354
316
|
* const y = SMath.stdevs([1, 2, 3, 4]); // 1.29...
|
|
355
|
-
* ```
|
|
356
317
|
*/
|
|
357
318
|
function stdevs(data) {
|
|
358
319
|
return Math.sqrt(vars(data));
|
|
@@ -363,9 +324,7 @@ function stdevs(data) {
|
|
|
363
324
|
* @param max The maximum bound
|
|
364
325
|
* @returns A random float within the range
|
|
365
326
|
* @example
|
|
366
|
-
* ```js
|
|
367
327
|
* const y = SMath.runif(-2, 2); // 0.376...
|
|
368
|
-
* ```
|
|
369
328
|
*/
|
|
370
329
|
function runif(min, max) {
|
|
371
330
|
return expand(Math.random(), min, max);
|
|
@@ -376,9 +335,7 @@ function runif(min, max) {
|
|
|
376
335
|
* @param max The maximum bound (inclusive)
|
|
377
336
|
* @returns A random integer within the range
|
|
378
337
|
* @example
|
|
379
|
-
* ```js
|
|
380
338
|
* const y = SMath.rint(-4, 3); // -4
|
|
381
|
-
* ```
|
|
382
339
|
*/
|
|
383
340
|
function rint(min, max) {
|
|
384
341
|
min |= 0;
|
|
@@ -397,9 +354,7 @@ function rint(min, max) {
|
|
|
397
354
|
* @param stdev The standard deviation of the population
|
|
398
355
|
* @returns A random float
|
|
399
356
|
* @example
|
|
400
|
-
* ```js
|
|
401
357
|
* const y = SMath.rnorm(2, 3); // 1.627...
|
|
402
|
-
* ```
|
|
403
358
|
*/
|
|
404
359
|
function rnorm(mean, stdev) {
|
|
405
360
|
if (mean === void 0) { mean = 0; }
|
|
@@ -413,9 +368,7 @@ function rnorm(mean, stdev) {
|
|
|
413
368
|
* @param stdev The standard deviation of the population
|
|
414
369
|
* @returns A population of random floats
|
|
415
370
|
* @example
|
|
416
|
-
* ```js
|
|
417
371
|
* const dataset = SMath.rdist(3); // [ 1.051..., -0.779..., -2.254... ]
|
|
418
|
-
* ```
|
|
419
372
|
*/
|
|
420
373
|
function rdist(count, mean, stdev) {
|
|
421
374
|
if (mean === void 0) { mean = 0; }
|
|
@@ -431,9 +384,7 @@ function rdist(count, mean, stdev) {
|
|
|
431
384
|
* @param stack An array of arbitrary elements
|
|
432
385
|
* @returns The `stack` array in a random order
|
|
433
386
|
* @example
|
|
434
|
-
* ```js
|
|
435
387
|
* const shuffled = SMath.shuffle(['a', 'b', 'c']); // [ 'c', 'a', 'b' ]
|
|
436
|
-
* ```
|
|
437
388
|
*/
|
|
438
389
|
function shuffle(stack) {
|
|
439
390
|
var rawData = [];
|
|
@@ -452,13 +403,11 @@ function shuffle(stack) {
|
|
|
452
403
|
* @param discontinuity_cutoff The discontinuity cutoff
|
|
453
404
|
* @returns `lim(f(x->x))`
|
|
454
405
|
* @example
|
|
455
|
-
* ```js
|
|
456
406
|
* const y = SMath.lim(Math.log, 0); // -Infinity
|
|
457
|
-
* ```
|
|
458
407
|
*/
|
|
459
408
|
function lim(f, x, h, discontinuity_cutoff) {
|
|
460
|
-
if (h === void 0) { h = 1e-
|
|
461
|
-
if (discontinuity_cutoff === void 0) { discontinuity_cutoff =
|
|
409
|
+
if (h === void 0) { h = 1e-6; }
|
|
410
|
+
if (discontinuity_cutoff === void 0) { discontinuity_cutoff = 1e-3; }
|
|
462
411
|
var center = f(x), left1 = f(x - h), left2 = f(x - h / 2), right1 = f(x + h), right2 = f(x + h / 2);
|
|
463
412
|
var left, right;
|
|
464
413
|
if (Number.isFinite(center)) {
|
|
@@ -466,46 +415,46 @@ function lim(f, x, h, discontinuity_cutoff) {
|
|
|
466
415
|
}
|
|
467
416
|
// Check the limit approaching from the left
|
|
468
417
|
if (Number.isFinite(left1) && Number.isFinite(left2)) {
|
|
469
|
-
if (
|
|
470
|
-
left =
|
|
418
|
+
if (approx(left1, left2, discontinuity_cutoff)) {
|
|
419
|
+
left = left2; // Converges
|
|
471
420
|
}
|
|
472
|
-
else if (
|
|
473
|
-
left =
|
|
421
|
+
else if (left1 > 0 && left2 > left1) {
|
|
422
|
+
left = Infinity; // Diverges to +inf
|
|
423
|
+
}
|
|
424
|
+
else if (left1 < 0 && left2 < left1) {
|
|
425
|
+
left = -Infinity; // Diverges to -inf
|
|
474
426
|
}
|
|
475
427
|
else {
|
|
476
|
-
left =
|
|
428
|
+
left = NaN; // Diverges
|
|
477
429
|
}
|
|
478
430
|
}
|
|
479
|
-
else if (left1 === left2) { // Handles +/-Infinity case
|
|
480
|
-
left = left1;
|
|
481
|
-
}
|
|
482
431
|
else {
|
|
483
|
-
left = NaN;
|
|
432
|
+
left = NaN; // Discontinuous
|
|
484
433
|
}
|
|
485
434
|
// Check the limit approaching from the right
|
|
486
435
|
if (Number.isFinite(right1) && Number.isFinite(right2)) {
|
|
487
|
-
if (
|
|
488
|
-
right =
|
|
436
|
+
if (approx(right1, right2, discontinuity_cutoff)) {
|
|
437
|
+
right = right2; // Converges
|
|
489
438
|
}
|
|
490
|
-
else if (
|
|
491
|
-
right =
|
|
439
|
+
else if (right1 > 0 && right2 > right1) {
|
|
440
|
+
right = Infinity; // Diverges to +inf
|
|
441
|
+
}
|
|
442
|
+
else if (right1 < 0 && right2 < right1) {
|
|
443
|
+
right = -Infinity; // Diverges to -inf
|
|
492
444
|
}
|
|
493
445
|
else {
|
|
494
|
-
right =
|
|
446
|
+
right = NaN; // Diverges
|
|
495
447
|
}
|
|
496
448
|
}
|
|
497
|
-
else if (right1 === right2) { // Handles +/-Infinity case
|
|
498
|
-
right = right1;
|
|
499
|
-
}
|
|
500
449
|
else {
|
|
501
|
-
right = NaN;
|
|
450
|
+
right = NaN; // Discontinuous
|
|
502
451
|
}
|
|
503
452
|
// Check if limits match or are close
|
|
504
453
|
if (left === right) { // Handles +/-Infinity case
|
|
505
454
|
return left;
|
|
506
455
|
}
|
|
507
|
-
else if (
|
|
508
|
-
return
|
|
456
|
+
else if (Number.isNaN(left) && Number.isNaN(right)) {
|
|
457
|
+
return center;
|
|
509
458
|
}
|
|
510
459
|
else if (!Number.isNaN(left) && Number.isNaN(right)) {
|
|
511
460
|
return left;
|
|
@@ -513,6 +462,9 @@ function lim(f, x, h, discontinuity_cutoff) {
|
|
|
513
462
|
else if (Number.isNaN(left) && !Number.isNaN(right)) {
|
|
514
463
|
return right;
|
|
515
464
|
}
|
|
465
|
+
else if (approx(left, right, discontinuity_cutoff)) {
|
|
466
|
+
return avg([left, right]);
|
|
467
|
+
}
|
|
516
468
|
else {
|
|
517
469
|
return NaN;
|
|
518
470
|
}
|
|
@@ -521,16 +473,14 @@ function lim(f, x, h, discontinuity_cutoff) {
|
|
|
521
473
|
* Take the derivative of a function.
|
|
522
474
|
* @param f Function `f(x)`
|
|
523
475
|
* @param x The x-value where to evaluate the derivative
|
|
524
|
-
* @param
|
|
476
|
+
* @param epsilon Small step value
|
|
525
477
|
* @returns `f'(x)`
|
|
526
478
|
* @example
|
|
527
|
-
* ```js
|
|
528
479
|
* const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
|
|
529
|
-
* ```
|
|
530
480
|
*/
|
|
531
|
-
function differentiate(f, x,
|
|
532
|
-
if (
|
|
533
|
-
return (f(x + h) - f(x - h)) / (2 * h);
|
|
481
|
+
function differentiate(f, x, epsilon) {
|
|
482
|
+
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
483
|
+
return lim(function (h) { return (f(x + h) - f(x - h)) / (2 * h); }, 0, epsilon);
|
|
534
484
|
}
|
|
535
485
|
/**
|
|
536
486
|
* Compute the definite integral of a function.
|
|
@@ -540,12 +490,10 @@ function differentiate(f, x, h) {
|
|
|
540
490
|
* @param Ndx The number of rectangles to compute
|
|
541
491
|
* @returns `F(b)-F(a)`
|
|
542
492
|
* @example
|
|
543
|
-
* ```js
|
|
544
493
|
* const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
|
|
545
|
-
* ```
|
|
546
494
|
*/
|
|
547
495
|
function integrate(f, a, b, Ndx) {
|
|
548
|
-
if (Ndx === void 0) { Ndx =
|
|
496
|
+
if (Ndx === void 0) { Ndx = 1e6; }
|
|
549
497
|
return ((b - a) / Ndx) * sum(linspace(a, b, Ndx).map(function (x) { return f(x); }));
|
|
550
498
|
}
|
|
551
499
|
/**
|
|
@@ -555,9 +503,7 @@ function integrate(f, a, b, Ndx) {
|
|
|
555
503
|
* @param epsilon Maximum absolute error
|
|
556
504
|
* @returns An object containing the fraction's numerator and denominator
|
|
557
505
|
* @example
|
|
558
|
-
* ```js
|
|
559
506
|
* const frac = SMath.rat(0.625); // { num: 5, den: 8 }
|
|
560
|
-
* ```
|
|
561
507
|
*/
|
|
562
508
|
function rat(n, epsilon) {
|
|
563
509
|
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
@@ -581,25 +527,9 @@ function rat(n, epsilon) {
|
|
|
581
527
|
* @param epsilon Maximum absolute error
|
|
582
528
|
* @returns An object containing the whole part and fraction numerator and denominator
|
|
583
529
|
* @example
|
|
584
|
-
* ```js
|
|
585
530
|
* const frac = SMath.mixed(-8 / 6); // { whole: -1, num: 1, den: 3 }
|
|
586
|
-
* ```
|
|
587
531
|
*/
|
|
588
532
|
function mixed(n, epsilon) {
|
|
589
533
|
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
590
534
|
return __assign({ whole: n | 0 }, rat(n < -1 ? (n | 0) - n : n - (n | 0), epsilon));
|
|
591
535
|
}
|
|
592
|
-
/**
|
|
593
|
-
* Convert any number to its hexadecimal equivalent.
|
|
594
|
-
* @param n A decimal number to convert
|
|
595
|
-
* @param length The minimum number of digits to show
|
|
596
|
-
* @returns The number `n` converted to hexadecimal
|
|
597
|
-
* @example
|
|
598
|
-
* ```js
|
|
599
|
-
* const hex = SMath.toHex(10, 2); // '0A'
|
|
600
|
-
* ```
|
|
601
|
-
*/
|
|
602
|
-
function toHex(n, length) {
|
|
603
|
-
if (length === void 0) { length = 0; }
|
|
604
|
-
return (n < 0 ? '-' : '') + (n < 0 ? -n : n).toString(16).padStart(length, '0').toUpperCase();
|
|
605
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smath",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.0",
|
|
4
4
|
"description": "Small math function library",
|
|
5
5
|
"homepage": "https://npm.nicfv.com/",
|
|
6
6
|
"bin": "dist/bin.js",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"repository": "github:nicfv/npm",
|
|
53
53
|
"license": "MIT",
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@types/node": "
|
|
55
|
+
"@types/node": "25.0.10",
|
|
56
56
|
"t6": "1.2.1"
|
|
57
57
|
}
|
|
58
58
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -12,10 +12,8 @@
|
|
|
12
12
|
* @param epsilon Maximum absolute error
|
|
13
13
|
* @returns True if `a` is approximately `b`
|
|
14
14
|
* @example
|
|
15
|
-
* ```js
|
|
16
15
|
* const b1 = SMath.approx(1 / 3, 0.33, 1e-6), // false
|
|
17
16
|
* b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
|
|
18
|
-
* ```
|
|
19
17
|
*/
|
|
20
18
|
export declare function approx(a: number, b: number, epsilon?: number): boolean;
|
|
21
19
|
/**
|
|
@@ -25,10 +23,8 @@ export declare function approx(a: number, b: number, epsilon?: number): boolean;
|
|
|
25
23
|
* @param max The maximum value of the range
|
|
26
24
|
* @returns A clamped number
|
|
27
25
|
* @example
|
|
28
|
-
* ```js
|
|
29
26
|
* const n1 = SMath.clamp(5, 0, 10), // 5
|
|
30
27
|
* n2 = SMath.clamp(-2, 0, 10); // 0
|
|
31
|
-
* ```
|
|
32
28
|
*/
|
|
33
29
|
export declare function clamp(n: number, min: number, max: number): number;
|
|
34
30
|
/**
|
|
@@ -38,9 +34,7 @@ export declare function clamp(n: number, min: number, max: number): number;
|
|
|
38
34
|
* @param max The maximum value in the range
|
|
39
35
|
* @returns A normalized value
|
|
40
36
|
* @example
|
|
41
|
-
* ```js
|
|
42
37
|
* const y = SMath.normalize(18, 9, 99); // 0.1
|
|
43
|
-
* ```
|
|
44
38
|
*/
|
|
45
39
|
export declare function normalize(n: number, min: number, max: number): number;
|
|
46
40
|
/**
|
|
@@ -50,9 +44,7 @@ export declare function normalize(n: number, min: number, max: number): number;
|
|
|
50
44
|
* @param max The maximum value in the range
|
|
51
45
|
* @returns A value within the number range
|
|
52
46
|
* @example
|
|
53
|
-
* ```js
|
|
54
47
|
* const y = SMath.expand(0.25, 4, 6); // 4.5
|
|
55
|
-
* ```
|
|
56
48
|
*/
|
|
57
49
|
export declare function expand(n: number, min: number, max: number): number;
|
|
58
50
|
/**
|
|
@@ -64,10 +56,8 @@ export declare function expand(n: number, min: number, max: number): number;
|
|
|
64
56
|
* @param max2 The maximum value for the final range
|
|
65
57
|
* @returns A translated number in the final range
|
|
66
58
|
* @example
|
|
67
|
-
* ```js
|
|
68
59
|
* const C = 20,
|
|
69
60
|
* F = SMath.translate(C, 0, 100, 32, 212); // 68
|
|
70
|
-
* ```
|
|
71
61
|
*/
|
|
72
62
|
export declare function translate(n: number, min1: number, max1: number, min2: number, max2: number): number;
|
|
73
63
|
/**
|
|
@@ -77,10 +67,8 @@ export declare function translate(n: number, min1: number, max1: number, min2: n
|
|
|
77
67
|
* @param count The number of values in the space
|
|
78
68
|
* @returns The linear space as an array of numbers
|
|
79
69
|
* @example
|
|
80
|
-
* ```js
|
|
81
70
|
* const space = SMath.linspace(1, 5, 6);
|
|
82
71
|
* // [ 1, 1.8, 2.6, 3.4, 4.2, 5 ]
|
|
83
|
-
* ```
|
|
84
72
|
*/
|
|
85
73
|
export declare function linspace(min: number, max: number, count: number): number[];
|
|
86
74
|
/**
|
|
@@ -90,10 +78,8 @@ export declare function linspace(min: number, max: number, count: number): numbe
|
|
|
90
78
|
* @param count The number of values in the space
|
|
91
79
|
* @returns The logarithmic space as an array of numbers
|
|
92
80
|
* @example
|
|
93
|
-
* ```js
|
|
94
81
|
* const space = SMath.logspace(0, 2, 5);
|
|
95
82
|
* // [ 1, 3.2, 10, 31.6, 100 ]
|
|
96
|
-
* ```
|
|
97
83
|
*/
|
|
98
84
|
export declare function logspace(min: number, max: number, count: number): number[];
|
|
99
85
|
/**
|
|
@@ -101,9 +87,7 @@ export declare function logspace(min: number, max: number, count: number): numbe
|
|
|
101
87
|
* @param n Any positive integer
|
|
102
88
|
* @returns `n!`
|
|
103
89
|
* @example
|
|
104
|
-
* ```js
|
|
105
90
|
* const y = SMath.factorial(5); // 120
|
|
106
|
-
* ```
|
|
107
91
|
*/
|
|
108
92
|
export declare function factorial(n: number): number;
|
|
109
93
|
/**
|
|
@@ -111,9 +95,7 @@ export declare function factorial(n: number): number;
|
|
|
111
95
|
* @param n Any positive integer
|
|
112
96
|
* @returns The array of prime factors
|
|
113
97
|
* @example
|
|
114
|
-
* ```js
|
|
115
98
|
* const y = SMath.factors(12); // [ 2, 2, 3 ]
|
|
116
|
-
* ```
|
|
117
99
|
*/
|
|
118
100
|
export declare function factors(n: number): number[];
|
|
119
101
|
/**
|
|
@@ -123,9 +105,7 @@ export declare function factors(n: number): number[];
|
|
|
123
105
|
* @param base Any base to round to
|
|
124
106
|
* @returns `n` rounded to the nearest multiple of `base`
|
|
125
107
|
* @example
|
|
126
|
-
* ```js
|
|
127
108
|
* const y = SMath.round2(Math.PI, 0.2); // 3.2
|
|
128
|
-
* ```
|
|
129
109
|
*/
|
|
130
110
|
export declare function round2(n: number, base: number): number;
|
|
131
111
|
/**
|
|
@@ -139,9 +119,7 @@ export declare function round2(n: number, base: number): number;
|
|
|
139
119
|
* @param actual The accepted or theoretical value
|
|
140
120
|
* @returns The relative (normalized) error
|
|
141
121
|
* @example
|
|
142
|
-
* ```js
|
|
143
122
|
* const e = SMath.error(22.5, 25); // -0.1
|
|
144
|
-
* ```
|
|
145
123
|
*/
|
|
146
124
|
export declare function error(experimental: number, actual: number): number;
|
|
147
125
|
/**
|
|
@@ -150,9 +128,7 @@ export declare function error(experimental: number, actual: number): number;
|
|
|
150
128
|
* @param data An array of numeric inputs
|
|
151
129
|
* @returns The sum total
|
|
152
130
|
* @example
|
|
153
|
-
* ```js
|
|
154
131
|
* const y = SMath.sum([1, 2, 3]); // 6
|
|
155
|
-
* ```
|
|
156
132
|
*/
|
|
157
133
|
export declare function sum(data: number[]): number;
|
|
158
134
|
/**
|
|
@@ -161,9 +137,7 @@ export declare function sum(data: number[]): number;
|
|
|
161
137
|
* @param data An array of numeric inputs
|
|
162
138
|
* @returns The product
|
|
163
139
|
* @example
|
|
164
|
-
* ```js
|
|
165
140
|
* const y = SMath.prod([2, 2, 3, 5]); // 60
|
|
166
|
-
* ```
|
|
167
141
|
*/
|
|
168
142
|
export declare function prod(data: number[]): number;
|
|
169
143
|
/**
|
|
@@ -171,9 +145,7 @@ export declare function prod(data: number[]): number;
|
|
|
171
145
|
* @param data An array of numeric inputs
|
|
172
146
|
* @returns The average, or mean
|
|
173
147
|
* @example
|
|
174
|
-
* ```js
|
|
175
148
|
* const y = SMath.avg([1, 2, 4, 4]); // 2.75
|
|
176
|
-
* ```
|
|
177
149
|
*/
|
|
178
150
|
export declare function avg(data: number[]): number;
|
|
179
151
|
/**
|
|
@@ -181,9 +153,7 @@ export declare function avg(data: number[]): number;
|
|
|
181
153
|
* @param data An array of numeric inputs
|
|
182
154
|
* @returns The median of the dataset
|
|
183
155
|
* @example
|
|
184
|
-
* ```js
|
|
185
156
|
* const y = SMath.median([2, 5, 3, 1]); // 2.5
|
|
186
|
-
* ```
|
|
187
157
|
*/
|
|
188
158
|
export declare function median(data: number[]): number;
|
|
189
159
|
/**
|
|
@@ -191,9 +161,7 @@ export declare function median(data: number[]): number;
|
|
|
191
161
|
* @param data An array of numeric inputs
|
|
192
162
|
* @returns The population variance
|
|
193
163
|
* @example
|
|
194
|
-
* ```js
|
|
195
164
|
* const y = SMath.varp([1, 2, 4, 4]); // 1.6875
|
|
196
|
-
* ```
|
|
197
165
|
*/
|
|
198
166
|
export declare function varp(data: number[]): number;
|
|
199
167
|
/**
|
|
@@ -201,9 +169,7 @@ export declare function varp(data: number[]): number;
|
|
|
201
169
|
* @param data An array of numeric inputs
|
|
202
170
|
* @returns The sample variance
|
|
203
171
|
* @example
|
|
204
|
-
* ```js
|
|
205
172
|
* const y = SMath.vars([1, 2, 4, 4]); // 2.25
|
|
206
|
-
* ```
|
|
207
173
|
*/
|
|
208
174
|
export declare function vars(data: number[]): number;
|
|
209
175
|
/**
|
|
@@ -211,9 +177,7 @@ export declare function vars(data: number[]): number;
|
|
|
211
177
|
* @param data An array of numeric inputs
|
|
212
178
|
* @returns The population standard deviation
|
|
213
179
|
* @example
|
|
214
|
-
* ```js
|
|
215
180
|
* const y = SMath.stdevp([1, 2, 3, 4]); // 1.118...
|
|
216
|
-
* ```
|
|
217
181
|
*/
|
|
218
182
|
export declare function stdevp(data: number[]): number;
|
|
219
183
|
/**
|
|
@@ -221,9 +185,7 @@ export declare function stdevp(data: number[]): number;
|
|
|
221
185
|
* @param data An array of numeric inputs
|
|
222
186
|
* @returns The sample standard deviation
|
|
223
187
|
* @example
|
|
224
|
-
* ```js
|
|
225
188
|
* const y = SMath.stdevs([1, 2, 3, 4]); // 1.29...
|
|
226
|
-
* ```
|
|
227
189
|
*/
|
|
228
190
|
export declare function stdevs(data: number[]): number;
|
|
229
191
|
/**
|
|
@@ -232,9 +194,7 @@ export declare function stdevs(data: number[]): number;
|
|
|
232
194
|
* @param max The maximum bound
|
|
233
195
|
* @returns A random float within the range
|
|
234
196
|
* @example
|
|
235
|
-
* ```js
|
|
236
197
|
* const y = SMath.runif(-2, 2); // 0.376...
|
|
237
|
-
* ```
|
|
238
198
|
*/
|
|
239
199
|
export declare function runif(min: number, max: number): number;
|
|
240
200
|
/**
|
|
@@ -243,9 +203,7 @@ export declare function runif(min: number, max: number): number;
|
|
|
243
203
|
* @param max The maximum bound (inclusive)
|
|
244
204
|
* @returns A random integer within the range
|
|
245
205
|
* @example
|
|
246
|
-
* ```js
|
|
247
206
|
* const y = SMath.rint(-4, 3); // -4
|
|
248
|
-
* ```
|
|
249
207
|
*/
|
|
250
208
|
export declare function rint(min: number, max: number): number;
|
|
251
209
|
/**
|
|
@@ -254,9 +212,7 @@ export declare function rint(min: number, max: number): number;
|
|
|
254
212
|
* @param stdev The standard deviation of the population
|
|
255
213
|
* @returns A random float
|
|
256
214
|
* @example
|
|
257
|
-
* ```js
|
|
258
215
|
* const y = SMath.rnorm(2, 3); // 1.627...
|
|
259
|
-
* ```
|
|
260
216
|
*/
|
|
261
217
|
export declare function rnorm(mean?: number, stdev?: number): number;
|
|
262
218
|
/**
|
|
@@ -266,9 +222,7 @@ export declare function rnorm(mean?: number, stdev?: number): number;
|
|
|
266
222
|
* @param stdev The standard deviation of the population
|
|
267
223
|
* @returns A population of random floats
|
|
268
224
|
* @example
|
|
269
|
-
* ```js
|
|
270
225
|
* const dataset = SMath.rdist(3); // [ 1.051..., -0.779..., -2.254... ]
|
|
271
|
-
* ```
|
|
272
226
|
*/
|
|
273
227
|
export declare function rdist(count: number, mean?: number, stdev?: number): number[];
|
|
274
228
|
/**
|
|
@@ -276,9 +230,7 @@ export declare function rdist(count: number, mean?: number, stdev?: number): num
|
|
|
276
230
|
* @param stack An array of arbitrary elements
|
|
277
231
|
* @returns The `stack` array in a random order
|
|
278
232
|
* @example
|
|
279
|
-
* ```js
|
|
280
233
|
* const shuffled = SMath.shuffle(['a', 'b', 'c']); // [ 'c', 'a', 'b' ]
|
|
281
|
-
* ```
|
|
282
234
|
*/
|
|
283
235
|
export declare function shuffle<T>(stack: T[]): T[];
|
|
284
236
|
/**
|
|
@@ -290,23 +242,19 @@ export declare function shuffle<T>(stack: T[]): T[];
|
|
|
290
242
|
* @param discontinuity_cutoff The discontinuity cutoff
|
|
291
243
|
* @returns `lim(f(x->x))`
|
|
292
244
|
* @example
|
|
293
|
-
* ```js
|
|
294
245
|
* const y = SMath.lim(Math.log, 0); // -Infinity
|
|
295
|
-
* ```
|
|
296
246
|
*/
|
|
297
247
|
export declare function lim(f: (x: number) => number, x: number, h?: number, discontinuity_cutoff?: number): number;
|
|
298
248
|
/**
|
|
299
249
|
* Take the derivative of a function.
|
|
300
250
|
* @param f Function `f(x)`
|
|
301
251
|
* @param x The x-value where to evaluate the derivative
|
|
302
|
-
* @param
|
|
252
|
+
* @param epsilon Small step value
|
|
303
253
|
* @returns `f'(x)`
|
|
304
254
|
* @example
|
|
305
|
-
* ```js
|
|
306
255
|
* const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
|
|
307
|
-
* ```
|
|
308
256
|
*/
|
|
309
|
-
export declare function differentiate(f: (x: number) => number, x: number,
|
|
257
|
+
export declare function differentiate(f: (x: number) => number, x: number, epsilon?: number): number;
|
|
310
258
|
/**
|
|
311
259
|
* Compute the definite integral of a function.
|
|
312
260
|
* @param f Function `f(x)`
|
|
@@ -315,9 +263,7 @@ export declare function differentiate(f: (x: number) => number, x: number, h?: n
|
|
|
315
263
|
* @param Ndx The number of rectangles to compute
|
|
316
264
|
* @returns `F(b)-F(a)`
|
|
317
265
|
* @example
|
|
318
|
-
* ```js
|
|
319
266
|
* const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
|
|
320
|
-
* ```
|
|
321
267
|
*/
|
|
322
268
|
export declare function integrate(f: (x: number) => number, a: number, b: number, Ndx?: number): number;
|
|
323
269
|
/**
|
|
@@ -327,9 +273,7 @@ export declare function integrate(f: (x: number) => number, a: number, b: number
|
|
|
327
273
|
* @param epsilon Maximum absolute error
|
|
328
274
|
* @returns An object containing the fraction's numerator and denominator
|
|
329
275
|
* @example
|
|
330
|
-
* ```js
|
|
331
276
|
* const frac = SMath.rat(0.625); // { num: 5, den: 8 }
|
|
332
|
-
* ```
|
|
333
277
|
*/
|
|
334
278
|
export declare function rat(n: number, epsilon?: number): {
|
|
335
279
|
num: number;
|
|
@@ -343,23 +287,10 @@ export declare function rat(n: number, epsilon?: number): {
|
|
|
343
287
|
* @param epsilon Maximum absolute error
|
|
344
288
|
* @returns An object containing the whole part and fraction numerator and denominator
|
|
345
289
|
* @example
|
|
346
|
-
* ```js
|
|
347
290
|
* const frac = SMath.mixed(-8 / 6); // { whole: -1, num: 1, den: 3 }
|
|
348
|
-
* ```
|
|
349
291
|
*/
|
|
350
292
|
export declare function mixed(n: number, epsilon?: number): {
|
|
351
293
|
whole: number;
|
|
352
294
|
num: number;
|
|
353
295
|
den: number;
|
|
354
296
|
};
|
|
355
|
-
/**
|
|
356
|
-
* Convert any number to its hexadecimal equivalent.
|
|
357
|
-
* @param n A decimal number to convert
|
|
358
|
-
* @param length The minimum number of digits to show
|
|
359
|
-
* @returns The number `n` converted to hexadecimal
|
|
360
|
-
* @example
|
|
361
|
-
* ```js
|
|
362
|
-
* const hex = SMath.toHex(10, 2); // '0A'
|
|
363
|
-
* ```
|
|
364
|
-
*/
|
|
365
|
-
export declare function toHex(n: number, length?: number): string;
|