smath 1.10.0 → 1.11.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 +20 -15
- package/dist/index.js +54 -22
- package/package.json +3 -3
- package/types/index.d.ts +33 -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,12 +19,12 @@ 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
30
|
console.log(' error <exp> <act> : Calculate the normaized percent error between `exp` and `act`');
|
|
@@ -40,7 +40,8 @@ if (func.includes('help')) {
|
|
|
40
40
|
console.log(' rint <min> <max> : Generate a uniformly-distributed random integer, range inclusive');
|
|
41
41
|
console.log(' rnorm [mean] [stdev] : Generate a normally-distributed random float');
|
|
42
42
|
console.log(' rdist <n> [mean] [stdev] : Generate `n` normally-distributed random floats');
|
|
43
|
-
console.log('
|
|
43
|
+
console.log(' rat <n> [eps] : Decompose `n` into a ratio');
|
|
44
|
+
console.log(' mixed <n> [eps] : Decompose `n` into a mixed number');
|
|
44
45
|
process.exit(1);
|
|
45
46
|
}
|
|
46
47
|
switch (func) {
|
|
@@ -52,10 +53,18 @@ switch (func) {
|
|
|
52
53
|
console.log(_1.SMath.clamp(nums[0], nums[1], nums[2]));
|
|
53
54
|
break;
|
|
54
55
|
}
|
|
56
|
+
case ('normalize'): {
|
|
57
|
+
console.log(_1.SMath.normalize(nums[0], nums[1], nums[2]));
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
55
60
|
case ('expand'): {
|
|
56
61
|
console.log(_1.SMath.expand(nums[0], nums[1], nums[2]));
|
|
57
62
|
break;
|
|
58
63
|
}
|
|
64
|
+
case ('translate'): {
|
|
65
|
+
console.log(_1.SMath.translate(nums[0], nums[1], nums[2], nums[3], nums[4]));
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
59
68
|
case ('linspace'): {
|
|
60
69
|
console.log(_1.SMath.linspace(nums[0], nums[1], nums[2]));
|
|
61
70
|
break;
|
|
@@ -64,14 +73,6 @@ switch (func) {
|
|
|
64
73
|
console.log(_1.SMath.logspace(nums[0], nums[1], nums[2]));
|
|
65
74
|
break;
|
|
66
75
|
}
|
|
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
76
|
case ('factorial'): {
|
|
76
77
|
console.log(_1.SMath.factorial(nums[0]));
|
|
77
78
|
break;
|
|
@@ -132,8 +133,12 @@ switch (func) {
|
|
|
132
133
|
console.log(_1.SMath.rdist(nums[0], nums[1], nums[2]));
|
|
133
134
|
break;
|
|
134
135
|
}
|
|
135
|
-
case ('
|
|
136
|
-
console.log(_1.SMath.
|
|
136
|
+
case ('rat'): {
|
|
137
|
+
console.log(_1.SMath.rat(nums[0], (_c = nums[1]) !== null && _c !== void 0 ? _c : 1e-6));
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case ('mixed'): {
|
|
141
|
+
console.log(_1.SMath.mixed(nums[0], (_d = nums[1]) !== null && _d !== void 0 ? _d : 1e-6));
|
|
137
142
|
break;
|
|
138
143
|
}
|
|
139
144
|
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
|
/**
|
|
@@ -397,34 +408,13 @@ var SMath;
|
|
|
397
408
|
return distribution;
|
|
398
409
|
}
|
|
399
410
|
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
411
|
/**
|
|
423
412
|
* Randomize an array of arbitrary elements.
|
|
424
413
|
* @param stack An array of arbitrary elements
|
|
425
414
|
* @returns The `stack` array in a random order
|
|
426
415
|
* @example
|
|
427
416
|
* ```js
|
|
417
|
+
* const shuffled = SMath.shuffle(['a', 'b', 'c']); // [ 'c', 'a', 'b' ]
|
|
428
418
|
* ```
|
|
429
419
|
*/
|
|
430
420
|
function shuffle(stack) {
|
|
@@ -544,4 +534,46 @@ var SMath;
|
|
|
544
534
|
return ((b - a) / Ndx) * sum(linspace(a, b, Ndx).map(function (x) { return f(x); }));
|
|
545
535
|
}
|
|
546
536
|
SMath.integrate = integrate;
|
|
537
|
+
/**
|
|
538
|
+
* Convert an arbitrary decimal number into a simplified fraction (or ratio).
|
|
539
|
+
* See `mixed()` for instructions on how to break out the whole number part.
|
|
540
|
+
* @param n The decimal number to convert
|
|
541
|
+
* @param epsilon Maximum absolute error
|
|
542
|
+
* @returns An object containing the fraction's numerator and denominator
|
|
543
|
+
* @example
|
|
544
|
+
* ```js
|
|
545
|
+
* const frac = SMath.rat(0.625); // { num: 5, den: 8 }
|
|
546
|
+
* ```
|
|
547
|
+
*/
|
|
548
|
+
function rat(n, epsilon) {
|
|
549
|
+
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
550
|
+
var num = 0, den = 1, sign = n < 0 ? -1 : 1;
|
|
551
|
+
while (!approx(sign * n, num / den, epsilon)) {
|
|
552
|
+
if (sign * n > num / den) {
|
|
553
|
+
num++;
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
den++;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return { num: sign * num, den: den };
|
|
560
|
+
}
|
|
561
|
+
SMath.rat = rat;
|
|
562
|
+
/**
|
|
563
|
+
* Convert an arbitrary decimal number into a simplified fraction, after
|
|
564
|
+
* breaking out the whole number part first. See `rat()` for keeping the
|
|
565
|
+
* number as a ratio without separating the whole number part.
|
|
566
|
+
* @param n A decimal number to convert
|
|
567
|
+
* @param epsilon Maximum absolute error
|
|
568
|
+
* @returns An object containing the whole part and fraction numerator and denominator
|
|
569
|
+
* @example
|
|
570
|
+
* ```js
|
|
571
|
+
* const frac = SMath.mixed(-8 / 6); // { whole: -1, num: 1, den: 3 }
|
|
572
|
+
* ```
|
|
573
|
+
*/
|
|
574
|
+
function mixed(n, epsilon) {
|
|
575
|
+
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
576
|
+
return __assign({ whole: n | 0 }, rat(n < -1 ? (n | 0) - n : n - (n | 0), epsilon));
|
|
577
|
+
}
|
|
578
|
+
SMath.mixed = mixed;
|
|
547
579
|
})(SMath || (exports.SMath = SMath = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smath",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.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
|
@@ -263,24 +263,13 @@ export declare namespace SMath {
|
|
|
263
263
|
* ```
|
|
264
264
|
*/
|
|
265
265
|
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
266
|
/**
|
|
279
267
|
* Randomize an array of arbitrary elements.
|
|
280
268
|
* @param stack An array of arbitrary elements
|
|
281
269
|
* @returns The `stack` array in a random order
|
|
282
270
|
* @example
|
|
283
271
|
* ```js
|
|
272
|
+
* const shuffled = SMath.shuffle(['a', 'b', 'c']); // [ 'c', 'a', 'b' ]
|
|
284
273
|
* ```
|
|
285
274
|
*/
|
|
286
275
|
function shuffle<T>(stack: Array<T>): Array<T>;
|
|
@@ -323,4 +312,36 @@ export declare namespace SMath {
|
|
|
323
312
|
* ```
|
|
324
313
|
*/
|
|
325
314
|
function integrate(f: (x: number) => number, a: number, b: number, Ndx?: number): number;
|
|
315
|
+
/**
|
|
316
|
+
* Convert an arbitrary decimal number into a simplified fraction (or ratio).
|
|
317
|
+
* See `mixed()` for instructions on how to break out the whole number part.
|
|
318
|
+
* @param n The decimal number to convert
|
|
319
|
+
* @param epsilon Maximum absolute error
|
|
320
|
+
* @returns An object containing the fraction's numerator and denominator
|
|
321
|
+
* @example
|
|
322
|
+
* ```js
|
|
323
|
+
* const frac = SMath.rat(0.625); // { num: 5, den: 8 }
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
function rat(n: number, epsilon?: number): {
|
|
327
|
+
num: number;
|
|
328
|
+
den: number;
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* Convert an arbitrary decimal number into a simplified fraction, after
|
|
332
|
+
* breaking out the whole number part first. See `rat()` for keeping the
|
|
333
|
+
* number as a ratio without separating the whole number part.
|
|
334
|
+
* @param n A decimal number to convert
|
|
335
|
+
* @param epsilon Maximum absolute error
|
|
336
|
+
* @returns An object containing the whole part and fraction numerator and denominator
|
|
337
|
+
* @example
|
|
338
|
+
* ```js
|
|
339
|
+
* const frac = SMath.mixed(-8 / 6); // { whole: -1, num: 1, den: 3 }
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
function mixed(n: number, epsilon?: number): {
|
|
343
|
+
whole: number;
|
|
344
|
+
num: number;
|
|
345
|
+
den: number;
|
|
346
|
+
};
|
|
326
347
|
}
|