smath 1.6.1 → 1.7.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 +2 -2
- package/dist/bin.js +20 -0
- package/dist/index.js +70 -1
- package/package.json +14 -4
- package/types/index.d.ts +45 -0
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|

|
|
5
|
-

|
|
6
6
|

|
|
7
7
|

|
|
8
8
|

|
|
@@ -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.
|
|
15
|
+
npm i smath@1.7.0
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
## Bugs and Requests
|
package/dist/bin.js
CHANGED
|
@@ -36,6 +36,10 @@ if (func.includes('help')) {
|
|
|
36
36
|
console.log(' vars <c0> [c1] ... [cn] : Compute the sample variance of `n` numbers');
|
|
37
37
|
console.log(' stdevp <c0> [c1] ... [cn]: Compute the population standard deviation of `n` numbers');
|
|
38
38
|
console.log(' stdevs <c0> [c1] ... [cn]: Compute the sample standard deviation of `n` numbers');
|
|
39
|
+
console.log(' runif <min> <max> : Generate a uniformly-distributed random float');
|
|
40
|
+
console.log(' rint <min> <max> : Generate a uniformly-distributed random integer, range inclusive');
|
|
41
|
+
console.log(' rnorm [mean] [stdev] : Generate a normally-distributed random float');
|
|
42
|
+
console.log(' rdist <n> [mean] [stdev] : Generate `n` normally-distributed random floats');
|
|
39
43
|
process.exit(1);
|
|
40
44
|
}
|
|
41
45
|
switch (func) {
|
|
@@ -111,6 +115,22 @@ switch (func) {
|
|
|
111
115
|
console.log(_1.SMath.stdevs(nums));
|
|
112
116
|
break;
|
|
113
117
|
}
|
|
118
|
+
case ('runif'): {
|
|
119
|
+
console.log(_1.SMath.runif(nums[0], nums[1]));
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case ('rint'): {
|
|
123
|
+
console.log(_1.SMath.rint(nums[0], nums[1]));
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case ('rnorm'): {
|
|
127
|
+
console.log(_1.SMath.rnorm(nums[0], nums[1]));
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
case ('rdist'): {
|
|
131
|
+
console.log(_1.SMath.rdist(nums[0], nums[1], nums[2]));
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
114
134
|
case (''): {
|
|
115
135
|
console.error('Missing argument. Use with "help" for a list of commands.');
|
|
116
136
|
process.exit(1);
|
package/dist/index.js
CHANGED
|
@@ -251,7 +251,7 @@ var SMath = /** @class */ (function () {
|
|
|
251
251
|
* ```
|
|
252
252
|
*/
|
|
253
253
|
SMath.median = function (data) {
|
|
254
|
-
data.sort();
|
|
254
|
+
data.sort(function (a, b) { return a - b; });
|
|
255
255
|
if (data.length % 2) {
|
|
256
256
|
return data[(data.length - 1) / 2];
|
|
257
257
|
}
|
|
@@ -307,6 +307,75 @@ var SMath = /** @class */ (function () {
|
|
|
307
307
|
SMath.stdevs = function (data) {
|
|
308
308
|
return Math.sqrt(this.vars(data));
|
|
309
309
|
};
|
|
310
|
+
/**
|
|
311
|
+
* Generate a uniformly-distributed floating-point number within the range.
|
|
312
|
+
* @param min The minimum bound
|
|
313
|
+
* @param max The maximum bound
|
|
314
|
+
* @returns A random float within the range
|
|
315
|
+
* @example
|
|
316
|
+
* ```js
|
|
317
|
+
* const y = SMath.runif(-2, 2); // 0.376...
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
SMath.runif = function (min, max) {
|
|
321
|
+
return this.expand(Math.random(), min, max);
|
|
322
|
+
};
|
|
323
|
+
/**
|
|
324
|
+
* Generate a uniformly-distributed integer within the range.
|
|
325
|
+
* @param min The minimum bound (inclusive)
|
|
326
|
+
* @param max The maximum bound (inclusive)
|
|
327
|
+
* @returns A random integer within the range
|
|
328
|
+
* @example
|
|
329
|
+
* ```js
|
|
330
|
+
* const y = SMath.rint(-4, 3); // -4
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
SMath.rint = function (min, max) {
|
|
334
|
+
min |= 0;
|
|
335
|
+
max |= 0;
|
|
336
|
+
if (min < 0) {
|
|
337
|
+
min--;
|
|
338
|
+
}
|
|
339
|
+
if (max < 0) {
|
|
340
|
+
max--;
|
|
341
|
+
}
|
|
342
|
+
return this.runif(min, max + 1) | 0; // `| 0` pulls toward 0
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* Generate a normally-distributed floating-point number.
|
|
346
|
+
* @param mean The mean of the population distribution
|
|
347
|
+
* @param stdev The standard deviation of the population
|
|
348
|
+
* @returns A random float
|
|
349
|
+
* @example
|
|
350
|
+
* ```js
|
|
351
|
+
* const y = SMath.rnorm(2, 3); // 1.627...
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
SMath.rnorm = function (mean, stdev) {
|
|
355
|
+
if (mean === void 0) { mean = 0; }
|
|
356
|
+
if (stdev === void 0) { stdev = 1; }
|
|
357
|
+
return mean + stdev * Math.sqrt(-2 * Math.log(Math.random())) * Math.cos(2 * Math.PI * Math.random());
|
|
358
|
+
};
|
|
359
|
+
/**
|
|
360
|
+
* Generate a population of normally-distributed floating-point numbers.
|
|
361
|
+
* @param count The number of values to generate
|
|
362
|
+
* @param mean The mean of the population distribution
|
|
363
|
+
* @param stdev The standard deviation of the population
|
|
364
|
+
* @returns A population of random floats
|
|
365
|
+
* @example
|
|
366
|
+
* ```js
|
|
367
|
+
* const dataset = SMath.rdist(3); // [ 1.051..., -0.779..., -2.254... ]
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
SMath.rdist = function (count, mean, stdev) {
|
|
371
|
+
if (mean === void 0) { mean = 0; }
|
|
372
|
+
if (stdev === void 0) { stdev = 1; }
|
|
373
|
+
var distribution = [];
|
|
374
|
+
for (var i = 0; i < count; i++) {
|
|
375
|
+
distribution[i] = this.rnorm(mean, stdev);
|
|
376
|
+
}
|
|
377
|
+
return distribution;
|
|
378
|
+
};
|
|
310
379
|
/**
|
|
311
380
|
* Take the limit of a function. A return value of `NaN` indicates
|
|
312
381
|
* 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.
|
|
3
|
+
"version": "1.7.0",
|
|
4
4
|
"description": "Small math function library",
|
|
5
5
|
"homepage": "https://npm.nicfv.com/smath",
|
|
6
6
|
"bin": "dist/bin.js",
|
|
@@ -28,6 +28,16 @@
|
|
|
28
28
|
"avg",
|
|
29
29
|
"average",
|
|
30
30
|
"mean",
|
|
31
|
+
"calculus",
|
|
32
|
+
"statistics",
|
|
33
|
+
"numeric",
|
|
34
|
+
"numerical",
|
|
35
|
+
"analysis",
|
|
36
|
+
"random",
|
|
37
|
+
"distribution",
|
|
38
|
+
"population",
|
|
39
|
+
"normal",
|
|
40
|
+
"normalize",
|
|
31
41
|
"interpolate",
|
|
32
42
|
"interpolation",
|
|
33
43
|
"extrapolate",
|
|
@@ -47,9 +57,9 @@
|
|
|
47
57
|
"repository": "github:nicfv/npm",
|
|
48
58
|
"license": "MIT",
|
|
49
59
|
"devDependencies": {
|
|
50
|
-
"@types/node": "20.
|
|
51
|
-
"exray": "1.0.
|
|
60
|
+
"@types/node": "20.12.4",
|
|
61
|
+
"exray": "1.0.3",
|
|
52
62
|
"typedoc": "0.25.12",
|
|
53
|
-
"typescript": "5.4.
|
|
63
|
+
"typescript": "5.4.4"
|
|
54
64
|
}
|
|
55
65
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -218,6 +218,51 @@ export declare abstract class SMath {
|
|
|
218
218
|
* ```
|
|
219
219
|
*/
|
|
220
220
|
static stdevs(data: Array<number>): number;
|
|
221
|
+
/**
|
|
222
|
+
* Generate a uniformly-distributed floating-point number within the range.
|
|
223
|
+
* @param min The minimum bound
|
|
224
|
+
* @param max The maximum bound
|
|
225
|
+
* @returns A random float within the range
|
|
226
|
+
* @example
|
|
227
|
+
* ```js
|
|
228
|
+
* const y = SMath.runif(-2, 2); // 0.376...
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
static runif(min: number, max: number): number;
|
|
232
|
+
/**
|
|
233
|
+
* Generate a uniformly-distributed integer within the range.
|
|
234
|
+
* @param min The minimum bound (inclusive)
|
|
235
|
+
* @param max The maximum bound (inclusive)
|
|
236
|
+
* @returns A random integer within the range
|
|
237
|
+
* @example
|
|
238
|
+
* ```js
|
|
239
|
+
* const y = SMath.rint(-4, 3); // -4
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
static rint(min: number, max: number): number;
|
|
243
|
+
/**
|
|
244
|
+
* Generate a normally-distributed floating-point number.
|
|
245
|
+
* @param mean The mean of the population distribution
|
|
246
|
+
* @param stdev The standard deviation of the population
|
|
247
|
+
* @returns A random float
|
|
248
|
+
* @example
|
|
249
|
+
* ```js
|
|
250
|
+
* const y = SMath.rnorm(2, 3); // 1.627...
|
|
251
|
+
* ```
|
|
252
|
+
*/
|
|
253
|
+
static rnorm(mean?: number, stdev?: number): number;
|
|
254
|
+
/**
|
|
255
|
+
* Generate a population of normally-distributed floating-point numbers.
|
|
256
|
+
* @param count The number of values to generate
|
|
257
|
+
* @param mean The mean of the population distribution
|
|
258
|
+
* @param stdev The standard deviation of the population
|
|
259
|
+
* @returns A population of random floats
|
|
260
|
+
* @example
|
|
261
|
+
* ```js
|
|
262
|
+
* const dataset = SMath.rdist(3); // [ 1.051..., -0.779..., -2.254... ]
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
static rdist(count: number, mean?: number, stdev?: number): Array<number>;
|
|
221
266
|
/**
|
|
222
267
|
* Take the limit of a function. A return value of `NaN` indicates
|
|
223
268
|
* that no limit exists either due to a discontinuity or imaginary value.
|