smath 1.3.5 → 1.5.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
@@ -2,7 +2,7 @@
2
2
 
3
3
  ![NPM Downloads](https://img.shields.io/npm/dt/smath)
4
4
  ![NPM Version](https://img.shields.io/npm/v/smath)
5
- ![Relative date](https://img.shields.io/date/1711226964)
5
+ ![Relative date](https://img.shields.io/date/1711484161)
6
6
  ![GitHub watchers](https://img.shields.io/github/watchers/nicfv/npm)
7
7
  ![GitHub forks](https://img.shields.io/github/forks/nicfv/npm)
8
8
  ![GitHub Repo stars](https://img.shields.io/github/stars/nicfv/npm)
@@ -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.3.5
15
+ npm i smath@1.5.0
16
16
  ```
17
17
 
18
18
  ## Bugs and Requests
@@ -25,6 +25,26 @@ Thank you for your interest in contributing to smath! smath is an open source so
25
25
  ## Getting Started
26
26
 
27
27
  Small math? Simple math? Or supplemental math? Canonically, "SMath" is pronounced "smath" and stands for "small math (library.)" Similar to JavaScript's builtin [`Math`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math) object, `SMath` exports one global object with several math-related helper functions. There is no need to instantiate the class, just call functions directly. See the examples below to get started using SMath!
28
+
29
+ ## Executables
30
+
31
+ 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!
32
+
33
+ ```shell
34
+ npx smath
35
+ ```
36
+
37
+ Commands are all structured like this.
38
+
39
+ ```shell
40
+ npx smath [cmd] [args]
41
+ ```
42
+
43
+ This example command returns the value 0.4.
44
+
45
+ ```shell
46
+ npx smath normalize 4 0 10
47
+ ```
28
48
  ## Examples
29
49
  Here are a few quickstart examples written in JavaScript that showcase some out-of-box features of the `smath` package.
30
50
  ### JavaScript Math Oddities
package/dist/bin.js CHANGED
@@ -1,40 +1,28 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var _a;
3
4
  Object.defineProperty(exports, "__esModule", { value: true });
4
5
  var _1 = require(".");
5
- var args = process.argv.slice(2);
6
- /**
7
- * Try to convert an argument into a numeric value.
8
- */
9
- function N(index, defaultVal) {
10
- if (defaultVal === void 0) { defaultVal = NaN; }
11
- if (index >= args.length) {
12
- if (Number.isFinite(defaultVal)) {
13
- return defaultVal;
14
- }
15
- else {
16
- console.error('Required argument ' + index + ' is missing!');
17
- process.exit(1);
18
- }
19
- }
20
- var arg = Number.parseFloat(args[index]);
21
- if (Number.isFinite(arg)) {
22
- return arg;
23
- }
24
- else if (Number.isFinite(defaultVal)) {
25
- return defaultVal;
6
+ var func = process.argv[2].toLowerCase(), nums = process.argv.slice(3).map(function (arg, i) {
7
+ var num = Number.parseFloat(arg);
8
+ if (Number.isFinite(num)) {
9
+ return num;
26
10
  }
27
11
  else {
28
- console.error('Argument #' + index + ' is ' + arg + ' but a number was expected.');
12
+ console.error('Argument #' + i + ' is "' + arg + '" but a number was expected.');
29
13
  process.exit(1);
30
14
  }
31
- }
32
- if (args.length < 1 || args[0].includes('help')) {
15
+ });
16
+ if (func.includes('help')) {
33
17
  console.log('Key: <required> [optional]');
34
18
  console.log('Arguments:');
35
19
  console.log(' help : Show this page');
36
- console.log(' approx <a> <b> [eps] : Check if `a` and `b` are approximately equal');
20
+ console.log(' sum <c0> [c1] ... [cn] : Compute a total of `n` numbers');
21
+ console.log(' prod <c0> [c1] ... [cn] : Compute a product of `n` numbers');
37
22
  console.log(' avg <c0> [c1] ... [cn] : Take an average of `n` numbers');
23
+ console.log(' varp <c0> [c1] ... [cn] : Compute the population variance of `n` numbers');
24
+ console.log(' vars <c0> [c1] ... [cn] : Compute the sample variance of `n` numbers');
25
+ console.log(' approx <a> <b> [eps] : Check if `a` and `b` are approximately equal');
38
26
  console.log(' clamp <n> <min> <max> : Clamp `n` between `min` and `max`');
39
27
  console.log(' expand <n> <min> <max> : Expand normalized `n` between `min` and `max`');
40
28
  console.log(' linspace <min> <max> <n> : Generate `n` linearly spaced numbers between `min` and `max`');
@@ -43,51 +31,69 @@ if (args.length < 1 || args[0].includes('help')) {
43
31
  console.log(' : Normalize `n` between `min` and `max`');
44
32
  console.log(' translate <n> <min1> <max1> <min2> <max2>');
45
33
  console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`');
34
+ console.log(' factorial <n> : Compute `n!` (factorial)');
35
+ console.log(' error <exp> <act> : Calculate the normaized percent error between `exp` and `act`');
46
36
  process.exit(1);
47
37
  }
48
- switch (args[0]) {
49
- case ('approx'): {
50
- console.log(_1.SMath.approx(N(1), N(2), N(3, 1e-6)));
38
+ switch (func) {
39
+ case ('sum'): {
40
+ console.log(_1.SMath.sum(nums));
41
+ break;
42
+ }
43
+ case ('prod'): {
44
+ console.log(_1.SMath.prod(nums));
51
45
  break;
52
46
  }
53
47
  case ('avg'): {
54
- if (args.length < 2) {
55
- console.error('Need at least 1 argument.');
56
- process.exit(1);
57
- }
58
- var operands = [];
59
- for (var i = 1; i < args.length; i++) {
60
- operands.push(N(i));
61
- }
62
- console.log(_1.SMath.avg.apply(_1.SMath, operands));
48
+ console.log(_1.SMath.avg(nums));
49
+ break;
50
+ }
51
+ case ('varp'): {
52
+ console.log(_1.SMath.varp(nums));
53
+ break;
54
+ }
55
+ case ('vars'): {
56
+ console.log(_1.SMath.vars(nums));
57
+ break;
58
+ }
59
+ case ('approx'): {
60
+ console.log(_1.SMath.approx(nums[0], nums[1], (_a = nums[2]) !== null && _a !== void 0 ? _a : 1e-6));
63
61
  break;
64
62
  }
65
63
  case ('clamp'): {
66
- console.log(_1.SMath.clamp(N(1), N(2), N(3)));
64
+ console.log(_1.SMath.clamp(nums[0], nums[1], nums[2]));
67
65
  break;
68
66
  }
69
67
  case ('expand'): {
70
- console.log(_1.SMath.expand(N(1), N(2), N(3)));
68
+ console.log(_1.SMath.expand(nums[0], nums[1], nums[2]));
71
69
  break;
72
70
  }
73
71
  case ('linspace'): {
74
- console.log(_1.SMath.linspace(N(1), N(2), N(3)));
72
+ console.log(_1.SMath.linspace(nums[0], nums[1], nums[2]));
75
73
  break;
76
74
  }
77
75
  case ('logspace'): {
78
- console.log(_1.SMath.logspace(N(1), N(2), N(3)));
76
+ console.log(_1.SMath.logspace(nums[0], nums[1], nums[2]));
79
77
  break;
80
78
  }
81
79
  case ('normalize'): {
82
- console.log(_1.SMath.normalize(N(1), N(2), N(3)));
80
+ console.log(_1.SMath.normalize(nums[0], nums[1], nums[2]));
83
81
  break;
84
82
  }
85
83
  case ('translate'): {
86
- console.log(_1.SMath.translate(N(1), N(2), N(3), N(4), N(5)));
84
+ console.log(_1.SMath.translate(nums[0], nums[1], nums[2], nums[3], nums[4]));
85
+ break;
86
+ }
87
+ case ('factorial'): {
88
+ console.log(_1.SMath.factorial(nums[0]));
89
+ break;
90
+ }
91
+ case ('error'): {
92
+ console.log(_1.SMath.error(nums[0], nums[1]));
87
93
  break;
88
94
  }
89
95
  default: {
90
- console.error('Unknown argument "' + args[0] + '". Use with "help" for a list of commands.');
96
+ console.error('Unknown argument "' + func + '". Use with "help" for a list of commands.');
91
97
  process.exit(1);
92
98
  }
93
99
  }
package/dist/index.js CHANGED
@@ -14,42 +14,69 @@ exports.SMath = void 0;
14
14
  var SMath = /** @class */ (function () {
15
15
  function SMath() {
16
16
  }
17
+ /**
18
+ * Add up all the inputs.
19
+ * If none are present, returns 0.
20
+ * @param n Any amount of numeric inputs
21
+ * @returns The sum total
22
+ * @example
23
+ * ```js
24
+ * const y = SMath.sum([1, 2, 3]); // 6
25
+ * ```
26
+ */
27
+ SMath.sum = function (n) {
28
+ return n.reduce(function (a, b) { return a + b; }, 0);
29
+ };
30
+ /**
31
+ * Multiply all the inputs.
32
+ * If none are present, returns 1.
33
+ * @param n Any amount of numeric inputs
34
+ * @returns The product
35
+ * @example
36
+ * ```js
37
+ * const y = SMath.prod([2, 2, 3, 5]); // 60
38
+ * ```
39
+ */
40
+ SMath.prod = function (n) {
41
+ return n.reduce(function (a, b) { return a * b; }, 1);
42
+ };
17
43
  /**
18
44
  * Compute the average, or mean, of a set of numbers.
19
45
  * @param n Any amount of numeric inputs
20
46
  * @returns The average, or mean
21
47
  * @example
22
48
  * ```js
23
- * const mean = SMath.avg(1, 2, 3, 4); // 2.5
49
+ * const y = SMath.avg([1, 2, 4, 4]); // 2.75
24
50
  * ```
25
51
  */
26
- SMath.avg = function () {
27
- var n = [];
28
- for (var _i = 0; _i < arguments.length; _i++) {
29
- n[_i] = arguments[_i];
30
- }
31
- return n.reduce(function (prev, curr) { return prev + curr; }) / n.length;
52
+ SMath.avg = function (n) {
53
+ return this.sum(n) / n.length;
32
54
  };
33
55
  /**
34
- * Clamp a number within a range.
35
- * @param n The number to clamp
36
- * @param min The minimum value of the range
37
- * @param max The maximum value of the range
38
- * @returns A clamped number
56
+ * Compute the variance of a **complete population**.
57
+ * @param n Any amount of numeric inputs
58
+ * @returns The population variance
39
59
  * @example
40
60
  * ```js
41
- * const n1 = SMath.clamp(5, 0, 10), // 5
42
- * n2 = SMath.clamp(-2, 0, 10); // 0
61
+ * const y = SMath.varp([1, 2, 4, 4]); // 1.6875
43
62
  * ```
44
63
  */
45
- SMath.clamp = function (n, min, max) {
46
- if (n < min) {
47
- return min;
48
- }
49
- if (n > max) {
50
- return max;
51
- }
52
- return n;
64
+ SMath.varp = function (n) {
65
+ var mean = this.avg(n), squares = n.map(function (x) { return Math.pow((x - mean), 2); });
66
+ return this.sum(squares) / n.length;
67
+ };
68
+ /**
69
+ * Compute the variance of a **sample**.
70
+ * @param n Any amount of numeric inputs
71
+ * @returns The sample variance
72
+ * @example
73
+ * ```js
74
+ * const y = SMath.vars([1, 2, 4, 4]); // 2.25
75
+ * ```
76
+ */
77
+ SMath.vars = function (n) {
78
+ var mean = this.avg(n), squares = n.map(function (x) { return Math.pow((x - mean), 2); });
79
+ return this.sum(squares) / (n.length - 1);
53
80
  };
54
81
  /**
55
82
  * Check if two numbers are approximately equal with a maximum abolute error.
@@ -67,6 +94,27 @@ var SMath = /** @class */ (function () {
67
94
  if (epsilon === void 0) { epsilon = 1e-6; }
68
95
  return a - b < epsilon && b - a < epsilon;
69
96
  };
97
+ /**
98
+ * Clamp a number within a range.
99
+ * @param n The number to clamp
100
+ * @param min The minimum value of the range
101
+ * @param max The maximum value of the range
102
+ * @returns A clamped number
103
+ * @example
104
+ * ```js
105
+ * const n1 = SMath.clamp(5, 0, 10), // 5
106
+ * n2 = SMath.clamp(-2, 0, 10); // 0
107
+ * ```
108
+ */
109
+ SMath.clamp = function (n, min, max) {
110
+ if (n < min) {
111
+ return min;
112
+ }
113
+ if (n > max) {
114
+ return max;
115
+ }
116
+ return n;
117
+ };
70
118
  /**
71
119
  * Normalize the number `n` from the range `min, max` to the range `0, 1`
72
120
  * @param n The number to normalize
@@ -149,6 +197,47 @@ var SMath = /** @class */ (function () {
149
197
  SMath.logspace = function (min, max, count) {
150
198
  return this.linspace(min, max, count).map(function (n) { return Math.pow(10, n); });
151
199
  };
200
+ /**
201
+ * Compute the factorial of `n`.
202
+ * @param n Any positive integer
203
+ * @returns `n!`
204
+ * @example
205
+ * ```js
206
+ * const y = SMath.factorial(5); // 120
207
+ * ```
208
+ */
209
+ SMath.factorial = function (n) {
210
+ if (n < 0 || (n | 0) !== n) {
211
+ throw new Error('Input must be a positive integer.');
212
+ }
213
+ else if (n === 0) {
214
+ return 1;
215
+ }
216
+ else if (n <= 2) {
217
+ return n;
218
+ }
219
+ else {
220
+ return n * this.factorial(n - 1);
221
+ }
222
+ };
223
+ /**
224
+ * Calculate the relative normalized error or deviation from any
225
+ * value to an accepted value. An error of 0 indicates that the
226
+ * two values are identical. An error of -0.1 indicates that the
227
+ * experimental value is 10% smaller than (90% of) the accepted
228
+ * value. An error of 1.0 indicates that the experimental value
229
+ * is 100% greater (or twice the size) of the accepted value.
230
+ * @param experimental The value observed or produced by a test
231
+ * @param actual The accepted or theoretical value
232
+ * @returns The relative (normalized) error
233
+ * @example
234
+ * ```js
235
+ * const e = SMath.error(22.5, 25); // -0.1
236
+ * ```
237
+ */
238
+ SMath.error = function (experimental, actual) {
239
+ return (experimental - actual) / actual;
240
+ };
152
241
  return SMath;
153
242
  }());
154
243
  exports.SMath = SMath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "1.3.5",
3
+ "version": "1.5.0",
4
4
  "description": "Small math function library",
5
5
  "homepage": "https://npm.nicfv.com/smath",
6
6
  "bin": "dist/bin.js",
@@ -48,7 +48,7 @@
48
48
  "license": "MIT",
49
49
  "devDependencies": {
50
50
  "@types/node": "20.11.30",
51
- "exray": "1.0.0",
51
+ "exray": "1.0.2",
52
52
  "typedoc": "0.25.12",
53
53
  "typescript": "5.4.3"
54
54
  }
package/types/index.d.ts CHANGED
@@ -9,29 +9,58 @@
9
9
  * useful interpolation and extrapolation functions.
10
10
  */
11
11
  export declare abstract class SMath {
12
+ /**
13
+ * Add up all the inputs.
14
+ * If none are present, returns 0.
15
+ * @param n Any amount of numeric inputs
16
+ * @returns The sum total
17
+ * @example
18
+ * ```js
19
+ * const y = SMath.sum([1, 2, 3]); // 6
20
+ * ```
21
+ */
22
+ static sum(n: Array<number>): number;
23
+ /**
24
+ * Multiply all the inputs.
25
+ * If none are present, returns 1.
26
+ * @param n Any amount of numeric inputs
27
+ * @returns The product
28
+ * @example
29
+ * ```js
30
+ * const y = SMath.prod([2, 2, 3, 5]); // 60
31
+ * ```
32
+ */
33
+ static prod(n: Array<number>): number;
12
34
  /**
13
35
  * Compute the average, or mean, of a set of numbers.
14
36
  * @param n Any amount of numeric inputs
15
37
  * @returns The average, or mean
16
38
  * @example
17
39
  * ```js
18
- * const mean = SMath.avg(1, 2, 3, 4); // 2.5
40
+ * const y = SMath.avg([1, 2, 4, 4]); // 2.75
19
41
  * ```
20
42
  */
21
- static avg(...n: Array<number>): number;
43
+ static avg(n: Array<number>): number;
22
44
  /**
23
- * Clamp a number within a range.
24
- * @param n The number to clamp
25
- * @param min The minimum value of the range
26
- * @param max The maximum value of the range
27
- * @returns A clamped number
45
+ * Compute the variance of a **complete population**.
46
+ * @param n Any amount of numeric inputs
47
+ * @returns The population variance
28
48
  * @example
29
49
  * ```js
30
- * const n1 = SMath.clamp(5, 0, 10), // 5
31
- * n2 = SMath.clamp(-2, 0, 10); // 0
50
+ * const y = SMath.varp([1, 2, 4, 4]); // 1.6875
32
51
  * ```
33
52
  */
34
- static clamp(n: number, min: number, max: number): number;
53
+ static varp(n: Array<number>): number;
54
+ /**
55
+ * Compute the variance of a **sample**.
56
+ * @param n Any amount of numeric inputs
57
+ * @returns The sample variance
58
+ * @example
59
+ * ```js
60
+ * const y = SMath.vars([1, 2, 4, 4]); // 2.25
61
+ * ```
62
+ */
63
+ static vars(n: Array<number>): number;
35
64
  /**
36
65
  * Check if two numbers are approximately equal with a maximum abolute error.
37
66
  * @param a Any number
@@ -45,6 +74,19 @@ export declare abstract class SMath {
45
74
  * ```
46
75
  */
47
76
  static approx(a: number, b: number, epsilon?: number): boolean;
77
+ /**
78
+ * Clamp a number within a range.
79
+ * @param n The number to clamp
80
+ * @param min The minimum value of the range
81
+ * @param max The maximum value of the range
82
+ * @returns A clamped number
83
+ * @example
84
+ * ```js
85
+ * const n1 = SMath.clamp(5, 0, 10), // 5
86
+ * n2 = SMath.clamp(-2, 0, 10); // 0
87
+ * ```
88
+ */
89
+ static clamp(n: number, min: number, max: number): number;
48
90
  /**
49
91
  * Normalize the number `n` from the range `min, max` to the range `0, 1`
50
92
  * @param n The number to normalize
@@ -110,4 +152,30 @@ export declare abstract class SMath {
110
152
  * ```
111
153
  */
112
154
  static logspace(min: number, max: number, count: number): Array<number>;
155
+ /**
156
+ * Compute the factorial of `n`.
157
+ * @param n Any positive integer
158
+ * @returns `n!`
159
+ * @example
160
+ * ```js
161
+ * const y = SMath.factorial(5); // 120
162
+ * ```
163
+ */
164
+ static factorial(n: number): number;
165
+ /**
166
+ * Calculate the relative normalized error or deviation from any
167
+ * value to an accepted value. An error of 0 indicates that the
168
+ * two values are identical. An error of -0.1 indicates that the
169
+ * experimental value is 10% smaller than (90% of) the accepted
170
+ * value. An error of 1.0 indicates that the experimental value
171
+ * is 100% greater (or twice the size) of the accepted value.
172
+ * @param experimental The value observed or produced by a test
173
+ * @param actual The accepted or theoretical value
174
+ * @returns The relative (normalized) error
175
+ * @example
176
+ * ```js
177
+ * const e = SMath.error(22.5, 25); // -0.1
178
+ * ```
179
+ */
180
+ static error(experimental: number, actual: number): number;
113
181
  }