smath 1.4.0 → 1.5.1

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/1711423325)
5
+ ![Relative date](https://img.shields.io/date/1711486263)
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.4.0
15
+ npm i smath@1.5.1
16
16
  ```
17
17
 
18
18
  ## Bugs and Requests
package/dist/bin.js CHANGED
@@ -1,98 +1,112 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
+ var _a, _b;
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 = ((_a = process.argv[2]) !== null && _a !== void 0 ? _a : '').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(' stdevp <c0> [c1] ... [cn]: Compute the population standard deviation of `n` numbers');
26
+ console.log(' stdevs <c0> [c1] ... [cn]: Compute the sample standard deviation of `n` numbers');
27
+ console.log(' approx <a> <b> [eps] : Check if `a` and `b` are approximately equal');
38
28
  console.log(' clamp <n> <min> <max> : Clamp `n` between `min` and `max`');
39
29
  console.log(' expand <n> <min> <max> : Expand normalized `n` between `min` and `max`');
40
30
  console.log(' linspace <min> <max> <n> : Generate `n` linearly spaced numbers between `min` and `max`');
41
31
  console.log(' logspace <min> <max> <n> : Generate `n` logarithmically spaced numbers between `min` and `max`');
42
- console.log(' normalize <n> <min> <max>');
43
- console.log(' : Normalize `n` between `min` and `max`');
32
+ console.log(' normalize <n> <min> <max>: Normalize `n` between `min` and `max`');
44
33
  console.log(' translate <n> <min1> <max1> <min2> <max2>');
45
34
  console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`');
35
+ console.log(' factorial <n> : Compute `n!` (factorial)');
46
36
  console.log(' error <exp> <act> : Calculate the normaized percent error between `exp` and `act`');
47
37
  process.exit(1);
48
38
  }
49
- switch (args[0]) {
50
- case ('approx'): {
51
- console.log(_1.SMath.approx(N(1), N(2), N(3, 1e-6)));
39
+ switch (func) {
40
+ case ('sum'): {
41
+ console.log(_1.SMath.sum(nums));
42
+ break;
43
+ }
44
+ case ('prod'): {
45
+ console.log(_1.SMath.prod(nums));
52
46
  break;
53
47
  }
54
48
  case ('avg'): {
55
- if (args.length < 2) {
56
- console.error('Need at least 1 argument.');
57
- process.exit(1);
58
- }
59
- var operands = [];
60
- for (var i = 1; i < args.length; i++) {
61
- operands.push(N(i));
62
- }
63
- console.log(_1.SMath.avg.apply(_1.SMath, operands));
49
+ console.log(_1.SMath.avg(nums));
50
+ break;
51
+ }
52
+ case ('varp'): {
53
+ console.log(_1.SMath.varp(nums));
54
+ break;
55
+ }
56
+ case ('vars'): {
57
+ console.log(_1.SMath.vars(nums));
58
+ break;
59
+ }
60
+ case ('stdevp'): {
61
+ console.log(_1.SMath.stdevp(nums));
62
+ break;
63
+ }
64
+ case ('stdevs'): {
65
+ console.log(_1.SMath.stdevs(nums));
66
+ break;
67
+ }
68
+ case ('approx'): {
69
+ console.log(_1.SMath.approx(nums[0], nums[1], (_b = nums[2]) !== null && _b !== void 0 ? _b : 1e-6));
64
70
  break;
65
71
  }
66
72
  case ('clamp'): {
67
- console.log(_1.SMath.clamp(N(1), N(2), N(3)));
73
+ console.log(_1.SMath.clamp(nums[0], nums[1], nums[2]));
68
74
  break;
69
75
  }
70
76
  case ('expand'): {
71
- console.log(_1.SMath.expand(N(1), N(2), N(3)));
77
+ console.log(_1.SMath.expand(nums[0], nums[1], nums[2]));
72
78
  break;
73
79
  }
74
80
  case ('linspace'): {
75
- console.log(_1.SMath.linspace(N(1), N(2), N(3)));
81
+ console.log(_1.SMath.linspace(nums[0], nums[1], nums[2]));
76
82
  break;
77
83
  }
78
84
  case ('logspace'): {
79
- console.log(_1.SMath.logspace(N(1), N(2), N(3)));
85
+ console.log(_1.SMath.logspace(nums[0], nums[1], nums[2]));
80
86
  break;
81
87
  }
82
88
  case ('normalize'): {
83
- console.log(_1.SMath.normalize(N(1), N(2), N(3)));
89
+ console.log(_1.SMath.normalize(nums[0], nums[1], nums[2]));
84
90
  break;
85
91
  }
86
92
  case ('translate'): {
87
- console.log(_1.SMath.translate(N(1), N(2), N(3), N(4), N(5)));
93
+ console.log(_1.SMath.translate(nums[0], nums[1], nums[2], nums[3], nums[4]));
94
+ break;
95
+ }
96
+ case ('factorial'): {
97
+ console.log(_1.SMath.factorial(nums[0]));
88
98
  break;
89
99
  }
90
100
  case ('error'): {
91
- console.log(_1.SMath.error(N(1), N(2)));
101
+ console.log(_1.SMath.error(nums[0], nums[1]));
92
102
  break;
93
103
  }
104
+ case (''): {
105
+ console.error('Missing argument. Use with "help" for a list of commands.');
106
+ process.exit(1);
107
+ }
94
108
  default: {
95
- console.error('Unknown argument "' + args[0] + '". Use with "help" for a list of commands.');
109
+ console.error('Unknown argument "' + func + '". Use with "help" for a list of commands.');
96
110
  process.exit(1);
97
111
  }
98
112
  }
package/dist/index.js CHANGED
@@ -17,86 +17,98 @@ var SMath = /** @class */ (function () {
17
17
  /**
18
18
  * Add up all the inputs.
19
19
  * If none are present, returns 0.
20
- * @param n Any amount of numeric inputs
20
+ * @param n An array of numeric inputs
21
21
  * @returns The sum total
22
22
  * @example
23
23
  * ```js
24
- * const sum = SMath.sum(1, 2, 3); // 6
24
+ * const y = SMath.sum([1, 2, 3]); // 6
25
25
  * ```
26
26
  */
27
- SMath.sum = function () {
28
- var n = [];
29
- for (var _i = 0; _i < arguments.length; _i++) {
30
- n[_i] = arguments[_i];
31
- }
27
+ SMath.sum = function (n) {
32
28
  return n.reduce(function (a, b) { return a + b; }, 0);
33
29
  };
34
30
  /**
35
31
  * Multiply all the inputs.
36
32
  * If none are present, returns 1.
37
- * @param n Any amount of numeric inputs
33
+ * @param n An array of numeric inputs
38
34
  * @returns The product
39
35
  * @example
40
36
  * ```js
41
- * const prod = SMath.prod(2, 2, 3, 5); // 60
37
+ * const y = SMath.prod([2, 2, 3, 5]); // 60
42
38
  * ```
43
39
  */
44
- SMath.prod = function () {
45
- var n = [];
46
- for (var _i = 0; _i < arguments.length; _i++) {
47
- n[_i] = arguments[_i];
48
- }
40
+ SMath.prod = function (n) {
49
41
  return n.reduce(function (a, b) { return a * b; }, 1);
50
42
  };
51
43
  /**
52
44
  * Compute the average, or mean, of a set of numbers.
53
- * @param n Any amount of numeric inputs
45
+ * @param n An array of numeric inputs
54
46
  * @returns The average, or mean
55
47
  * @example
56
48
  * ```js
57
- * const mean = SMath.avg(1, 2, 3, 4); // 2.5
49
+ * const y = SMath.avg([1, 2, 4, 4]); // 2.75
58
50
  * ```
59
51
  */
60
- SMath.avg = function () {
61
- var n = [];
62
- for (var _i = 0; _i < arguments.length; _i++) {
63
- n[_i] = arguments[_i];
64
- }
65
- return this.sum.apply(this, n) / n.length;
52
+ SMath.avg = function (n) {
53
+ return this.sum(n) / n.length;
66
54
  };
67
55
  /**
68
56
  * Compute the variance of a **complete population**.
69
- * @param n Any amount of numeric inputs
57
+ * @param n An array of numeric inputs
70
58
  * @returns The population variance
71
59
  * @example
72
60
  * ```js
73
- * const pvar = SMath.pvar(1, 2, 3, 4); // 1.25
61
+ * const y = SMath.varp([1, 2, 4, 4]); // 1.6875
74
62
  * ```
75
63
  */
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;
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;
83
67
  };
84
68
  /**
85
69
  * Compute the variance of a **sample**.
86
- * @param n Any amount of numeric inputs
70
+ * @param n An array of numeric inputs
87
71
  * @returns The sample variance
88
72
  * @example
89
73
  * ```js
90
- * const svar = SMath.svar(1, 2, 3, 4); // 1.666...
74
+ * const y = SMath.vars([1, 2, 4, 4]); // 2.25
91
75
  * ```
92
76
  */
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);
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);
80
+ };
81
+ /**
82
+ * Compute the standard deviation of a **complete population**.
83
+ * @param n An array of numeric inputs
84
+ * @returns The population standard deviation
85
+ */
86
+ SMath.stdevp = function (n) {
87
+ return Math.sqrt(this.varp(n));
88
+ };
89
+ /**
90
+ * Compute the standard deviation of a **sample**.
91
+ * @param n An array of numeric inputs
92
+ * @returns The sample standard deviation
93
+ */
94
+ SMath.stdevs = function (n) {
95
+ return Math.sqrt(this.vars(n));
96
+ };
97
+ /**
98
+ * Check if two numbers are approximately equal with a maximum abolute error.
99
+ * @param a Any number
100
+ * @param b Any number
101
+ * @param epsilon Maximum absolute error
102
+ * @returns True if `a` is approximately `b`
103
+ * @example
104
+ * ```js
105
+ * const b1 = SMath.approx(1 / 3, 0.33, 1e-6), // false
106
+ * b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
107
+ * ```
108
+ */
109
+ SMath.approx = function (a, b, epsilon) {
110
+ if (epsilon === void 0) { epsilon = 1e-6; }
111
+ return a - b < epsilon && b - a < epsilon;
100
112
  };
101
113
  /**
102
114
  * Clamp a number within a range.
@@ -119,22 +131,6 @@ var SMath = /** @class */ (function () {
119
131
  }
120
132
  return n;
121
133
  };
122
- /**
123
- * Check if two numbers are approximately equal with a maximum abolute error.
124
- * @param a Any number
125
- * @param b Any number
126
- * @param epsilon Maximum absolute error
127
- * @returns True if `a` is approximately `b`
128
- * @example
129
- * ```js
130
- * const b1 = SMath.approx(1 / 3, 0.33, 1e-6), // false
131
- * b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
132
- * ```
133
- */
134
- SMath.approx = function (a, b, epsilon) {
135
- if (epsilon === void 0) { epsilon = 1e-6; }
136
- return a - b < epsilon && b - a < epsilon;
137
- };
138
134
  /**
139
135
  * Normalize the number `n` from the range `min, max` to the range `0, 1`
140
136
  * @param n The number to normalize
@@ -223,7 +219,7 @@ var SMath = /** @class */ (function () {
223
219
  * @returns `n!`
224
220
  * @example
225
221
  * ```js
226
- * const factorial = SMath.factorial(5); // 120
222
+ * const y = SMath.factorial(5); // 120
227
223
  * ```
228
224
  */
229
225
  SMath.factorial = function (n) {
@@ -231,7 +227,7 @@ var SMath = /** @class */ (function () {
231
227
  throw new Error('Input must be a positive integer.');
232
228
  }
233
229
  else if (n === 0) {
234
- return 0;
230
+ return 1;
235
231
  }
236
232
  else if (n <= 2) {
237
233
  return n;
@@ -252,7 +248,7 @@ var SMath = /** @class */ (function () {
252
248
  * @returns The relative (normalized) error
253
249
  * @example
254
250
  * ```js
255
- * const error = SMath.error(22.5, 25); // -0.1
251
+ * const e = SMath.error(22.5, 25); // -0.1
256
252
  * ```
257
253
  */
258
254
  SMath.error = function (experimental, actual) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "1.4.0",
3
+ "version": "1.5.1",
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.1",
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
@@ -12,68 +12,67 @@ export declare abstract class SMath {
12
12
  /**
13
13
  * Add up all the inputs.
14
14
  * If none are present, returns 0.
15
- * @param n Any amount of numeric inputs
15
+ * @param n An array of numeric inputs
16
16
  * @returns The sum total
17
17
  * @example
18
18
  * ```js
19
- * const sum = SMath.sum(1, 2, 3); // 6
19
+ * const y = SMath.sum([1, 2, 3]); // 6
20
20
  * ```
21
21
  */
22
- static sum(...n: Array<number>): number;
22
+ static sum(n: Array<number>): number;
23
23
  /**
24
24
  * Multiply all the inputs.
25
25
  * If none are present, returns 1.
26
- * @param n Any amount of numeric inputs
26
+ * @param n An array of numeric inputs
27
27
  * @returns The product
28
28
  * @example
29
29
  * ```js
30
- * const prod = SMath.prod(2, 2, 3, 5); // 60
30
+ * const y = SMath.prod([2, 2, 3, 5]); // 60
31
31
  * ```
32
32
  */
33
- static prod(...n: Array<number>): number;
33
+ static prod(n: Array<number>): number;
34
34
  /**
35
35
  * Compute the average, or mean, of a set of numbers.
36
- * @param n Any amount of numeric inputs
36
+ * @param n An array of numeric inputs
37
37
  * @returns The average, or mean
38
38
  * @example
39
39
  * ```js
40
- * const mean = SMath.avg(1, 2, 3, 4); // 2.5
40
+ * const y = SMath.avg([1, 2, 4, 4]); // 2.75
41
41
  * ```
42
42
  */
43
- static avg(...n: Array<number>): number;
43
+ static avg(n: Array<number>): number;
44
44
  /**
45
45
  * Compute the variance of a **complete population**.
46
- * @param n Any amount of numeric inputs
46
+ * @param n An array of numeric inputs
47
47
  * @returns The population variance
48
48
  * @example
49
49
  * ```js
50
- * const pvar = SMath.pvar(1, 2, 3, 4); // 1.25
50
+ * const y = SMath.varp([1, 2, 4, 4]); // 1.6875
51
51
  * ```
52
52
  */
53
- static pvar(...n: Array<number>): number;
53
+ static varp(n: Array<number>): number;
54
54
  /**
55
55
  * Compute the variance of a **sample**.
56
- * @param n Any amount of numeric inputs
56
+ * @param n An array of numeric inputs
57
57
  * @returns The sample variance
58
58
  * @example
59
59
  * ```js
60
- * const svar = SMath.svar(1, 2, 3, 4); // 1.666...
60
+ * const y = SMath.vars([1, 2, 4, 4]); // 2.25
61
61
  * ```
62
62
  */
63
- static svar(...n: Array<number>): number;
63
+ static vars(n: Array<number>): number;
64
64
  /**
65
- * Clamp a number within a range.
66
- * @param n The number to clamp
67
- * @param min The minimum value of the range
68
- * @param max The maximum value of the range
69
- * @returns A clamped number
70
- * @example
71
- * ```js
72
- * const n1 = SMath.clamp(5, 0, 10), // 5
73
- * n2 = SMath.clamp(-2, 0, 10); // 0
74
- * ```
65
+ * Compute the standard deviation of a **complete population**.
66
+ * @param n An array of numeric inputs
67
+ * @returns The population standard deviation
75
68
  */
76
- static clamp(n: number, min: number, max: number): number;
69
+ static stdevp(n: Array<number>): number;
70
+ /**
71
+ * Compute the standard deviation of a **sample**.
72
+ * @param n An array of numeric inputs
73
+ * @returns The sample standard deviation
74
+ */
75
+ static stdevs(n: Array<number>): number;
77
76
  /**
78
77
  * Check if two numbers are approximately equal with a maximum abolute error.
79
78
  * @param a Any number
@@ -87,6 +86,19 @@ export declare abstract class SMath {
87
86
  * ```
88
87
  */
89
88
  static approx(a: number, b: number, epsilon?: number): boolean;
89
+ /**
90
+ * Clamp a number within a range.
91
+ * @param n The number to clamp
92
+ * @param min The minimum value of the range
93
+ * @param max The maximum value of the range
94
+ * @returns A clamped number
95
+ * @example
96
+ * ```js
97
+ * const n1 = SMath.clamp(5, 0, 10), // 5
98
+ * n2 = SMath.clamp(-2, 0, 10); // 0
99
+ * ```
100
+ */
101
+ static clamp(n: number, min: number, max: number): number;
90
102
  /**
91
103
  * Normalize the number `n` from the range `min, max` to the range `0, 1`
92
104
  * @param n The number to normalize
@@ -158,7 +170,7 @@ export declare abstract class SMath {
158
170
  * @returns `n!`
159
171
  * @example
160
172
  * ```js
161
- * const factorial = SMath.factorial(5); // 120
173
+ * const y = SMath.factorial(5); // 120
162
174
  * ```
163
175
  */
164
176
  static factorial(n: number): number;
@@ -174,7 +186,7 @@ export declare abstract class SMath {
174
186
  * @returns The relative (normalized) error
175
187
  * @example
176
188
  * ```js
177
- * const error = SMath.error(22.5, 25); // -0.1
189
+ * const e = SMath.error(22.5, 25); // -0.1
178
190
  * ```
179
191
  */
180
192
  static error(experimental: number, actual: number): number;