smath 1.14.0 → 1.15.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
@@ -41,9 +41,14 @@ if (func.includes('help')) {
41
41
  console.log(' rint <min> <max> : Generate a uniformly-distributed random integer, range inclusive');
42
42
  console.log(' rnorm [mean] [stdev] : Generate a normally-distributed random float');
43
43
  console.log(' rdist <n> [mean] [stdev] : Generate `n` normally-distributed random floats');
44
+ console.log(' shuffle <c0> [c1] ... [cn]');
45
+ console.log(' : Shuffle an array of arbirary elements');
46
+ console.log(' selectRandom <c0> [c1] ... [cn]');
47
+ console.log(' : Select a number at random from a list');
48
+ console.log(' selectRandomWeighted <c0> [c1] ... [cn]');
49
+ console.log(' : Select an index at random from a list of weights');
44
50
  console.log(' rat <n> [eps] : Decompose `n` into a ratio');
45
51
  console.log(' mixed <n> [eps] : Decompose `n` into a mixed number');
46
- console.log(' toHex <n> [length] : Convert decimal `n` into hexadecimal');
47
52
  process.exit(1);
48
53
  }
49
54
  switch (func) {
@@ -139,6 +144,18 @@ switch (func) {
139
144
  console.log(SMath.rdist(nums[0], nums[1], nums[2]));
140
145
  break;
141
146
  }
147
+ case ('shuffle'): {
148
+ console.log(SMath.shuffle(nums));
149
+ break;
150
+ }
151
+ case ('selectRandom'): {
152
+ console.log(SMath.selectRandom(nums));
153
+ break;
154
+ }
155
+ case ('selectRandomWeighted'): {
156
+ console.log(SMath.selectRandomWeighted(nums));
157
+ break;
158
+ }
142
159
  case ('rat'): {
143
160
  console.log(SMath.rat(nums[0], (_c = nums[1]) !== null && _c !== void 0 ? _c : 1e-6));
144
161
  break;
package/dist/index.js CHANGED
@@ -35,6 +35,8 @@ exports.rint = rint;
35
35
  exports.rnorm = rnorm;
36
36
  exports.rdist = rdist;
37
37
  exports.shuffle = shuffle;
38
+ exports.selectRandom = selectRandom;
39
+ exports.selectRandomWeighted = selectRandomWeighted;
38
40
  exports.lim = lim;
39
41
  exports.differentiate = differentiate;
40
42
  exports.integrate = integrate;
@@ -228,7 +230,12 @@ function round2(n, base) {
228
230
  * const e = SMath.error(22.5, 25); // -0.1
229
231
  */
230
232
  function error(experimental, actual) {
231
- return (experimental - actual) / actual;
233
+ if (experimental === 0 && actual === 0) {
234
+ return 0;
235
+ }
236
+ else {
237
+ return (experimental - actual) / actual;
238
+ }
232
239
  }
233
240
  /**
234
241
  * Add up all the inputs.
@@ -394,6 +401,32 @@ function shuffle(stack) {
394
401
  }
395
402
  return rawData.sort(function (a, b) { return a.index - b.index; }).map(function (a) { return a.value; });
396
403
  }
404
+ /**
405
+ * Select a single item from an array at random with uniform weights.
406
+ * @param stack An array of arbirary item
407
+ * @returns A single randomly selected item
408
+ * @example
409
+ * const selected = SMath.selectRandom([10, 20, 30, 40]); // 30
410
+ */
411
+ function selectRandom(stack) {
412
+ return stack[rint(0, stack.length - 1)];
413
+ }
414
+ /**
415
+ * Select a single index in an array at random with different weights.
416
+ * @param weights The weights for each item
417
+ * @returns The index of the randomly selected item
418
+ */
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];
424
+ accumulation += clamp(0, weight, Infinity);
425
+ startWeights.push(accumulation);
426
+ }
427
+ var random = runif(0, accumulation);
428
+ return startWeights.findIndex(function (weight) { return random < weight; });
429
+ }
397
430
  /**
398
431
  * Take the limit of a function. A return value of `NaN` indicates
399
432
  * that no limit exists either due to a discontinuity or imaginary value.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "smath",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "description": "Small math function library",
5
5
  "homepage": "https://npm.nicfv.com/",
6
6
  "bin": "dist/bin.js",
package/types/index.d.ts CHANGED
@@ -233,6 +233,20 @@ export declare function rdist(count: number, mean?: number, stdev?: number): num
233
233
  * const shuffled = SMath.shuffle(['a', 'b', 'c']); // [ 'c', 'a', 'b' ]
234
234
  */
235
235
  export declare function shuffle<T>(stack: T[]): T[];
236
+ /**
237
+ * Select a single item from an array at random with uniform weights.
238
+ * @param stack An array of arbirary item
239
+ * @returns A single randomly selected item
240
+ * @example
241
+ * const selected = SMath.selectRandom([10, 20, 30, 40]); // 30
242
+ */
243
+ export declare function selectRandom<T>(stack: T[]): T;
244
+ /**
245
+ * Select a single index in an array at random with different weights.
246
+ * @param weights The weights for each item
247
+ * @returns The index of the randomly selected item
248
+ */
249
+ export declare function selectRandomWeighted(weights: number[]): number;
236
250
  /**
237
251
  * Take the limit of a function. A return value of `NaN` indicates
238
252
  * that no limit exists either due to a discontinuity or imaginary value.