smath 1.4.0 → 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/1711423325)
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.4.0
15
+ npm i smath@1.5.0
16
16
  ```
17
17
 
18
18
  ## Bugs and Requests
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,56 +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)');
46
35
  console.log(' error <exp> <act> : Calculate the normaized percent error between `exp` and `act`');
47
36
  process.exit(1);
48
37
  }
49
- switch (args[0]) {
50
- case ('approx'): {
51
- 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));
52
45
  break;
53
46
  }
54
47
  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));
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));
64
61
  break;
65
62
  }
66
63
  case ('clamp'): {
67
- console.log(_1.SMath.clamp(N(1), N(2), N(3)));
64
+ console.log(_1.SMath.clamp(nums[0], nums[1], nums[2]));
68
65
  break;
69
66
  }
70
67
  case ('expand'): {
71
- console.log(_1.SMath.expand(N(1), N(2), N(3)));
68
+ console.log(_1.SMath.expand(nums[0], nums[1], nums[2]));
72
69
  break;
73
70
  }
74
71
  case ('linspace'): {
75
- console.log(_1.SMath.linspace(N(1), N(2), N(3)));
72
+ console.log(_1.SMath.linspace(nums[0], nums[1], nums[2]));
76
73
  break;
77
74
  }
78
75
  case ('logspace'): {
79
- console.log(_1.SMath.logspace(N(1), N(2), N(3)));
76
+ console.log(_1.SMath.logspace(nums[0], nums[1], nums[2]));
80
77
  break;
81
78
  }
82
79
  case ('normalize'): {
83
- console.log(_1.SMath.normalize(N(1), N(2), N(3)));
80
+ console.log(_1.SMath.normalize(nums[0], nums[1], nums[2]));
84
81
  break;
85
82
  }
86
83
  case ('translate'): {
87
- 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]));
88
89
  break;
89
90
  }
90
91
  case ('error'): {
91
- console.log(_1.SMath.error(N(1), N(2)));
92
+ console.log(_1.SMath.error(nums[0], nums[1]));
92
93
  break;
93
94
  }
94
95
  default: {
95
- 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.');
96
97
  process.exit(1);
97
98
  }
98
99
  }
package/dist/index.js CHANGED
@@ -21,14 +21,10 @@ var SMath = /** @class */ (function () {
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
  /**
@@ -38,14 +34,10 @@ var SMath = /** @class */ (function () {
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
  /**
@@ -54,15 +46,11 @@ var SMath = /** @class */ (function () {
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**.
@@ -70,16 +58,12 @@ var SMath = /** @class */ (function () {
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**.
@@ -87,16 +71,28 @@ var SMath = /** @class */ (function () {
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
+ * Check if two numbers are approximately equal with a maximum abolute error.
83
+ * @param a Any number
84
+ * @param b Any number
85
+ * @param epsilon Maximum absolute error
86
+ * @returns True if `a` is approximately `b`
87
+ * @example
88
+ * ```js
89
+ * const b1 = SMath.approx(1 / 3, 0.33, 1e-6), // false
90
+ * b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
91
+ * ```
92
+ */
93
+ SMath.approx = function (a, b, epsilon) {
94
+ if (epsilon === void 0) { epsilon = 1e-6; }
95
+ return a - b < epsilon && b - a < epsilon;
100
96
  };
101
97
  /**
102
98
  * Clamp a number within a range.
@@ -119,22 +115,6 @@ var SMath = /** @class */ (function () {
119
115
  }
120
116
  return n;
121
117
  };
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
118
  /**
139
119
  * Normalize the number `n` from the range `min, max` to the range `0, 1`
140
120
  * @param n The number to normalize
@@ -223,7 +203,7 @@ var SMath = /** @class */ (function () {
223
203
  * @returns `n!`
224
204
  * @example
225
205
  * ```js
226
- * const factorial = SMath.factorial(5); // 120
206
+ * const y = SMath.factorial(5); // 120
227
207
  * ```
228
208
  */
229
209
  SMath.factorial = function (n) {
@@ -231,7 +211,7 @@ var SMath = /** @class */ (function () {
231
211
  throw new Error('Input must be a positive integer.');
232
212
  }
233
213
  else if (n === 0) {
234
- return 0;
214
+ return 1;
235
215
  }
236
216
  else if (n <= 2) {
237
217
  return n;
@@ -252,7 +232,7 @@ var SMath = /** @class */ (function () {
252
232
  * @returns The relative (normalized) error
253
233
  * @example
254
234
  * ```js
255
- * const error = SMath.error(22.5, 25); // -0.1
235
+ * const e = SMath.error(22.5, 25); // -0.1
256
236
  * ```
257
237
  */
258
238
  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.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.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
@@ -16,10 +16,10 @@ export declare abstract class SMath {
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.
@@ -27,53 +27,40 @@ export declare abstract class SMath {
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
36
  * @param n Any amount 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
46
  * @param n Any amount 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
56
  * @param n Any amount 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;
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
- * ```
75
- */
76
- static clamp(n: number, min: number, max: number): number;
63
+ static vars(n: Array<number>): number;
77
64
  /**
78
65
  * Check if two numbers are approximately equal with a maximum abolute error.
79
66
  * @param a Any number
@@ -87,6 +74,19 @@ export declare abstract class SMath {
87
74
  * ```
88
75
  */
89
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;
90
90
  /**
91
91
  * Normalize the number `n` from the range `min, max` to the range `0, 1`
92
92
  * @param n The number to normalize
@@ -158,7 +158,7 @@ export declare abstract class SMath {
158
158
  * @returns `n!`
159
159
  * @example
160
160
  * ```js
161
- * const factorial = SMath.factorial(5); // 120
161
+ * const y = SMath.factorial(5); // 120
162
162
  * ```
163
163
  */
164
164
  static factorial(n: number): number;
@@ -174,7 +174,7 @@ export declare abstract class SMath {
174
174
  * @returns The relative (normalized) error
175
175
  * @example
176
176
  * ```js
177
- * const error = SMath.error(22.5, 25); // -0.1
177
+ * const e = SMath.error(22.5, 25); // -0.1
178
178
  * ```
179
179
  */
180
180
  static error(experimental: number, actual: number): number;