smath 1.10.0 → 1.12.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 +3 -1
- package/dist/bin.js +25 -15
- package/dist/index.js +69 -22
- package/package.json +3 -3
- package/types/index.d.ts +45 -12
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Small math? Simple math? Or supplemental math? Canonically, "SMath" is pronounce
|
|
|
7
7
|
SMath is also packaged with an executabe that can be run directly through `npx` in the terminal - even outside of a NodeJS project! In fact, open your terminal now, and type the following to show a list of valid `npx smath` commands!
|
|
8
8
|
|
|
9
9
|
```shell
|
|
10
|
-
npx smath
|
|
10
|
+
npx smath help
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
Commands are all structured like this.
|
|
@@ -21,3 +21,5 @@ This example command returns the value 0.4.
|
|
|
21
21
|
```shell
|
|
22
22
|
npx smath normalize 4 0 10
|
|
23
23
|
```
|
|
24
|
+
|
|
25
|
+
> Most `SMath` functions are available through `npx`, except for calculus functions which require a functional argument.
|
package/dist/bin.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
var _a, _b;
|
|
3
|
+
var _a, _b, _c, _d;
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
var _1 = require(".");
|
|
6
6
|
var func = ((_a = process.argv[2]) !== null && _a !== void 0 ? _a : '').toLowerCase(), nums = process.argv.slice(3).map(function (arg, i) {
|
|
@@ -19,14 +19,15 @@ if (func.includes('help')) {
|
|
|
19
19
|
console.log(' help : Show this page');
|
|
20
20
|
console.log(' approx <a> <b> [eps] : Check if `a` and `b` are approximately equal');
|
|
21
21
|
console.log(' clamp <n> <min> <max> : Clamp `n` between `min` and `max`');
|
|
22
|
-
console.log(' expand <n> <min> <max> : Expand normalized `n` between `min` and `max`');
|
|
23
|
-
console.log(' linspace <min> <max> <n> : Generate `n` linearly spaced numbers between `min` and `max`');
|
|
24
|
-
console.log(' logspace <min> <max> <n> : Generate `n` logarithmically spaced numbers between `min` and `max`');
|
|
25
22
|
console.log(' normalize <n> <min> <max>: Normalize `n` between `min` and `max`');
|
|
23
|
+
console.log(' expand <n> <min> <max> : Expand normalized `n` between `min` and `max`');
|
|
26
24
|
console.log(' translate <n> <min1> <max1> <min2> <max2>');
|
|
27
25
|
console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`');
|
|
26
|
+
console.log(' linspace <min> <max> <n> : Generate `n` linearly spaced numbers between `min` and `max`');
|
|
27
|
+
console.log(' logspace <min> <max> <n> : Generate `n` logarithmically spaced numbers between `min` and `max`');
|
|
28
28
|
console.log(' factorial <n> : Compute `n!` (factorial)');
|
|
29
29
|
console.log(' factors <n> : List the prime factors of `n`');
|
|
30
|
+
console.log(' round2 <n> <base> : Round `n` to a multiple of any `base`');
|
|
30
31
|
console.log(' error <exp> <act> : Calculate the normaized percent error between `exp` and `act`');
|
|
31
32
|
console.log(' sum <c0> [c1] ... [cn] : Compute a total of `n` numbers');
|
|
32
33
|
console.log(' prod <c0> [c1] ... [cn] : Compute a product of `n` numbers');
|
|
@@ -40,7 +41,8 @@ if (func.includes('help')) {
|
|
|
40
41
|
console.log(' rint <min> <max> : Generate a uniformly-distributed random integer, range inclusive');
|
|
41
42
|
console.log(' rnorm [mean] [stdev] : Generate a normally-distributed random float');
|
|
42
43
|
console.log(' rdist <n> [mean] [stdev] : Generate `n` normally-distributed random floats');
|
|
43
|
-
console.log('
|
|
44
|
+
console.log(' rat <n> [eps] : Decompose `n` into a ratio');
|
|
45
|
+
console.log(' mixed <n> [eps] : Decompose `n` into a mixed number');
|
|
44
46
|
process.exit(1);
|
|
45
47
|
}
|
|
46
48
|
switch (func) {
|
|
@@ -52,10 +54,18 @@ switch (func) {
|
|
|
52
54
|
console.log(_1.SMath.clamp(nums[0], nums[1], nums[2]));
|
|
53
55
|
break;
|
|
54
56
|
}
|
|
57
|
+
case ('normalize'): {
|
|
58
|
+
console.log(_1.SMath.normalize(nums[0], nums[1], nums[2]));
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
55
61
|
case ('expand'): {
|
|
56
62
|
console.log(_1.SMath.expand(nums[0], nums[1], nums[2]));
|
|
57
63
|
break;
|
|
58
64
|
}
|
|
65
|
+
case ('translate'): {
|
|
66
|
+
console.log(_1.SMath.translate(nums[0], nums[1], nums[2], nums[3], nums[4]));
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
59
69
|
case ('linspace'): {
|
|
60
70
|
console.log(_1.SMath.linspace(nums[0], nums[1], nums[2]));
|
|
61
71
|
break;
|
|
@@ -64,14 +74,6 @@ switch (func) {
|
|
|
64
74
|
console.log(_1.SMath.logspace(nums[0], nums[1], nums[2]));
|
|
65
75
|
break;
|
|
66
76
|
}
|
|
67
|
-
case ('normalize'): {
|
|
68
|
-
console.log(_1.SMath.normalize(nums[0], nums[1], nums[2]));
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
case ('translate'): {
|
|
72
|
-
console.log(_1.SMath.translate(nums[0], nums[1], nums[2], nums[3], nums[4]));
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
77
|
case ('factorial'): {
|
|
76
78
|
console.log(_1.SMath.factorial(nums[0]));
|
|
77
79
|
break;
|
|
@@ -80,6 +82,10 @@ switch (func) {
|
|
|
80
82
|
console.log(_1.SMath.factors(nums[0]));
|
|
81
83
|
break;
|
|
82
84
|
}
|
|
85
|
+
case ('round2'): {
|
|
86
|
+
console.log(_1.SMath.round2(nums[0], nums[1]));
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
83
89
|
case ('error'): {
|
|
84
90
|
console.log(_1.SMath.error(nums[0], nums[1]));
|
|
85
91
|
break;
|
|
@@ -132,8 +138,12 @@ switch (func) {
|
|
|
132
138
|
console.log(_1.SMath.rdist(nums[0], nums[1], nums[2]));
|
|
133
139
|
break;
|
|
134
140
|
}
|
|
135
|
-
case ('
|
|
136
|
-
console.log(_1.SMath.
|
|
141
|
+
case ('rat'): {
|
|
142
|
+
console.log(_1.SMath.rat(nums[0], (_c = nums[1]) !== null && _c !== void 0 ? _c : 1e-6));
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
case ('mixed'): {
|
|
146
|
+
console.log(_1.SMath.mixed(nums[0], (_d = nums[1]) !== null && _d !== void 0 ? _d : 1e-6));
|
|
137
147
|
break;
|
|
138
148
|
}
|
|
139
149
|
case (''): {
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
2
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
14
|
exports.SMath = void 0;
|
|
4
15
|
/**
|
|
@@ -193,6 +204,21 @@ var SMath;
|
|
|
193
204
|
return f;
|
|
194
205
|
}
|
|
195
206
|
SMath.factors = factors;
|
|
207
|
+
/**
|
|
208
|
+
* Round a number to the nearest multiple of an arbitrary
|
|
209
|
+
* base. Does not round when the base is set to zero.
|
|
210
|
+
* @param n Any number to round
|
|
211
|
+
* @param base Any base to round to
|
|
212
|
+
* @returns `n` rounded to the nearest multiple of `base`
|
|
213
|
+
* @example
|
|
214
|
+
* ```js
|
|
215
|
+
* const y = SMath.round2(Math.PI, 0.2); // 3.2
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
218
|
+
function round2(n, base) {
|
|
219
|
+
return base ? base * Math.round(n / base) : n;
|
|
220
|
+
}
|
|
221
|
+
SMath.round2 = round2;
|
|
196
222
|
/**
|
|
197
223
|
* Calculate the relative normalized error or deviation from any
|
|
198
224
|
* value to an accepted value. An error of 0 indicates that the
|
|
@@ -397,34 +423,13 @@ var SMath;
|
|
|
397
423
|
return distribution;
|
|
398
424
|
}
|
|
399
425
|
SMath.rdist = rdist;
|
|
400
|
-
/**
|
|
401
|
-
* Randomize a sequence of integers from `min` to `max`.
|
|
402
|
-
* @param min The minimum integer in the randomized sequence
|
|
403
|
-
* @param max The maximum integer in the randomized sequence
|
|
404
|
-
* @returns A randomized sequence of integers from `min` to `max`
|
|
405
|
-
* @example
|
|
406
|
-
* ```js
|
|
407
|
-
* const sequence = SMath.rseq(-2, 2); // [ 2, 0, 1, -2, -1 ]
|
|
408
|
-
* ```
|
|
409
|
-
* @deprecated Use `SMath.shuffle()` instead
|
|
410
|
-
*/
|
|
411
|
-
function rseq(min, max) {
|
|
412
|
-
min |= 0;
|
|
413
|
-
max |= 0;
|
|
414
|
-
max++;
|
|
415
|
-
var rawData = [];
|
|
416
|
-
for (var i = min; i < max; i++) {
|
|
417
|
-
rawData.push({ index: runif(-1, 1), value: i });
|
|
418
|
-
}
|
|
419
|
-
return rawData.sort(function (a, b) { return a.index - b.index; }).map(function (a) { return a.value; });
|
|
420
|
-
}
|
|
421
|
-
SMath.rseq = rseq;
|
|
422
426
|
/**
|
|
423
427
|
* Randomize an array of arbitrary elements.
|
|
424
428
|
* @param stack An array of arbitrary elements
|
|
425
429
|
* @returns The `stack` array in a random order
|
|
426
430
|
* @example
|
|
427
431
|
* ```js
|
|
432
|
+
* const shuffled = SMath.shuffle(['a', 'b', 'c']); // [ 'c', 'a', 'b' ]
|
|
428
433
|
* ```
|
|
429
434
|
*/
|
|
430
435
|
function shuffle(stack) {
|
|
@@ -544,4 +549,46 @@ var SMath;
|
|
|
544
549
|
return ((b - a) / Ndx) * sum(linspace(a, b, Ndx).map(function (x) { return f(x); }));
|
|
545
550
|
}
|
|
546
551
|
SMath.integrate = integrate;
|
|
552
|
+
/**
|
|
553
|
+
* Convert an arbitrary decimal number into a simplified fraction (or ratio).
|
|
554
|
+
* See `mixed()` for instructions on how to break out the whole number part.
|
|
555
|
+
* @param n The decimal number to convert
|
|
556
|
+
* @param epsilon Maximum absolute error
|
|
557
|
+
* @returns An object containing the fraction's numerator and denominator
|
|
558
|
+
* @example
|
|
559
|
+
* ```js
|
|
560
|
+
* const frac = SMath.rat(0.625); // { num: 5, den: 8 }
|
|
561
|
+
* ```
|
|
562
|
+
*/
|
|
563
|
+
function rat(n, epsilon) {
|
|
564
|
+
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
565
|
+
var num = 0, den = 1, sign = n < 0 ? -1 : 1;
|
|
566
|
+
while (!approx(sign * n, num / den, epsilon)) {
|
|
567
|
+
if (sign * n > num / den) {
|
|
568
|
+
num++;
|
|
569
|
+
}
|
|
570
|
+
else {
|
|
571
|
+
den++;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
return { num: sign * num, den: den };
|
|
575
|
+
}
|
|
576
|
+
SMath.rat = rat;
|
|
577
|
+
/**
|
|
578
|
+
* Convert an arbitrary decimal number into a simplified fraction, after
|
|
579
|
+
* breaking out the whole number part first. See `rat()` for keeping the
|
|
580
|
+
* number as a ratio without separating the whole number part.
|
|
581
|
+
* @param n A decimal number to convert
|
|
582
|
+
* @param epsilon Maximum absolute error
|
|
583
|
+
* @returns An object containing the whole part and fraction numerator and denominator
|
|
584
|
+
* @example
|
|
585
|
+
* ```js
|
|
586
|
+
* const frac = SMath.mixed(-8 / 6); // { whole: -1, num: 1, den: 3 }
|
|
587
|
+
* ```
|
|
588
|
+
*/
|
|
589
|
+
function mixed(n, epsilon) {
|
|
590
|
+
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
591
|
+
return __assign({ whole: n | 0 }, rat(n < -1 ? (n | 0) - n : n - (n | 0), epsilon));
|
|
592
|
+
}
|
|
593
|
+
SMath.mixed = mixed;
|
|
547
594
|
})(SMath || (exports.SMath = SMath = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smath",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.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": "22.10.
|
|
56
|
-
"t6": "1.1.
|
|
55
|
+
"@types/node": "22.10.5",
|
|
56
|
+
"t6": "1.1.9"
|
|
57
57
|
}
|
|
58
58
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -120,6 +120,18 @@ export declare namespace SMath {
|
|
|
120
120
|
* ```
|
|
121
121
|
*/
|
|
122
122
|
function factors(n: number): Array<number>;
|
|
123
|
+
/**
|
|
124
|
+
* Round a number to the nearest multiple of an arbitrary
|
|
125
|
+
* base. Does not round when the base is set to zero.
|
|
126
|
+
* @param n Any number to round
|
|
127
|
+
* @param base Any base to round to
|
|
128
|
+
* @returns `n` rounded to the nearest multiple of `base`
|
|
129
|
+
* @example
|
|
130
|
+
* ```js
|
|
131
|
+
* const y = SMath.round2(Math.PI, 0.2); // 3.2
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
function round2(n: number, base: number): number;
|
|
123
135
|
/**
|
|
124
136
|
* Calculate the relative normalized error or deviation from any
|
|
125
137
|
* value to an accepted value. An error of 0 indicates that the
|
|
@@ -263,24 +275,13 @@ export declare namespace SMath {
|
|
|
263
275
|
* ```
|
|
264
276
|
*/
|
|
265
277
|
function rdist(count: number, mean?: number, stdev?: number): Array<number>;
|
|
266
|
-
/**
|
|
267
|
-
* Randomize a sequence of integers from `min` to `max`.
|
|
268
|
-
* @param min The minimum integer in the randomized sequence
|
|
269
|
-
* @param max The maximum integer in the randomized sequence
|
|
270
|
-
* @returns A randomized sequence of integers from `min` to `max`
|
|
271
|
-
* @example
|
|
272
|
-
* ```js
|
|
273
|
-
* const sequence = SMath.rseq(-2, 2); // [ 2, 0, 1, -2, -1 ]
|
|
274
|
-
* ```
|
|
275
|
-
* @deprecated Use `SMath.shuffle()` instead
|
|
276
|
-
*/
|
|
277
|
-
function rseq(min: number, max: number): Array<number>;
|
|
278
278
|
/**
|
|
279
279
|
* Randomize an array of arbitrary elements.
|
|
280
280
|
* @param stack An array of arbitrary elements
|
|
281
281
|
* @returns The `stack` array in a random order
|
|
282
282
|
* @example
|
|
283
283
|
* ```js
|
|
284
|
+
* const shuffled = SMath.shuffle(['a', 'b', 'c']); // [ 'c', 'a', 'b' ]
|
|
284
285
|
* ```
|
|
285
286
|
*/
|
|
286
287
|
function shuffle<T>(stack: Array<T>): Array<T>;
|
|
@@ -323,4 +324,36 @@ export declare namespace SMath {
|
|
|
323
324
|
* ```
|
|
324
325
|
*/
|
|
325
326
|
function integrate(f: (x: number) => number, a: number, b: number, Ndx?: number): number;
|
|
327
|
+
/**
|
|
328
|
+
* Convert an arbitrary decimal number into a simplified fraction (or ratio).
|
|
329
|
+
* See `mixed()` for instructions on how to break out the whole number part.
|
|
330
|
+
* @param n The decimal number to convert
|
|
331
|
+
* @param epsilon Maximum absolute error
|
|
332
|
+
* @returns An object containing the fraction's numerator and denominator
|
|
333
|
+
* @example
|
|
334
|
+
* ```js
|
|
335
|
+
* const frac = SMath.rat(0.625); // { num: 5, den: 8 }
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
function rat(n: number, epsilon?: number): {
|
|
339
|
+
num: number;
|
|
340
|
+
den: number;
|
|
341
|
+
};
|
|
342
|
+
/**
|
|
343
|
+
* Convert an arbitrary decimal number into a simplified fraction, after
|
|
344
|
+
* breaking out the whole number part first. See `rat()` for keeping the
|
|
345
|
+
* number as a ratio without separating the whole number part.
|
|
346
|
+
* @param n A decimal number to convert
|
|
347
|
+
* @param epsilon Maximum absolute error
|
|
348
|
+
* @returns An object containing the whole part and fraction numerator and denominator
|
|
349
|
+
* @example
|
|
350
|
+
* ```js
|
|
351
|
+
* const frac = SMath.mixed(-8 / 6); // { whole: -1, num: 1, den: 3 }
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
function mixed(n: number, epsilon?: number): {
|
|
355
|
+
whole: number;
|
|
356
|
+
num: number;
|
|
357
|
+
den: number;
|
|
358
|
+
};
|
|
326
359
|
}
|