smath 1.5.1 → 1.6.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/1711486263)
5
+ ![Relative date](https://img.shields.io/date/1711573124)
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.5.1
15
+ npm i smath@1.6.0
16
16
  ```
17
17
 
18
18
  ## Bugs and Requests
package/dist/bin.js CHANGED
@@ -17,13 +17,6 @@ if (func.includes('help')) {
17
17
  console.log('Key: <required> [optional]');
18
18
  console.log('Arguments:');
19
19
  console.log(' help : Show this page');
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');
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
20
  console.log(' approx <a> <b> [eps] : Check if `a` and `b` are approximately equal');
28
21
  console.log(' clamp <n> <min> <max> : Clamp `n` between `min` and `max`');
29
22
  console.log(' expand <n> <min> <max> : Expand normalized `n` between `min` and `max`');
@@ -34,37 +27,17 @@ if (func.includes('help')) {
34
27
  console.log(' : Linearly interpolate `n` from `min1`, `max1` to `min2`, `max2`');
35
28
  console.log(' factorial <n> : Compute `n!` (factorial)');
36
29
  console.log(' error <exp> <act> : Calculate the normaized percent error between `exp` and `act`');
30
+ console.log(' sum <c0> [c1] ... [cn] : Compute a total of `n` numbers');
31
+ console.log(' prod <c0> [c1] ... [cn] : Compute a product of `n` numbers');
32
+ console.log(' avg <c0> [c1] ... [cn] : Take an average of `n` numbers');
33
+ console.log(' median <c0> [c1] ... [cn]: Take the median of `n` numbers');
34
+ console.log(' varp <c0> [c1] ... [cn] : Compute the population variance of `n` numbers');
35
+ console.log(' vars <c0> [c1] ... [cn] : Compute the sample variance of `n` numbers');
36
+ console.log(' stdevp <c0> [c1] ... [cn]: Compute the population standard deviation of `n` numbers');
37
+ console.log(' stdevs <c0> [c1] ... [cn]: Compute the sample standard deviation of `n` numbers');
37
38
  process.exit(1);
38
39
  }
39
40
  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));
46
- break;
47
- }
48
- case ('avg'): {
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
41
  case ('approx'): {
69
42
  console.log(_1.SMath.approx(nums[0], nums[1], (_b = nums[2]) !== null && _b !== void 0 ? _b : 1e-6));
70
43
  break;
@@ -101,6 +74,38 @@ switch (func) {
101
74
  console.log(_1.SMath.error(nums[0], nums[1]));
102
75
  break;
103
76
  }
77
+ case ('sum'): {
78
+ console.log(_1.SMath.sum(nums));
79
+ break;
80
+ }
81
+ case ('prod'): {
82
+ console.log(_1.SMath.prod(nums));
83
+ break;
84
+ }
85
+ case ('avg'): {
86
+ console.log(_1.SMath.avg(nums));
87
+ break;
88
+ }
89
+ case ('median'): {
90
+ console.log(_1.SMath.median(nums));
91
+ break;
92
+ }
93
+ case ('varp'): {
94
+ console.log(_1.SMath.varp(nums));
95
+ break;
96
+ }
97
+ case ('vars'): {
98
+ console.log(_1.SMath.vars(nums));
99
+ break;
100
+ }
101
+ case ('stdevp'): {
102
+ console.log(_1.SMath.stdevp(nums));
103
+ break;
104
+ }
105
+ case ('stdevs'): {
106
+ console.log(_1.SMath.stdevs(nums));
107
+ break;
108
+ }
104
109
  case (''): {
105
110
  console.error('Missing argument. Use with "help" for a list of commands.');
106
111
  process.exit(1);
package/dist/index.js CHANGED
@@ -14,86 +14,6 @@ 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 An array 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 An array 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
- };
43
- /**
44
- * Compute the average, or mean, of a set of numbers.
45
- * @param n An array of numeric inputs
46
- * @returns The average, or mean
47
- * @example
48
- * ```js
49
- * const y = SMath.avg([1, 2, 4, 4]); // 2.75
50
- * ```
51
- */
52
- SMath.avg = function (n) {
53
- return this.sum(n) / n.length;
54
- };
55
- /**
56
- * Compute the variance of a **complete population**.
57
- * @param n An array of numeric inputs
58
- * @returns The population variance
59
- * @example
60
- * ```js
61
- * const y = SMath.varp([1, 2, 4, 4]); // 1.6875
62
- * ```
63
- */
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 An array 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);
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
17
  /**
98
18
  * Check if two numbers are approximately equal with a maximum abolute error.
99
19
  * @param a Any number
@@ -254,6 +174,215 @@ var SMath = /** @class */ (function () {
254
174
  SMath.error = function (experimental, actual) {
255
175
  return (experimental - actual) / actual;
256
176
  };
177
+ /**
178
+ * Add up all the inputs.
179
+ * If none are present, returns 0.
180
+ * @param data An array of numeric inputs
181
+ * @returns The sum total
182
+ * @example
183
+ * ```js
184
+ * const y = SMath.sum([1, 2, 3]); // 6
185
+ * ```
186
+ */
187
+ SMath.sum = function (data) {
188
+ return data.reduce(function (a, b) { return a + b; }, 0);
189
+ };
190
+ /**
191
+ * Multiply all the inputs.
192
+ * If none are present, returns 1.
193
+ * @param data An array of numeric inputs
194
+ * @returns The product
195
+ * @example
196
+ * ```js
197
+ * const y = SMath.prod([2, 2, 3, 5]); // 60
198
+ * ```
199
+ */
200
+ SMath.prod = function (data) {
201
+ return data.reduce(function (a, b) { return a * b; }, 1);
202
+ };
203
+ /**
204
+ * Compute the average, or mean, of a set of numbers.
205
+ * @param data An array of numeric inputs
206
+ * @returns The average, or mean
207
+ * @example
208
+ * ```js
209
+ * const y = SMath.avg([1, 2, 4, 4]); // 2.75
210
+ * ```
211
+ */
212
+ SMath.avg = function (data) {
213
+ return this.sum(data) / data.length;
214
+ };
215
+ /**
216
+ * Compute the median of a set of numbers.
217
+ * @param data An array of numeric inputs
218
+ * @returns The median of the dataset
219
+ * @example
220
+ * ```js
221
+ * const y = SMath.median([2, 5, 3, 1]); // 2.5
222
+ * ```
223
+ */
224
+ SMath.median = function (data) {
225
+ data.sort();
226
+ if (data.length % 2) {
227
+ return data[(data.length - 1) / 2];
228
+ }
229
+ return this.avg([data[data.length / 2 - 1], data[data.length / 2]]);
230
+ };
231
+ /**
232
+ * Compute the variance of a **complete population**.
233
+ * @param data An array of numeric inputs
234
+ * @returns The population variance
235
+ * @example
236
+ * ```js
237
+ * const y = SMath.varp([1, 2, 4, 4]); // 1.6875
238
+ * ```
239
+ */
240
+ SMath.varp = function (data) {
241
+ var mean = this.avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
242
+ return this.sum(squares) / data.length;
243
+ };
244
+ /**
245
+ * Compute the variance of a **sample**.
246
+ * @param data An array of numeric inputs
247
+ * @returns The sample variance
248
+ * @example
249
+ * ```js
250
+ * const y = SMath.vars([1, 2, 4, 4]); // 2.25
251
+ * ```
252
+ */
253
+ SMath.vars = function (data) {
254
+ var mean = this.avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
255
+ return this.sum(squares) / (data.length - 1);
256
+ };
257
+ /**
258
+ * Compute the standard deviation of a **complete population**.
259
+ * @param data An array of numeric inputs
260
+ * @returns The population standard deviation
261
+ * @example
262
+ * ```js
263
+ * const y = SMath.stdevp([1, 2, 3, 4]); // 1.118...
264
+ * ```
265
+ */
266
+ SMath.stdevp = function (data) {
267
+ return Math.sqrt(this.varp(data));
268
+ };
269
+ /**
270
+ * Compute the standard deviation of a **sample**.
271
+ * @param data An array of numeric inputs
272
+ * @returns The sample standard deviation
273
+ * @example
274
+ * ```js
275
+ * const y = SMath.stdevs([1, 2, 3, 4]); // 1.29...
276
+ * ```
277
+ */
278
+ SMath.stdevs = function (data) {
279
+ return Math.sqrt(this.vars(data));
280
+ };
281
+ /**
282
+ * Take the limit of a function. A return value of `NaN` indicates
283
+ * that no limit exists either due to a discontinuity or imaginary value.
284
+ * @param f Function `f(x)`
285
+ * @param x The x-value where to take the limit
286
+ * @param h The approach distance
287
+ * @param discontinuity_cutoff The discontinuity cutoff
288
+ * @returns `lim(f(x->x))`
289
+ * @example
290
+ * ```js
291
+ * const y = SMath.lim(Math.log, 0); // -Infinity
292
+ * ```
293
+ */
294
+ SMath.lim = function (f, x, h, discontinuity_cutoff) {
295
+ if (h === void 0) { h = 1e-3; }
296
+ if (discontinuity_cutoff === void 0) { discontinuity_cutoff = 1; }
297
+ var center = f(x), left1 = f(x - h), left2 = f(x - h / 2), right1 = f(x + h), right2 = f(x + h / 2);
298
+ var left, right;
299
+ if (Number.isFinite(center)) {
300
+ return center;
301
+ }
302
+ // Check the limit approaching from the left
303
+ if (Number.isFinite(left1) && Number.isFinite(left2)) {
304
+ if (left2 > left1 + 2 * h) {
305
+ left = Infinity;
306
+ }
307
+ else if (left2 < left1 - 2 * h) {
308
+ left = -Infinity;
309
+ }
310
+ else {
311
+ left = this.avg([left1, left2]);
312
+ }
313
+ }
314
+ else if (left1 === left2) { // Handles +/-Infinity case
315
+ left = left1;
316
+ }
317
+ else {
318
+ left = NaN;
319
+ }
320
+ // Check the limit approaching from the right
321
+ if (Number.isFinite(right1) && Number.isFinite(right2)) {
322
+ if (right2 > right1 + 2 * h) {
323
+ right = Infinity;
324
+ }
325
+ else if (right2 < right1 - 2 * h) {
326
+ right = -Infinity;
327
+ }
328
+ else {
329
+ right = this.avg([right1, right2]);
330
+ }
331
+ }
332
+ else if (right1 === right2) { // Handles +/-Infinity case
333
+ right = right1;
334
+ }
335
+ else {
336
+ right = NaN;
337
+ }
338
+ // Check if limits match or are close
339
+ if (left === right) { // Handles +/-Infinity case
340
+ return left;
341
+ }
342
+ else if (SMath.approx(left, right, discontinuity_cutoff)) {
343
+ return this.avg([left, right]);
344
+ }
345
+ else if (!Number.isNaN(left) && Number.isNaN(right)) {
346
+ return left;
347
+ }
348
+ else if (Number.isNaN(left) && !Number.isNaN(right)) {
349
+ return right;
350
+ }
351
+ else {
352
+ return NaN;
353
+ }
354
+ };
355
+ /**
356
+ * Take the derivative of a function.
357
+ * @param f Function `f(x)`
358
+ * @param x The x-value where to evaluate the derivative
359
+ * @param h Small step value
360
+ * @returns `f'(x)`
361
+ * @example
362
+ * ```js
363
+ * const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
364
+ * ```
365
+ */
366
+ SMath.differentiate = function (f, x, h) {
367
+ if (h === void 0) { h = 1e-3; }
368
+ return (f(x + h) - f(x - h)) / (2 * h);
369
+ };
370
+ /**
371
+ * Compute the definite integral of a function.
372
+ * @param f Function `f(x)`
373
+ * @param a The miminum integral bound
374
+ * @param b The maximum integral bound
375
+ * @param Ndx The number of rectangles to compute
376
+ * @returns `F(b)-F(a)`
377
+ * @example
378
+ * ```js
379
+ * const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
380
+ * ```
381
+ */
382
+ SMath.integrate = function (f, a, b, Ndx) {
383
+ if (Ndx === void 0) { Ndx = 1e3; }
384
+ return ((b - a) / Ndx) * this.sum(this.linspace(a, b, Ndx).map(function (x) { return f(x); }));
385
+ };
257
386
  return SMath;
258
387
  }());
259
388
  exports.SMath = SMath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "description": "Small math function library",
5
5
  "homepage": "https://npm.nicfv.com/smath",
6
6
  "bin": "dist/bin.js",
package/types/index.d.ts CHANGED
@@ -9,70 +9,6 @@
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 An array 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 An array 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;
34
- /**
35
- * Compute the average, or mean, of a set of numbers.
36
- * @param n An array of numeric inputs
37
- * @returns The average, or mean
38
- * @example
39
- * ```js
40
- * const y = SMath.avg([1, 2, 4, 4]); // 2.75
41
- * ```
42
- */
43
- static avg(n: Array<number>): number;
44
- /**
45
- * Compute the variance of a **complete population**.
46
- * @param n An array of numeric inputs
47
- * @returns The population variance
48
- * @example
49
- * ```js
50
- * const y = SMath.varp([1, 2, 4, 4]); // 1.6875
51
- * ```
52
- */
53
- static varp(n: Array<number>): number;
54
- /**
55
- * Compute the variance of a **sample**.
56
- * @param n An array 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;
64
- /**
65
- * Compute the standard deviation of a **complete population**.
66
- * @param n An array of numeric inputs
67
- * @returns The population standard deviation
68
- */
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;
76
12
  /**
77
13
  * Check if two numbers are approximately equal with a maximum abolute error.
78
14
  * @param a Any number
@@ -190,4 +126,125 @@ export declare abstract class SMath {
190
126
  * ```
191
127
  */
192
128
  static error(experimental: number, actual: number): number;
129
+ /**
130
+ * Add up all the inputs.
131
+ * If none are present, returns 0.
132
+ * @param data An array of numeric inputs
133
+ * @returns The sum total
134
+ * @example
135
+ * ```js
136
+ * const y = SMath.sum([1, 2, 3]); // 6
137
+ * ```
138
+ */
139
+ static sum(data: Array<number>): number;
140
+ /**
141
+ * Multiply all the inputs.
142
+ * If none are present, returns 1.
143
+ * @param data An array of numeric inputs
144
+ * @returns The product
145
+ * @example
146
+ * ```js
147
+ * const y = SMath.prod([2, 2, 3, 5]); // 60
148
+ * ```
149
+ */
150
+ static prod(data: Array<number>): number;
151
+ /**
152
+ * Compute the average, or mean, of a set of numbers.
153
+ * @param data An array of numeric inputs
154
+ * @returns The average, or mean
155
+ * @example
156
+ * ```js
157
+ * const y = SMath.avg([1, 2, 4, 4]); // 2.75
158
+ * ```
159
+ */
160
+ static avg(data: Array<number>): number;
161
+ /**
162
+ * Compute the median of a set of numbers.
163
+ * @param data An array of numeric inputs
164
+ * @returns The median of the dataset
165
+ * @example
166
+ * ```js
167
+ * const y = SMath.median([2, 5, 3, 1]); // 2.5
168
+ * ```
169
+ */
170
+ static median(data: Array<number>): number;
171
+ /**
172
+ * Compute the variance of a **complete population**.
173
+ * @param data An array of numeric inputs
174
+ * @returns The population variance
175
+ * @example
176
+ * ```js
177
+ * const y = SMath.varp([1, 2, 4, 4]); // 1.6875
178
+ * ```
179
+ */
180
+ static varp(data: Array<number>): number;
181
+ /**
182
+ * Compute the variance of a **sample**.
183
+ * @param data An array of numeric inputs
184
+ * @returns The sample variance
185
+ * @example
186
+ * ```js
187
+ * const y = SMath.vars([1, 2, 4, 4]); // 2.25
188
+ * ```
189
+ */
190
+ static vars(data: Array<number>): number;
191
+ /**
192
+ * Compute the standard deviation of a **complete population**.
193
+ * @param data An array of numeric inputs
194
+ * @returns The population standard deviation
195
+ * @example
196
+ * ```js
197
+ * const y = SMath.stdevp([1, 2, 3, 4]); // 1.118...
198
+ * ```
199
+ */
200
+ static stdevp(data: Array<number>): number;
201
+ /**
202
+ * Compute the standard deviation of a **sample**.
203
+ * @param data An array of numeric inputs
204
+ * @returns The sample standard deviation
205
+ * @example
206
+ * ```js
207
+ * const y = SMath.stdevs([1, 2, 3, 4]); // 1.29...
208
+ * ```
209
+ */
210
+ static stdevs(data: Array<number>): number;
211
+ /**
212
+ * Take the limit of a function. A return value of `NaN` indicates
213
+ * that no limit exists either due to a discontinuity or imaginary value.
214
+ * @param f Function `f(x)`
215
+ * @param x The x-value where to take the limit
216
+ * @param h The approach distance
217
+ * @param discontinuity_cutoff The discontinuity cutoff
218
+ * @returns `lim(f(x->x))`
219
+ * @example
220
+ * ```js
221
+ * const y = SMath.lim(Math.log, 0); // -Infinity
222
+ * ```
223
+ */
224
+ static lim(f: (x: number) => number, x: number, h?: number, discontinuity_cutoff?: number): number;
225
+ /**
226
+ * Take the derivative of a function.
227
+ * @param f Function `f(x)`
228
+ * @param x The x-value where to evaluate the derivative
229
+ * @param h Small step value
230
+ * @returns `f'(x)`
231
+ * @example
232
+ * ```js
233
+ * const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
234
+ * ```
235
+ */
236
+ static differentiate(f: (x: number) => number, x: number, h?: number): number;
237
+ /**
238
+ * Compute the definite integral of a function.
239
+ * @param f Function `f(x)`
240
+ * @param a The miminum integral bound
241
+ * @param b The maximum integral bound
242
+ * @param Ndx The number of rectangles to compute
243
+ * @returns `F(b)-F(a)`
244
+ * @example
245
+ * ```js
246
+ * const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
247
+ * ```
248
+ */
249
+ static integrate(f: (x: number) => number, a: number, b: number, Ndx?: number): number;
193
250
  }