smath 1.15.0 → 1.16.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/dist/bin.js CHANGED
@@ -1,10 +1,7 @@
1
1
  #!/usr/bin/env node
2
- "use strict";
3
- var _a, _b, _c, _d;
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- var SMath = require(".");
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);
2
+ import * as SMath from './index.js';
3
+ const func = (process.argv[2] ?? '').toLowerCase(), nums = process.argv.slice(3).map((arg, i) => {
4
+ const num = Number.parseFloat(arg);
8
5
  if (Number.isFinite(num)) {
9
6
  return num;
10
7
  }
@@ -53,7 +50,7 @@ if (func.includes('help')) {
53
50
  }
54
51
  switch (func) {
55
52
  case ('approx'): {
56
- console.log(SMath.approx(nums[0], nums[1], (_b = nums[2]) !== null && _b !== void 0 ? _b : 1e-6));
53
+ console.log(SMath.approx(nums[0], nums[1], nums[2] ?? 1e-6));
57
54
  break;
58
55
  }
59
56
  case ('clamp'): {
@@ -157,11 +154,11 @@ switch (func) {
157
154
  break;
158
155
  }
159
156
  case ('rat'): {
160
- console.log(SMath.rat(nums[0], (_c = nums[1]) !== null && _c !== void 0 ? _c : 1e-6));
157
+ console.log(SMath.rat(nums[0], nums[1] ?? 1e-6));
161
158
  break;
162
159
  }
163
160
  case ('mixed'): {
164
- console.log(SMath.mixed(nums[0], (_d = nums[1]) !== null && _d !== void 0 ? _d : 1e-6));
161
+ console.log(SMath.mixed(nums[0], nums[1] ?? 1e-6));
165
162
  break;
166
163
  }
167
164
  case (''): {
package/dist/index.js CHANGED
@@ -1,47 +1,3 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.approx = approx;
15
- exports.clamp = clamp;
16
- exports.normalize = normalize;
17
- exports.expand = expand;
18
- exports.translate = translate;
19
- exports.linspace = linspace;
20
- exports.logspace = logspace;
21
- exports.factorial = factorial;
22
- exports.factors = factors;
23
- exports.round2 = round2;
24
- exports.error = error;
25
- exports.sum = sum;
26
- exports.prod = prod;
27
- exports.avg = avg;
28
- exports.median = median;
29
- exports.varp = varp;
30
- exports.vars = vars;
31
- exports.stdevp = stdevp;
32
- exports.stdevs = stdevs;
33
- exports.runif = runif;
34
- exports.rint = rint;
35
- exports.rnorm = rnorm;
36
- exports.rdist = rdist;
37
- exports.shuffle = shuffle;
38
- exports.selectRandom = selectRandom;
39
- exports.selectRandomWeighted = selectRandomWeighted;
40
- exports.lim = lim;
41
- exports.differentiate = differentiate;
42
- exports.integrate = integrate;
43
- exports.rat = rat;
44
- exports.mixed = mixed;
45
1
  /**
46
2
  * @packageDocumentation
47
3
  * Small math function library
@@ -59,8 +15,7 @@ exports.mixed = mixed;
59
15
  * const b1 = SMath.approx(1 / 3, 0.33, 1e-6), // false
60
16
  * b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
61
17
  */
62
- function approx(a, b, epsilon) {
63
- if (epsilon === void 0) { epsilon = 1e-6; }
18
+ export function approx(a, b, epsilon = 1e-6) {
64
19
  return a - b < epsilon && b - a < epsilon;
65
20
  }
66
21
  /**
@@ -73,7 +28,7 @@ function approx(a, b, epsilon) {
73
28
  * const n1 = SMath.clamp(5, 0, 10), // 5
74
29
  * n2 = SMath.clamp(-2, 0, 10); // 0
75
30
  */
76
- function clamp(n, min, max) {
31
+ export function clamp(n, min, max) {
77
32
  if (n < min) {
78
33
  return min;
79
34
  }
@@ -91,7 +46,7 @@ function clamp(n, min, max) {
91
46
  * @example
92
47
  * const y = SMath.normalize(18, 9, 99); // 0.1
93
48
  */
94
- function normalize(n, min, max) {
49
+ export function normalize(n, min, max) {
95
50
  if (min === max) {
96
51
  return 0;
97
52
  }
@@ -106,7 +61,7 @@ function normalize(n, min, max) {
106
61
  * @example
107
62
  * const y = SMath.expand(0.25, 4, 6); // 4.5
108
63
  */
109
- function expand(n, min, max) {
64
+ export function expand(n, min, max) {
110
65
  return (max - min) * n + min;
111
66
  }
112
67
  /**
@@ -121,7 +76,7 @@ function expand(n, min, max) {
121
76
  * const C = 20,
122
77
  * F = SMath.translate(C, 0, 100, 32, 212); // 68
123
78
  */
124
- function translate(n, min1, max1, min2, max2) {
79
+ export function translate(n, min1, max1, min2, max2) {
125
80
  return expand(normalize(n, min1, max1), min2, max2);
126
81
  }
127
82
  /**
@@ -134,9 +89,9 @@ function translate(n, min1, max1, min2, max2) {
134
89
  * const space = SMath.linspace(1, 5, 6);
135
90
  * // [ 1, 1.8, 2.6, 3.4, 4.2, 5 ]
136
91
  */
137
- function linspace(min, max, count) {
138
- var space = [];
139
- for (var i = 0; i < count; i++) {
92
+ export function linspace(min, max, count) {
93
+ const space = [];
94
+ for (let i = 0; i < count; i++) {
140
95
  space[i] = translate(i, 0, count - 1, min, max);
141
96
  }
142
97
  return space;
@@ -151,8 +106,8 @@ function linspace(min, max, count) {
151
106
  * const space = SMath.logspace(0, 2, 5);
152
107
  * // [ 1, 3.2, 10, 31.6, 100 ]
153
108
  */
154
- function logspace(min, max, count) {
155
- return linspace(min, max, count).map(function (n) { return Math.pow(10, n); });
109
+ export function logspace(min, max, count) {
110
+ return linspace(min, max, count).map(n => 10 ** n);
156
111
  }
157
112
  /**
158
113
  * Compute the factorial of `n`.
@@ -161,7 +116,7 @@ function logspace(min, max, count) {
161
116
  * @example
162
117
  * const y = SMath.factorial(5); // 120
163
118
  */
164
- function factorial(n) {
119
+ export function factorial(n) {
165
120
  if (n < 0 || (n | 0) !== n) {
166
121
  throw new Error('Input must be a positive integer.');
167
122
  }
@@ -182,15 +137,15 @@ function factorial(n) {
182
137
  * @example
183
138
  * const y = SMath.factors(12); // [ 2, 2, 3 ]
184
139
  */
185
- function factors(n) {
140
+ export function factors(n) {
186
141
  if (n < 0 || (n | 0) !== n) {
187
142
  throw new Error('Input must be a positive integer!');
188
143
  }
189
144
  if (n <= 3) {
190
145
  return [n];
191
146
  }
192
- var f = [];
193
- var i = 2;
147
+ const f = [];
148
+ let i = 2;
194
149
  while (n > 1 && i <= n) {
195
150
  if ((n / i) === ((n / i) | 0)) {
196
151
  n /= i;
@@ -211,9 +166,9 @@ function factors(n) {
211
166
  * @example
212
167
  * const y = SMath.round2(Math.PI, 0.2); // 3.2
213
168
  */
214
- function round2(n, base) {
215
- var rounded = base ? base * Math.round(n / base) : n;
216
- var precision = 10; // Removes precision errors
169
+ export function round2(n, base) {
170
+ const rounded = base ? base * Math.round(n / base) : n;
171
+ const precision = 10; // Removes precision errors
217
172
  return parseFloat(rounded.toFixed(precision));
218
173
  }
219
174
  /**
@@ -229,7 +184,7 @@ function round2(n, base) {
229
184
  * @example
230
185
  * const e = SMath.error(22.5, 25); // -0.1
231
186
  */
232
- function error(experimental, actual) {
187
+ export function error(experimental, actual) {
233
188
  if (experimental === 0 && actual === 0) {
234
189
  return 0;
235
190
  }
@@ -245,8 +200,8 @@ function error(experimental, actual) {
245
200
  * @example
246
201
  * const y = SMath.sum([1, 2, 3]); // 6
247
202
  */
248
- function sum(data) {
249
- return data.reduce(function (a, b) { return a + b; }, 0);
203
+ export function sum(data) {
204
+ return data.reduce((a, b) => a + b, 0);
250
205
  }
251
206
  /**
252
207
  * Multiply all the inputs.
@@ -256,8 +211,8 @@ function sum(data) {
256
211
  * @example
257
212
  * const y = SMath.prod([2, 2, 3, 5]); // 60
258
213
  */
259
- function prod(data) {
260
- return data.reduce(function (a, b) { return a * b; }, 1);
214
+ export function prod(data) {
215
+ return data.reduce((a, b) => a * b, 1);
261
216
  }
262
217
  /**
263
218
  * Compute the average, or mean, of a set of numbers.
@@ -266,7 +221,7 @@ function prod(data) {
266
221
  * @example
267
222
  * const y = SMath.avg([1, 2, 4, 4]); // 2.75
268
223
  */
269
- function avg(data) {
224
+ export function avg(data) {
270
225
  return sum(data) / data.length;
271
226
  }
272
227
  /**
@@ -276,8 +231,8 @@ function avg(data) {
276
231
  * @example
277
232
  * const y = SMath.median([2, 5, 3, 1]); // 2.5
278
233
  */
279
- function median(data) {
280
- data.sort(function (a, b) { return a - b; });
234
+ export function median(data) {
235
+ data.sort((a, b) => a - b);
281
236
  if (data.length % 2) {
282
237
  return data[(data.length - 1) / 2];
283
238
  }
@@ -290,8 +245,8 @@ function median(data) {
290
245
  * @example
291
246
  * const y = SMath.varp([1, 2, 4, 4]); // 1.6875
292
247
  */
293
- function varp(data) {
294
- var mean = avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
248
+ export function varp(data) {
249
+ const mean = avg(data), squares = data.map(x => (x - mean) ** 2);
295
250
  return sum(squares) / data.length;
296
251
  }
297
252
  /**
@@ -301,8 +256,8 @@ function varp(data) {
301
256
  * @example
302
257
  * const y = SMath.vars([1, 2, 4, 4]); // 2.25
303
258
  */
304
- function vars(data) {
305
- var mean = avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
259
+ export function vars(data) {
260
+ const mean = avg(data), squares = data.map(x => (x - mean) ** 2);
306
261
  return sum(squares) / (data.length - 1);
307
262
  }
308
263
  /**
@@ -312,7 +267,7 @@ function vars(data) {
312
267
  * @example
313
268
  * const y = SMath.stdevp([1, 2, 3, 4]); // 1.118...
314
269
  */
315
- function stdevp(data) {
270
+ export function stdevp(data) {
316
271
  return Math.sqrt(varp(data));
317
272
  }
318
273
  /**
@@ -322,7 +277,7 @@ function stdevp(data) {
322
277
  * @example
323
278
  * const y = SMath.stdevs([1, 2, 3, 4]); // 1.29...
324
279
  */
325
- function stdevs(data) {
280
+ export function stdevs(data) {
326
281
  return Math.sqrt(vars(data));
327
282
  }
328
283
  /**
@@ -333,7 +288,7 @@ function stdevs(data) {
333
288
  * @example
334
289
  * const y = SMath.runif(-2, 2); // 0.376...
335
290
  */
336
- function runif(min, max) {
291
+ export function runif(min, max) {
337
292
  return expand(Math.random(), min, max);
338
293
  }
339
294
  /**
@@ -344,7 +299,7 @@ function runif(min, max) {
344
299
  * @example
345
300
  * const y = SMath.rint(-4, 3); // -4
346
301
  */
347
- function rint(min, max) {
302
+ export function rint(min, max) {
348
303
  min |= 0;
349
304
  max |= 0;
350
305
  if (min < 0) {
@@ -363,9 +318,7 @@ function rint(min, max) {
363
318
  * @example
364
319
  * const y = SMath.rnorm(2, 3); // 1.627...
365
320
  */
366
- function rnorm(mean, stdev) {
367
- if (mean === void 0) { mean = 0; }
368
- if (stdev === void 0) { stdev = 1; }
321
+ export function rnorm(mean = 0, stdev = 1) {
369
322
  return mean + stdev * Math.sqrt(-2 * Math.log(Math.random())) * Math.cos(2 * Math.PI * Math.random());
370
323
  }
371
324
  /**
@@ -377,11 +330,9 @@ function rnorm(mean, stdev) {
377
330
  * @example
378
331
  * const dataset = SMath.rdist(3); // [ 1.051..., -0.779..., -2.254... ]
379
332
  */
380
- function rdist(count, mean, stdev) {
381
- if (mean === void 0) { mean = 0; }
382
- if (stdev === void 0) { stdev = 1; }
383
- var distribution = [];
384
- for (var i = 0; i < count; i++) {
333
+ export function rdist(count, mean = 0, stdev = 1) {
334
+ const distribution = [];
335
+ for (let i = 0; i < count; i++) {
385
336
  distribution[i] = rnorm(mean, stdev);
386
337
  }
387
338
  return distribution;
@@ -393,13 +344,12 @@ function rdist(count, mean, stdev) {
393
344
  * @example
394
345
  * const shuffled = SMath.shuffle(['a', 'b', 'c']); // [ 'c', 'a', 'b' ]
395
346
  */
396
- function shuffle(stack) {
397
- var rawData = [];
398
- for (var _i = 0, stack_1 = stack; _i < stack_1.length; _i++) {
399
- var item = stack_1[_i];
347
+ export function shuffle(stack) {
348
+ const rawData = [];
349
+ for (const item of stack) {
400
350
  rawData.push({ index: Math.random(), value: item });
401
351
  }
402
- return rawData.sort(function (a, b) { return a.index - b.index; }).map(function (a) { return a.value; });
352
+ return rawData.sort((a, b) => a.index - b.index).map(a => a.value);
403
353
  }
404
354
  /**
405
355
  * Select a single item from an array at random with uniform weights.
@@ -408,24 +358,25 @@ function shuffle(stack) {
408
358
  * @example
409
359
  * const selected = SMath.selectRandom([10, 20, 30, 40]); // 30
410
360
  */
411
- function selectRandom(stack) {
361
+ export function selectRandom(stack) {
412
362
  return stack[rint(0, stack.length - 1)];
413
363
  }
414
364
  /**
415
365
  * Select a single index in an array at random with different weights.
416
366
  * @param weights The weights for each item
417
- * @returns The index of the randomly selected item
367
+ * @returns The 0-based index of the randomly selected item
368
+ * @example
369
+ * const index = SMath.selectRandomWeighted([3.5, 4, 1]); // 1
418
370
  */
419
- function selectRandomWeighted(weights) {
420
- var startWeights = [];
421
- var accumulation = 0;
422
- for (var _i = 0, weights_1 = weights; _i < weights_1.length; _i++) {
423
- var weight = weights_1[_i];
371
+ export function selectRandomWeighted(weights) {
372
+ const startWeights = [];
373
+ let accumulation = 0;
374
+ for (const weight of weights) {
424
375
  accumulation += clamp(0, weight, Infinity);
425
376
  startWeights.push(accumulation);
426
377
  }
427
- var random = runif(0, accumulation);
428
- return startWeights.findIndex(function (weight) { return random < weight; });
378
+ const random = runif(0, accumulation);
379
+ return startWeights.findIndex(weight => random < weight);
429
380
  }
430
381
  /**
431
382
  * Take the limit of a function. A return value of `NaN` indicates
@@ -438,11 +389,9 @@ function selectRandomWeighted(weights) {
438
389
  * @example
439
390
  * const y = SMath.lim(Math.log, 0); // -Infinity
440
391
  */
441
- function lim(f, x, h, discontinuity_cutoff) {
442
- if (h === void 0) { h = 1e-6; }
443
- if (discontinuity_cutoff === void 0) { discontinuity_cutoff = 1e-3; }
444
- var center = f(x), left1 = f(x - h), left2 = f(x - h / 2), right1 = f(x + h), right2 = f(x + h / 2);
445
- var left, right;
392
+ export function lim(f, x, h = 1e-6, discontinuity_cutoff = 1e-3) {
393
+ const center = f(x), left1 = f(x - h), left2 = f(x - h / 2), right1 = f(x + h), right2 = f(x + h / 2);
394
+ let left, right;
446
395
  if (Number.isFinite(center)) {
447
396
  return center;
448
397
  }
@@ -511,9 +460,8 @@ function lim(f, x, h, discontinuity_cutoff) {
511
460
  * @example
512
461
  * const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
513
462
  */
514
- function differentiate(f, x, epsilon) {
515
- if (epsilon === void 0) { epsilon = 1e-6; }
516
- return lim(function (h) { return (f(x + h) - f(x - h)) / (2 * h); }, 0, epsilon);
463
+ export function differentiate(f, x, epsilon = 1e-6) {
464
+ return lim(h => (f(x + h) - f(x - h)) / (2 * h), 0, epsilon);
517
465
  }
518
466
  /**
519
467
  * Compute the definite integral of a function.
@@ -525,9 +473,8 @@ function differentiate(f, x, epsilon) {
525
473
  * @example
526
474
  * const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
527
475
  */
528
- function integrate(f, a, b, Ndx) {
529
- if (Ndx === void 0) { Ndx = 1e6; }
530
- return ((b - a) / Ndx) * sum(linspace(a, b, Ndx).map(function (x) { return f(x); }));
476
+ export function integrate(f, a, b, Ndx = 1e6) {
477
+ return ((b - a) / Ndx) * sum(linspace(a, b, Ndx).map(x => f(x)));
531
478
  }
532
479
  /**
533
480
  * Convert an arbitrary decimal number into a simplified fraction (or ratio).
@@ -538,10 +485,9 @@ function integrate(f, a, b, Ndx) {
538
485
  * @example
539
486
  * const frac = SMath.rat(0.625); // { num: 5, den: 8 }
540
487
  */
541
- function rat(n, epsilon) {
542
- if (epsilon === void 0) { epsilon = 1e-6; }
543
- var num = 0, den = 1;
544
- var sign = n < 0 ? -1 : 1;
488
+ export function rat(n, epsilon = 1e-6) {
489
+ let num = 0, den = 1;
490
+ const sign = n < 0 ? -1 : 1;
545
491
  while (!approx(sign * n, num / den, epsilon)) {
546
492
  if (sign * n > num / den) {
547
493
  num++;
@@ -562,7 +508,6 @@ function rat(n, epsilon) {
562
508
  * @example
563
509
  * const frac = SMath.mixed(-8 / 6); // { whole: -1, num: 1, den: 3 }
564
510
  */
565
- function mixed(n, epsilon) {
566
- if (epsilon === void 0) { epsilon = 1e-6; }
567
- return __assign({ whole: n | 0 }, rat(n < -1 ? (n | 0) - n : n - (n | 0), epsilon));
511
+ export function mixed(n, epsilon = 1e-6) {
512
+ return { whole: n | 0, ...rat(n < -1 ? (n | 0) - n : n - (n | 0), epsilon) };
568
513
  }
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "1.15.0",
3
+ "version": "1.16.0",
4
4
  "description": "Small math function library",
5
5
  "homepage": "https://npm.nicfv.com/",
6
+ "type": "module",
6
7
  "bin": "dist/bin.js",
7
8
  "main": "dist/index.js",
8
9
  "types": "types/index.d.ts",
@@ -52,7 +53,7 @@
52
53
  "repository": "github:nicfv/npm",
53
54
  "license": "MIT",
54
55
  "devDependencies": {
55
- "@types/node": "25.0.10",
56
- "t6": "1.2.1"
56
+ "@types/node": "25.6.0",
57
+ "t6": "1.3.0"
57
58
  }
58
59
  }
package/types/index.d.ts CHANGED
@@ -244,7 +244,9 @@ export declare function selectRandom<T>(stack: T[]): T;
244
244
  /**
245
245
  * Select a single index in an array at random with different weights.
246
246
  * @param weights The weights for each item
247
- * @returns The index of the randomly selected item
247
+ * @returns The 0-based index of the randomly selected item
248
+ * @example
249
+ * const index = SMath.selectRandomWeighted([3.5, 4, 1]); // 1
248
250
  */
249
251
  export declare function selectRandomWeighted(weights: number[]): number;
250
252
  /**