smath 1.9.2 → 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 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(' rseq <min> <max> : Randomize a sequence of integers from `min` to `max`');
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 ('rseq'): {
136
- console.log(_1.SMath.rseq(nums[0], nums[1]));
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,9 +1,22 @@
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
  /**
5
16
  * @packageDocumentation
6
- * ![NPM Downloads](https://img.shields.io/npm/d18m/smath) Small math function library
17
+ * Small math function library
18
+ *
19
+ * ![NPM Downloads](https://img.shields.io/npm/d18m/smath)
7
20
  */
8
21
  /**
9
22
  * Contains a small math function library including
@@ -396,26 +409,23 @@ var SMath;
396
409
  }
397
410
  SMath.rdist = rdist;
398
411
  /**
399
- * Randomize a sequence of integers from `min` to `max`.
400
- * @param min The minimum integer in the randomized sequence
401
- * @param max The maximum integer in the randomized sequence
402
- * @returns A randomized sequence of integers from `min` to `max`
412
+ * Randomize an array of arbitrary elements.
413
+ * @param stack An array of arbitrary elements
414
+ * @returns The `stack` array in a random order
403
415
  * @example
404
416
  * ```js
405
- * const sequence = SMath.rseq(-2, 2); // [ 2, 0, 1, -2, -1 ]
417
+ * const shuffled = SMath.shuffle(['a', 'b', 'c']); // [ 'c', 'a', 'b' ]
406
418
  * ```
407
419
  */
408
- function rseq(min, max) {
409
- min |= 0;
410
- max |= 0;
411
- max++;
420
+ function shuffle(stack) {
412
421
  var rawData = [];
413
- for (var i = min; i < max; i++) {
414
- rawData.push({ index: runif(-1, 1), value: i });
422
+ for (var _i = 0, stack_1 = stack; _i < stack_1.length; _i++) {
423
+ var item = stack_1[_i];
424
+ rawData.push({ index: Math.random(), value: item });
415
425
  }
416
426
  return rawData.sort(function (a, b) { return a.index - b.index; }).map(function (a) { return a.value; });
417
427
  }
418
- SMath.rseq = rseq;
428
+ SMath.shuffle = shuffle;
419
429
  /**
420
430
  * Take the limit of a function. A return value of `NaN` indicates
421
431
  * that no limit exists either due to a discontinuity or imaginary value.
@@ -524,4 +534,46 @@ var SMath;
524
534
  return ((b - a) / Ndx) * sum(linspace(a, b, Ndx).map(function (x) { return f(x); }));
525
535
  }
526
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;
527
579
  })(SMath || (exports.SMath = SMath = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "1.9.2",
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.1",
56
- "t6": "1.1.8"
55
+ "@types/node": "22.10.5",
56
+ "t6": "1.1.9"
57
57
  }
58
58
  }
package/types/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  /**
2
2
  * @packageDocumentation
3
- * ![NPM Downloads](https://img.shields.io/npm/d18m/smath) Small math function library
3
+ * Small math function library
4
+ *
5
+ * ![NPM Downloads](https://img.shields.io/npm/d18m/smath)
4
6
  */
5
7
  /**
6
8
  * Contains a small math function library including
@@ -262,16 +264,15 @@ export declare namespace SMath {
262
264
  */
263
265
  function rdist(count: number, mean?: number, stdev?: number): Array<number>;
264
266
  /**
265
- * Randomize a sequence of integers from `min` to `max`.
266
- * @param min The minimum integer in the randomized sequence
267
- * @param max The maximum integer in the randomized sequence
268
- * @returns A randomized sequence of integers from `min` to `max`
267
+ * Randomize an array of arbitrary elements.
268
+ * @param stack An array of arbitrary elements
269
+ * @returns The `stack` array in a random order
269
270
  * @example
270
271
  * ```js
271
- * const sequence = SMath.rseq(-2, 2); // [ 2, 0, 1, -2, -1 ]
272
+ * const shuffled = SMath.shuffle(['a', 'b', 'c']); // [ 'c', 'a', 'b' ]
272
273
  * ```
273
274
  */
274
- function rseq(min: number, max: number): Array<number>;
275
+ function shuffle<T>(stack: Array<T>): Array<T>;
275
276
  /**
276
277
  * Take the limit of a function. A return value of `NaN` indicates
277
278
  * that no limit exists either due to a discontinuity or imaginary value.
@@ -311,4 +312,36 @@ export declare namespace SMath {
311
312
  * ```
312
313
  */
313
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
+ };
314
347
  }