smath 1.3.5 → 1.4.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/1711423325)
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.4.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
@@ -43,6 +43,7 @@ if (args.length < 1 || args[0].includes('help')) {
43
43
  console.log(' : Normalize `n` between `min` and `max`');
44
44
  console.log(' translate <n> <min1> <max1> <min2> <max2>');
45
45
  console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`');
46
+ console.log(' error <exp> <act> : Calculate the normaized percent error between `exp` and `act`');
46
47
  process.exit(1);
47
48
  }
48
49
  switch (args[0]) {
@@ -86,6 +87,10 @@ switch (args[0]) {
86
87
  console.log(_1.SMath.translate(N(1), N(2), N(3), N(4), N(5)));
87
88
  break;
88
89
  }
90
+ case ('error'): {
91
+ console.log(_1.SMath.error(N(1), N(2)));
92
+ break;
93
+ }
89
94
  default: {
90
95
  console.error('Unknown argument "' + args[0] + '". Use with "help" for a list of commands.');
91
96
  process.exit(1);
package/dist/index.js CHANGED
@@ -14,6 +14,40 @@ 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 sum = SMath.sum(1, 2, 3); // 6
25
+ * ```
26
+ */
27
+ SMath.sum = function () {
28
+ var n = [];
29
+ for (var _i = 0; _i < arguments.length; _i++) {
30
+ n[_i] = arguments[_i];
31
+ }
32
+ return n.reduce(function (a, b) { return a + b; }, 0);
33
+ };
34
+ /**
35
+ * Multiply all the inputs.
36
+ * If none are present, returns 1.
37
+ * @param n Any amount of numeric inputs
38
+ * @returns The product
39
+ * @example
40
+ * ```js
41
+ * const prod = SMath.prod(2, 2, 3, 5); // 60
42
+ * ```
43
+ */
44
+ SMath.prod = function () {
45
+ var n = [];
46
+ for (var _i = 0; _i < arguments.length; _i++) {
47
+ n[_i] = arguments[_i];
48
+ }
49
+ return n.reduce(function (a, b) { return a * b; }, 1);
50
+ };
17
51
  /**
18
52
  * Compute the average, or mean, of a set of numbers.
19
53
  * @param n Any amount of numeric inputs
@@ -28,7 +62,41 @@ var SMath = /** @class */ (function () {
28
62
  for (var _i = 0; _i < arguments.length; _i++) {
29
63
  n[_i] = arguments[_i];
30
64
  }
31
- return n.reduce(function (prev, curr) { return prev + curr; }) / n.length;
65
+ return this.sum.apply(this, n) / n.length;
66
+ };
67
+ /**
68
+ * Compute the variance of a **complete population**.
69
+ * @param n Any amount of numeric inputs
70
+ * @returns The population variance
71
+ * @example
72
+ * ```js
73
+ * const pvar = SMath.pvar(1, 2, 3, 4); // 1.25
74
+ * ```
75
+ */
76
+ SMath.pvar = function () {
77
+ var n = [];
78
+ for (var _i = 0; _i < arguments.length; _i++) {
79
+ n[_i] = arguments[_i];
80
+ }
81
+ var mean = this.avg.apply(this, n), squares = n.map(function (x) { return Math.pow((x - mean), 2); });
82
+ return this.sum.apply(this, squares) / n.length;
83
+ };
84
+ /**
85
+ * Compute the variance of a **sample**.
86
+ * @param n Any amount of numeric inputs
87
+ * @returns The sample variance
88
+ * @example
89
+ * ```js
90
+ * const svar = SMath.svar(1, 2, 3, 4); // 1.666...
91
+ * ```
92
+ */
93
+ SMath.svar = function () {
94
+ var n = [];
95
+ for (var _i = 0; _i < arguments.length; _i++) {
96
+ n[_i] = arguments[_i];
97
+ }
98
+ var mean = this.avg.apply(this, n), squares = n.map(function (x) { return Math.pow((x - mean), 2); });
99
+ return this.sum.apply(this, squares) / (n.length - 1);
32
100
  };
33
101
  /**
34
102
  * Clamp a number within a range.
@@ -149,6 +217,47 @@ var SMath = /** @class */ (function () {
149
217
  SMath.logspace = function (min, max, count) {
150
218
  return this.linspace(min, max, count).map(function (n) { return Math.pow(10, n); });
151
219
  };
220
+ /**
221
+ * Compute the factorial of `n`.
222
+ * @param n Any positive integer
223
+ * @returns `n!`
224
+ * @example
225
+ * ```js
226
+ * const factorial = SMath.factorial(5); // 120
227
+ * ```
228
+ */
229
+ SMath.factorial = function (n) {
230
+ if (n < 0 || (n | 0) !== n) {
231
+ throw new Error('Input must be a positive integer.');
232
+ }
233
+ else if (n === 0) {
234
+ return 0;
235
+ }
236
+ else if (n <= 2) {
237
+ return n;
238
+ }
239
+ else {
240
+ return n * this.factorial(n - 1);
241
+ }
242
+ };
243
+ /**
244
+ * Calculate the relative normalized error or deviation from any
245
+ * value to an accepted value. An error of 0 indicates that the
246
+ * two values are identical. An error of -0.1 indicates that the
247
+ * experimental value is 10% smaller than (90% of) the accepted
248
+ * value. An error of 1.0 indicates that the experimental value
249
+ * is 100% greater (or twice the size) of the accepted value.
250
+ * @param experimental The value observed or produced by a test
251
+ * @param actual The accepted or theoretical value
252
+ * @returns The relative (normalized) error
253
+ * @example
254
+ * ```js
255
+ * const error = SMath.error(22.5, 25); // -0.1
256
+ * ```
257
+ */
258
+ SMath.error = function (experimental, actual) {
259
+ return (experimental - actual) / actual;
260
+ };
152
261
  return SMath;
153
262
  }());
154
263
  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.4.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.1",
52
52
  "typedoc": "0.25.12",
53
53
  "typescript": "5.4.3"
54
54
  }
package/types/index.d.ts CHANGED
@@ -9,6 +9,28 @@
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 sum = 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 prod = 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
@@ -19,6 +41,26 @@ export declare abstract class SMath {
19
41
  * ```
20
42
  */
21
43
  static avg(...n: Array<number>): number;
44
+ /**
45
+ * Compute the variance of a **complete population**.
46
+ * @param n Any amount of numeric inputs
47
+ * @returns The population variance
48
+ * @example
49
+ * ```js
50
+ * const pvar = SMath.pvar(1, 2, 3, 4); // 1.25
51
+ * ```
52
+ */
53
+ static pvar(...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 svar = SMath.svar(1, 2, 3, 4); // 1.666...
61
+ * ```
62
+ */
63
+ static svar(...n: Array<number>): number;
22
64
  /**
23
65
  * Clamp a number within a range.
24
66
  * @param n The number to clamp
@@ -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 factorial = 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 error = SMath.error(22.5, 25); // -0.1
178
+ * ```
179
+ */
180
+ static error(experimental: number, actual: number): number;
113
181
  }