smath 1.6.2 → 1.8.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 +155 -64
- package/package.json +11 -6
- package/types/index.d.ts +67 -22
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.8.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
|
@@ -11,9 +11,8 @@ exports.SMath = void 0;
|
|
|
11
11
|
* Contains a small math function library including
|
|
12
12
|
* useful interpolation and extrapolation functions.
|
|
13
13
|
*/
|
|
14
|
-
var SMath
|
|
15
|
-
|
|
16
|
-
}
|
|
14
|
+
var SMath;
|
|
15
|
+
(function (SMath) {
|
|
17
16
|
/**
|
|
18
17
|
* Check if two numbers are approximately equal with a maximum abolute error.
|
|
19
18
|
* @param a Any number
|
|
@@ -26,10 +25,11 @@ var SMath = /** @class */ (function () {
|
|
|
26
25
|
* b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
|
|
27
26
|
* ```
|
|
28
27
|
*/
|
|
29
|
-
|
|
28
|
+
function approx(a, b, epsilon) {
|
|
30
29
|
if (epsilon === void 0) { epsilon = 1e-6; }
|
|
31
30
|
return a - b < epsilon && b - a < epsilon;
|
|
32
|
-
}
|
|
31
|
+
}
|
|
32
|
+
SMath.approx = approx;
|
|
33
33
|
/**
|
|
34
34
|
* Clamp a number within a range.
|
|
35
35
|
* @param n The number to clamp
|
|
@@ -42,7 +42,7 @@ var SMath = /** @class */ (function () {
|
|
|
42
42
|
* n2 = SMath.clamp(-2, 0, 10); // 0
|
|
43
43
|
* ```
|
|
44
44
|
*/
|
|
45
|
-
|
|
45
|
+
function clamp(n, min, max) {
|
|
46
46
|
if (n < min) {
|
|
47
47
|
return min;
|
|
48
48
|
}
|
|
@@ -50,7 +50,8 @@ var SMath = /** @class */ (function () {
|
|
|
50
50
|
return max;
|
|
51
51
|
}
|
|
52
52
|
return n;
|
|
53
|
-
}
|
|
53
|
+
}
|
|
54
|
+
SMath.clamp = clamp;
|
|
54
55
|
/**
|
|
55
56
|
* Normalize the number `n` from the range `min, max` to the range `0, 1`
|
|
56
57
|
* @param n The number to normalize
|
|
@@ -62,12 +63,13 @@ var SMath = /** @class */ (function () {
|
|
|
62
63
|
* const y = SMath.normalize(18, 9, 99); // 0.1
|
|
63
64
|
* ```
|
|
64
65
|
*/
|
|
65
|
-
|
|
66
|
+
function normalize(n, min, max) {
|
|
66
67
|
if (min === max) {
|
|
67
68
|
return 0;
|
|
68
69
|
}
|
|
69
70
|
return (n - min) / (max - min);
|
|
70
|
-
}
|
|
71
|
+
}
|
|
72
|
+
SMath.normalize = normalize;
|
|
71
73
|
/**
|
|
72
74
|
* Expand a normalized number `n` to the range `min, max`
|
|
73
75
|
* @param n A normalized number
|
|
@@ -79,9 +81,10 @@ var SMath = /** @class */ (function () {
|
|
|
79
81
|
* const y = SMath.expand(0.25, 4, 6); // 4.5
|
|
80
82
|
* ```
|
|
81
83
|
*/
|
|
82
|
-
|
|
84
|
+
function expand(n, min, max) {
|
|
83
85
|
return (max - min) * n + min;
|
|
84
|
-
}
|
|
86
|
+
}
|
|
87
|
+
SMath.expand = expand;
|
|
85
88
|
/**
|
|
86
89
|
* Translate a number `n` from the range `min1, max1` to the range `min2, max2`
|
|
87
90
|
* @param n The number to translate
|
|
@@ -96,9 +99,10 @@ var SMath = /** @class */ (function () {
|
|
|
96
99
|
* F = SMath.translate(C, 0, 100, 32, 212); // 68
|
|
97
100
|
* ```
|
|
98
101
|
*/
|
|
99
|
-
|
|
100
|
-
return
|
|
101
|
-
}
|
|
102
|
+
function translate(n, min1, max1, min2, max2) {
|
|
103
|
+
return expand(normalize(n, min1, max1), min2, max2);
|
|
104
|
+
}
|
|
105
|
+
SMath.translate = translate;
|
|
102
106
|
/**
|
|
103
107
|
* Generate an array of linearly spaced numbers.
|
|
104
108
|
* @param min The initial value of the linear space
|
|
@@ -111,13 +115,14 @@ var SMath = /** @class */ (function () {
|
|
|
111
115
|
* // [ 1, 1.8, 2.6, 3.4, 4.2, 5 ]
|
|
112
116
|
* ```
|
|
113
117
|
*/
|
|
114
|
-
|
|
118
|
+
function linspace(min, max, count) {
|
|
115
119
|
var space = [];
|
|
116
120
|
for (var i = 0; i < count; i++) {
|
|
117
|
-
space[i] =
|
|
121
|
+
space[i] = translate(i, 0, count - 1, min, max);
|
|
118
122
|
}
|
|
119
123
|
return space;
|
|
120
|
-
}
|
|
124
|
+
}
|
|
125
|
+
SMath.linspace = linspace;
|
|
121
126
|
/**
|
|
122
127
|
* Generate an array of logarithmically spaced numbers.
|
|
123
128
|
* @param min The initial magnitude of the space
|
|
@@ -130,9 +135,10 @@ var SMath = /** @class */ (function () {
|
|
|
130
135
|
* // [ 1, 3.2, 10, 31.6, 100 ]
|
|
131
136
|
* ```
|
|
132
137
|
*/
|
|
133
|
-
|
|
134
|
-
return
|
|
135
|
-
}
|
|
138
|
+
function logspace(min, max, count) {
|
|
139
|
+
return linspace(min, max, count).map(function (n) { return Math.pow(10, n); });
|
|
140
|
+
}
|
|
141
|
+
SMath.logspace = logspace;
|
|
136
142
|
/**
|
|
137
143
|
* Compute the factorial of `n`.
|
|
138
144
|
* @param n Any positive integer
|
|
@@ -142,7 +148,7 @@ var SMath = /** @class */ (function () {
|
|
|
142
148
|
* const y = SMath.factorial(5); // 120
|
|
143
149
|
* ```
|
|
144
150
|
*/
|
|
145
|
-
|
|
151
|
+
function factorial(n) {
|
|
146
152
|
if (n < 0 || (n | 0) !== n) {
|
|
147
153
|
throw new Error('Input must be a positive integer.');
|
|
148
154
|
}
|
|
@@ -153,9 +159,10 @@ var SMath = /** @class */ (function () {
|
|
|
153
159
|
return n;
|
|
154
160
|
}
|
|
155
161
|
else {
|
|
156
|
-
return n *
|
|
162
|
+
return n * factorial(n - 1);
|
|
157
163
|
}
|
|
158
|
-
}
|
|
164
|
+
}
|
|
165
|
+
SMath.factorial = factorial;
|
|
159
166
|
/**
|
|
160
167
|
* Factorize `n` into its prime factors.
|
|
161
168
|
* @param n Any positive integer
|
|
@@ -165,7 +172,7 @@ var SMath = /** @class */ (function () {
|
|
|
165
172
|
* const y = SMath.factors(12); // [ 2, 2, 3 ]
|
|
166
173
|
* ```
|
|
167
174
|
*/
|
|
168
|
-
|
|
175
|
+
function factors(n) {
|
|
169
176
|
if (n < 0 || (n | 0) !== n) {
|
|
170
177
|
throw new Error('Input must be a positive integer!');
|
|
171
178
|
}
|
|
@@ -184,7 +191,8 @@ var SMath = /** @class */ (function () {
|
|
|
184
191
|
}
|
|
185
192
|
}
|
|
186
193
|
return f;
|
|
187
|
-
}
|
|
194
|
+
}
|
|
195
|
+
SMath.factors = factors;
|
|
188
196
|
/**
|
|
189
197
|
* Calculate the relative normalized error or deviation from any
|
|
190
198
|
* value to an accepted value. An error of 0 indicates that the
|
|
@@ -200,9 +208,10 @@ var SMath = /** @class */ (function () {
|
|
|
200
208
|
* const e = SMath.error(22.5, 25); // -0.1
|
|
201
209
|
* ```
|
|
202
210
|
*/
|
|
203
|
-
|
|
211
|
+
function error(experimental, actual) {
|
|
204
212
|
return (experimental - actual) / actual;
|
|
205
|
-
}
|
|
213
|
+
}
|
|
214
|
+
SMath.error = error;
|
|
206
215
|
/**
|
|
207
216
|
* Add up all the inputs.
|
|
208
217
|
* If none are present, returns 0.
|
|
@@ -213,9 +222,10 @@ var SMath = /** @class */ (function () {
|
|
|
213
222
|
* const y = SMath.sum([1, 2, 3]); // 6
|
|
214
223
|
* ```
|
|
215
224
|
*/
|
|
216
|
-
|
|
225
|
+
function sum(data) {
|
|
217
226
|
return data.reduce(function (a, b) { return a + b; }, 0);
|
|
218
|
-
}
|
|
227
|
+
}
|
|
228
|
+
SMath.sum = sum;
|
|
219
229
|
/**
|
|
220
230
|
* Multiply all the inputs.
|
|
221
231
|
* If none are present, returns 1.
|
|
@@ -226,9 +236,10 @@ var SMath = /** @class */ (function () {
|
|
|
226
236
|
* const y = SMath.prod([2, 2, 3, 5]); // 60
|
|
227
237
|
* ```
|
|
228
238
|
*/
|
|
229
|
-
|
|
239
|
+
function prod(data) {
|
|
230
240
|
return data.reduce(function (a, b) { return a * b; }, 1);
|
|
231
|
-
}
|
|
241
|
+
}
|
|
242
|
+
SMath.prod = prod;
|
|
232
243
|
/**
|
|
233
244
|
* Compute the average, or mean, of a set of numbers.
|
|
234
245
|
* @param data An array of numeric inputs
|
|
@@ -238,9 +249,10 @@ var SMath = /** @class */ (function () {
|
|
|
238
249
|
* const y = SMath.avg([1, 2, 4, 4]); // 2.75
|
|
239
250
|
* ```
|
|
240
251
|
*/
|
|
241
|
-
|
|
242
|
-
return
|
|
243
|
-
}
|
|
252
|
+
function avg(data) {
|
|
253
|
+
return sum(data) / data.length;
|
|
254
|
+
}
|
|
255
|
+
SMath.avg = avg;
|
|
244
256
|
/**
|
|
245
257
|
* Compute the median of a set of numbers.
|
|
246
258
|
* @param data An array of numeric inputs
|
|
@@ -250,13 +262,14 @@ var SMath = /** @class */ (function () {
|
|
|
250
262
|
* const y = SMath.median([2, 5, 3, 1]); // 2.5
|
|
251
263
|
* ```
|
|
252
264
|
*/
|
|
253
|
-
|
|
265
|
+
function median(data) {
|
|
254
266
|
data.sort(function (a, b) { return a - b; });
|
|
255
267
|
if (data.length % 2) {
|
|
256
268
|
return data[(data.length - 1) / 2];
|
|
257
269
|
}
|
|
258
|
-
return
|
|
259
|
-
}
|
|
270
|
+
return avg([data[data.length / 2 - 1], data[data.length / 2]]);
|
|
271
|
+
}
|
|
272
|
+
SMath.median = median;
|
|
260
273
|
/**
|
|
261
274
|
* Compute the variance of a **complete population**.
|
|
262
275
|
* @param data An array of numeric inputs
|
|
@@ -266,10 +279,11 @@ var SMath = /** @class */ (function () {
|
|
|
266
279
|
* const y = SMath.varp([1, 2, 4, 4]); // 1.6875
|
|
267
280
|
* ```
|
|
268
281
|
*/
|
|
269
|
-
|
|
270
|
-
var mean =
|
|
271
|
-
return
|
|
272
|
-
}
|
|
282
|
+
function varp(data) {
|
|
283
|
+
var mean = avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
|
|
284
|
+
return sum(squares) / data.length;
|
|
285
|
+
}
|
|
286
|
+
SMath.varp = varp;
|
|
273
287
|
/**
|
|
274
288
|
* Compute the variance of a **sample**.
|
|
275
289
|
* @param data An array of numeric inputs
|
|
@@ -279,10 +293,11 @@ var SMath = /** @class */ (function () {
|
|
|
279
293
|
* const y = SMath.vars([1, 2, 4, 4]); // 2.25
|
|
280
294
|
* ```
|
|
281
295
|
*/
|
|
282
|
-
|
|
283
|
-
var mean =
|
|
284
|
-
return
|
|
285
|
-
}
|
|
296
|
+
function vars(data) {
|
|
297
|
+
var mean = avg(data), squares = data.map(function (x) { return Math.pow((x - mean), 2); });
|
|
298
|
+
return sum(squares) / (data.length - 1);
|
|
299
|
+
}
|
|
300
|
+
SMath.vars = vars;
|
|
286
301
|
/**
|
|
287
302
|
* Compute the standard deviation of a **complete population**.
|
|
288
303
|
* @param data An array of numeric inputs
|
|
@@ -292,9 +307,10 @@ var SMath = /** @class */ (function () {
|
|
|
292
307
|
* const y = SMath.stdevp([1, 2, 3, 4]); // 1.118...
|
|
293
308
|
* ```
|
|
294
309
|
*/
|
|
295
|
-
|
|
296
|
-
return Math.sqrt(
|
|
297
|
-
}
|
|
310
|
+
function stdevp(data) {
|
|
311
|
+
return Math.sqrt(varp(data));
|
|
312
|
+
}
|
|
313
|
+
SMath.stdevp = stdevp;
|
|
298
314
|
/**
|
|
299
315
|
* Compute the standard deviation of a **sample**.
|
|
300
316
|
* @param data An array of numeric inputs
|
|
@@ -304,9 +320,83 @@ var SMath = /** @class */ (function () {
|
|
|
304
320
|
* const y = SMath.stdevs([1, 2, 3, 4]); // 1.29...
|
|
305
321
|
* ```
|
|
306
322
|
*/
|
|
307
|
-
|
|
308
|
-
return Math.sqrt(
|
|
309
|
-
}
|
|
323
|
+
function stdevs(data) {
|
|
324
|
+
return Math.sqrt(vars(data));
|
|
325
|
+
}
|
|
326
|
+
SMath.stdevs = stdevs;
|
|
327
|
+
/**
|
|
328
|
+
* Generate a uniformly-distributed floating-point number within the range.
|
|
329
|
+
* @param min The minimum bound
|
|
330
|
+
* @param max The maximum bound
|
|
331
|
+
* @returns A random float within the range
|
|
332
|
+
* @example
|
|
333
|
+
* ```js
|
|
334
|
+
* const y = SMath.runif(-2, 2); // 0.376...
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
337
|
+
function runif(min, max) {
|
|
338
|
+
return expand(Math.random(), min, max);
|
|
339
|
+
}
|
|
340
|
+
SMath.runif = runif;
|
|
341
|
+
/**
|
|
342
|
+
* Generate a uniformly-distributed integer within the range.
|
|
343
|
+
* @param min The minimum bound (inclusive)
|
|
344
|
+
* @param max The maximum bound (inclusive)
|
|
345
|
+
* @returns A random integer within the range
|
|
346
|
+
* @example
|
|
347
|
+
* ```js
|
|
348
|
+
* const y = SMath.rint(-4, 3); // -4
|
|
349
|
+
* ```
|
|
350
|
+
*/
|
|
351
|
+
function rint(min, max) {
|
|
352
|
+
min |= 0;
|
|
353
|
+
max |= 0;
|
|
354
|
+
if (min < 0) {
|
|
355
|
+
min--;
|
|
356
|
+
}
|
|
357
|
+
if (max < 0) {
|
|
358
|
+
max--;
|
|
359
|
+
}
|
|
360
|
+
return runif(min, max + 1) | 0; // `| 0` pulls toward 0
|
|
361
|
+
}
|
|
362
|
+
SMath.rint = rint;
|
|
363
|
+
/**
|
|
364
|
+
* Generate a normally-distributed floating-point number.
|
|
365
|
+
* @param mean The mean of the population distribution
|
|
366
|
+
* @param stdev The standard deviation of the population
|
|
367
|
+
* @returns A random float
|
|
368
|
+
* @example
|
|
369
|
+
* ```js
|
|
370
|
+
* const y = SMath.rnorm(2, 3); // 1.627...
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
function rnorm(mean, stdev) {
|
|
374
|
+
if (mean === void 0) { mean = 0; }
|
|
375
|
+
if (stdev === void 0) { stdev = 1; }
|
|
376
|
+
return mean + stdev * Math.sqrt(-2 * Math.log(Math.random())) * Math.cos(2 * Math.PI * Math.random());
|
|
377
|
+
}
|
|
378
|
+
SMath.rnorm = rnorm;
|
|
379
|
+
/**
|
|
380
|
+
* Generate a population of normally-distributed floating-point numbers.
|
|
381
|
+
* @param count The number of values to generate
|
|
382
|
+
* @param mean The mean of the population distribution
|
|
383
|
+
* @param stdev The standard deviation of the population
|
|
384
|
+
* @returns A population of random floats
|
|
385
|
+
* @example
|
|
386
|
+
* ```js
|
|
387
|
+
* const dataset = SMath.rdist(3); // [ 1.051..., -0.779..., -2.254... ]
|
|
388
|
+
* ```
|
|
389
|
+
*/
|
|
390
|
+
function rdist(count, mean, stdev) {
|
|
391
|
+
if (mean === void 0) { mean = 0; }
|
|
392
|
+
if (stdev === void 0) { stdev = 1; }
|
|
393
|
+
var distribution = [];
|
|
394
|
+
for (var i = 0; i < count; i++) {
|
|
395
|
+
distribution[i] = rnorm(mean, stdev);
|
|
396
|
+
}
|
|
397
|
+
return distribution;
|
|
398
|
+
}
|
|
399
|
+
SMath.rdist = rdist;
|
|
310
400
|
/**
|
|
311
401
|
* Take the limit of a function. A return value of `NaN` indicates
|
|
312
402
|
* that no limit exists either due to a discontinuity or imaginary value.
|
|
@@ -320,7 +410,7 @@ var SMath = /** @class */ (function () {
|
|
|
320
410
|
* const y = SMath.lim(Math.log, 0); // -Infinity
|
|
321
411
|
* ```
|
|
322
412
|
*/
|
|
323
|
-
|
|
413
|
+
function lim(f, x, h, discontinuity_cutoff) {
|
|
324
414
|
if (h === void 0) { h = 1e-3; }
|
|
325
415
|
if (discontinuity_cutoff === void 0) { discontinuity_cutoff = 1; }
|
|
326
416
|
var center = f(x), left1 = f(x - h), left2 = f(x - h / 2), right1 = f(x + h), right2 = f(x + h / 2);
|
|
@@ -337,7 +427,7 @@ var SMath = /** @class */ (function () {
|
|
|
337
427
|
left = -Infinity;
|
|
338
428
|
}
|
|
339
429
|
else {
|
|
340
|
-
left =
|
|
430
|
+
left = avg([left1, left2]);
|
|
341
431
|
}
|
|
342
432
|
}
|
|
343
433
|
else if (left1 === left2) { // Handles +/-Infinity case
|
|
@@ -355,7 +445,7 @@ var SMath = /** @class */ (function () {
|
|
|
355
445
|
right = -Infinity;
|
|
356
446
|
}
|
|
357
447
|
else {
|
|
358
|
-
right =
|
|
448
|
+
right = avg([right1, right2]);
|
|
359
449
|
}
|
|
360
450
|
}
|
|
361
451
|
else if (right1 === right2) { // Handles +/-Infinity case
|
|
@@ -369,7 +459,7 @@ var SMath = /** @class */ (function () {
|
|
|
369
459
|
return left;
|
|
370
460
|
}
|
|
371
461
|
else if (SMath.approx(left, right, discontinuity_cutoff)) {
|
|
372
|
-
return
|
|
462
|
+
return avg([left, right]);
|
|
373
463
|
}
|
|
374
464
|
else if (!Number.isNaN(left) && Number.isNaN(right)) {
|
|
375
465
|
return left;
|
|
@@ -380,7 +470,8 @@ var SMath = /** @class */ (function () {
|
|
|
380
470
|
else {
|
|
381
471
|
return NaN;
|
|
382
472
|
}
|
|
383
|
-
}
|
|
473
|
+
}
|
|
474
|
+
SMath.lim = lim;
|
|
384
475
|
/**
|
|
385
476
|
* Take the derivative of a function.
|
|
386
477
|
* @param f Function `f(x)`
|
|
@@ -392,10 +483,11 @@ var SMath = /** @class */ (function () {
|
|
|
392
483
|
* const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
|
|
393
484
|
* ```
|
|
394
485
|
*/
|
|
395
|
-
|
|
486
|
+
function differentiate(f, x, h) {
|
|
396
487
|
if (h === void 0) { h = 1e-3; }
|
|
397
488
|
return (f(x + h) - f(x - h)) / (2 * h);
|
|
398
|
-
}
|
|
489
|
+
}
|
|
490
|
+
SMath.differentiate = differentiate;
|
|
399
491
|
/**
|
|
400
492
|
* Compute the definite integral of a function.
|
|
401
493
|
* @param f Function `f(x)`
|
|
@@ -408,10 +500,9 @@ var SMath = /** @class */ (function () {
|
|
|
408
500
|
* const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
|
|
409
501
|
* ```
|
|
410
502
|
*/
|
|
411
|
-
|
|
503
|
+
function integrate(f, a, b, Ndx) {
|
|
412
504
|
if (Ndx === void 0) { Ndx = 1e3; }
|
|
413
|
-
return ((b - a) / Ndx) *
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
}());
|
|
417
|
-
exports.SMath = SMath;
|
|
505
|
+
return ((b - a) / Ndx) * sum(linspace(a, b, Ndx).map(function (x) { return f(x); }));
|
|
506
|
+
}
|
|
507
|
+
SMath.integrate = integrate;
|
|
508
|
+
})(SMath || (exports.SMath = SMath = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smath",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "Small math function library",
|
|
5
5
|
"homepage": "https://npm.nicfv.com/smath",
|
|
6
6
|
"bin": "dist/bin.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"build": "rm -rf dist types && tsc && node dist/test.js && rm dist/test.js",
|
|
15
15
|
"test": "tsc --noEmit",
|
|
16
16
|
"clean": "rm -rf node_modules package-lock.json dist types docs",
|
|
17
|
-
"docs": "rm -rf docs && typedoc --includeVersion --disableSources --hideGenerator src",
|
|
17
|
+
"docs": "rm -rf docs && typedoc --includeVersion --disableSources --hideGenerator --excludePrivate --excludeProtected src",
|
|
18
18
|
"prepack": "npm run build",
|
|
19
19
|
"postpack": "rm -rf dist types"
|
|
20
20
|
},
|
|
@@ -33,6 +33,11 @@
|
|
|
33
33
|
"numeric",
|
|
34
34
|
"numerical",
|
|
35
35
|
"analysis",
|
|
36
|
+
"random",
|
|
37
|
+
"distribution",
|
|
38
|
+
"population",
|
|
39
|
+
"normal",
|
|
40
|
+
"normalize",
|
|
36
41
|
"interpolate",
|
|
37
42
|
"interpolation",
|
|
38
43
|
"extrapolate",
|
|
@@ -52,9 +57,9 @@
|
|
|
52
57
|
"repository": "github:nicfv/npm",
|
|
53
58
|
"license": "MIT",
|
|
54
59
|
"devDependencies": {
|
|
55
|
-
"@types/node": "20.12.
|
|
56
|
-
"exray": "1.0.
|
|
57
|
-
"typedoc": "0.25.
|
|
58
|
-
"typescript": "5.4.
|
|
60
|
+
"@types/node": "20.12.7",
|
|
61
|
+
"exray": "1.0.3",
|
|
62
|
+
"typedoc": "0.25.13",
|
|
63
|
+
"typescript": "5.4.5"
|
|
59
64
|
}
|
|
60
65
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* Contains a small math function library including
|
|
9
9
|
* useful interpolation and extrapolation functions.
|
|
10
10
|
*/
|
|
11
|
-
export declare
|
|
11
|
+
export declare namespace SMath {
|
|
12
12
|
/**
|
|
13
13
|
* Check if two numbers are approximately equal with a maximum abolute error.
|
|
14
14
|
* @param a Any number
|
|
@@ -21,7 +21,7 @@ export declare abstract class SMath {
|
|
|
21
21
|
* b2 = SMath.approx(1 / 3, 0.33, 1e-2); // true
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
|
|
24
|
+
function approx(a: number, b: number, epsilon?: number): boolean;
|
|
25
25
|
/**
|
|
26
26
|
* Clamp a number within a range.
|
|
27
27
|
* @param n The number to clamp
|
|
@@ -34,7 +34,7 @@ export declare abstract class SMath {
|
|
|
34
34
|
* n2 = SMath.clamp(-2, 0, 10); // 0
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
|
-
|
|
37
|
+
function clamp(n: number, min: number, max: number): number;
|
|
38
38
|
/**
|
|
39
39
|
* Normalize the number `n` from the range `min, max` to the range `0, 1`
|
|
40
40
|
* @param n The number to normalize
|
|
@@ -46,7 +46,7 @@ export declare abstract class SMath {
|
|
|
46
46
|
* const y = SMath.normalize(18, 9, 99); // 0.1
|
|
47
47
|
* ```
|
|
48
48
|
*/
|
|
49
|
-
|
|
49
|
+
function normalize(n: number, min: number, max: number): number;
|
|
50
50
|
/**
|
|
51
51
|
* Expand a normalized number `n` to the range `min, max`
|
|
52
52
|
* @param n A normalized number
|
|
@@ -58,7 +58,7 @@ export declare abstract class SMath {
|
|
|
58
58
|
* const y = SMath.expand(0.25, 4, 6); // 4.5
|
|
59
59
|
* ```
|
|
60
60
|
*/
|
|
61
|
-
|
|
61
|
+
function expand(n: number, min: number, max: number): number;
|
|
62
62
|
/**
|
|
63
63
|
* Translate a number `n` from the range `min1, max1` to the range `min2, max2`
|
|
64
64
|
* @param n The number to translate
|
|
@@ -73,7 +73,7 @@ export declare abstract class SMath {
|
|
|
73
73
|
* F = SMath.translate(C, 0, 100, 32, 212); // 68
|
|
74
74
|
* ```
|
|
75
75
|
*/
|
|
76
|
-
|
|
76
|
+
function translate(n: number, min1: number, max1: number, min2: number, max2: number): number;
|
|
77
77
|
/**
|
|
78
78
|
* Generate an array of linearly spaced numbers.
|
|
79
79
|
* @param min The initial value of the linear space
|
|
@@ -86,7 +86,7 @@ export declare abstract class SMath {
|
|
|
86
86
|
* // [ 1, 1.8, 2.6, 3.4, 4.2, 5 ]
|
|
87
87
|
* ```
|
|
88
88
|
*/
|
|
89
|
-
|
|
89
|
+
function linspace(min: number, max: number, count: number): Array<number>;
|
|
90
90
|
/**
|
|
91
91
|
* Generate an array of logarithmically spaced numbers.
|
|
92
92
|
* @param min The initial magnitude of the space
|
|
@@ -99,7 +99,7 @@ export declare abstract class SMath {
|
|
|
99
99
|
* // [ 1, 3.2, 10, 31.6, 100 ]
|
|
100
100
|
* ```
|
|
101
101
|
*/
|
|
102
|
-
|
|
102
|
+
function logspace(min: number, max: number, count: number): Array<number>;
|
|
103
103
|
/**
|
|
104
104
|
* Compute the factorial of `n`.
|
|
105
105
|
* @param n Any positive integer
|
|
@@ -109,7 +109,7 @@ export declare abstract class SMath {
|
|
|
109
109
|
* const y = SMath.factorial(5); // 120
|
|
110
110
|
* ```
|
|
111
111
|
*/
|
|
112
|
-
|
|
112
|
+
function factorial(n: number): number;
|
|
113
113
|
/**
|
|
114
114
|
* Factorize `n` into its prime factors.
|
|
115
115
|
* @param n Any positive integer
|
|
@@ -119,7 +119,7 @@ export declare abstract class SMath {
|
|
|
119
119
|
* const y = SMath.factors(12); // [ 2, 2, 3 ]
|
|
120
120
|
* ```
|
|
121
121
|
*/
|
|
122
|
-
|
|
122
|
+
function factors(n: number): Array<number>;
|
|
123
123
|
/**
|
|
124
124
|
* Calculate the relative normalized error or deviation from any
|
|
125
125
|
* value to an accepted value. An error of 0 indicates that the
|
|
@@ -135,7 +135,7 @@ export declare abstract class SMath {
|
|
|
135
135
|
* const e = SMath.error(22.5, 25); // -0.1
|
|
136
136
|
* ```
|
|
137
137
|
*/
|
|
138
|
-
|
|
138
|
+
function error(experimental: number, actual: number): number;
|
|
139
139
|
/**
|
|
140
140
|
* Add up all the inputs.
|
|
141
141
|
* If none are present, returns 0.
|
|
@@ -146,7 +146,7 @@ export declare abstract class SMath {
|
|
|
146
146
|
* const y = SMath.sum([1, 2, 3]); // 6
|
|
147
147
|
* ```
|
|
148
148
|
*/
|
|
149
|
-
|
|
149
|
+
function sum(data: Array<number>): number;
|
|
150
150
|
/**
|
|
151
151
|
* Multiply all the inputs.
|
|
152
152
|
* If none are present, returns 1.
|
|
@@ -157,7 +157,7 @@ export declare abstract class SMath {
|
|
|
157
157
|
* const y = SMath.prod([2, 2, 3, 5]); // 60
|
|
158
158
|
* ```
|
|
159
159
|
*/
|
|
160
|
-
|
|
160
|
+
function prod(data: Array<number>): number;
|
|
161
161
|
/**
|
|
162
162
|
* Compute the average, or mean, of a set of numbers.
|
|
163
163
|
* @param data An array of numeric inputs
|
|
@@ -167,7 +167,7 @@ export declare abstract class SMath {
|
|
|
167
167
|
* const y = SMath.avg([1, 2, 4, 4]); // 2.75
|
|
168
168
|
* ```
|
|
169
169
|
*/
|
|
170
|
-
|
|
170
|
+
function avg(data: Array<number>): number;
|
|
171
171
|
/**
|
|
172
172
|
* Compute the median of a set of numbers.
|
|
173
173
|
* @param data An array of numeric inputs
|
|
@@ -177,7 +177,7 @@ export declare abstract class SMath {
|
|
|
177
177
|
* const y = SMath.median([2, 5, 3, 1]); // 2.5
|
|
178
178
|
* ```
|
|
179
179
|
*/
|
|
180
|
-
|
|
180
|
+
function median(data: Array<number>): number;
|
|
181
181
|
/**
|
|
182
182
|
* Compute the variance of a **complete population**.
|
|
183
183
|
* @param data An array of numeric inputs
|
|
@@ -187,7 +187,7 @@ export declare abstract class SMath {
|
|
|
187
187
|
* const y = SMath.varp([1, 2, 4, 4]); // 1.6875
|
|
188
188
|
* ```
|
|
189
189
|
*/
|
|
190
|
-
|
|
190
|
+
function varp(data: Array<number>): number;
|
|
191
191
|
/**
|
|
192
192
|
* Compute the variance of a **sample**.
|
|
193
193
|
* @param data An array of numeric inputs
|
|
@@ -197,7 +197,7 @@ export declare abstract class SMath {
|
|
|
197
197
|
* const y = SMath.vars([1, 2, 4, 4]); // 2.25
|
|
198
198
|
* ```
|
|
199
199
|
*/
|
|
200
|
-
|
|
200
|
+
function vars(data: Array<number>): number;
|
|
201
201
|
/**
|
|
202
202
|
* Compute the standard deviation of a **complete population**.
|
|
203
203
|
* @param data An array of numeric inputs
|
|
@@ -207,7 +207,7 @@ export declare abstract class SMath {
|
|
|
207
207
|
* const y = SMath.stdevp([1, 2, 3, 4]); // 1.118...
|
|
208
208
|
* ```
|
|
209
209
|
*/
|
|
210
|
-
|
|
210
|
+
function stdevp(data: Array<number>): number;
|
|
211
211
|
/**
|
|
212
212
|
* Compute the standard deviation of a **sample**.
|
|
213
213
|
* @param data An array of numeric inputs
|
|
@@ -217,7 +217,52 @@ export declare abstract class SMath {
|
|
|
217
217
|
* const y = SMath.stdevs([1, 2, 3, 4]); // 1.29...
|
|
218
218
|
* ```
|
|
219
219
|
*/
|
|
220
|
-
|
|
220
|
+
function 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
|
+
function 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
|
+
function 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
|
+
function 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
|
+
function 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.
|
|
@@ -231,7 +276,7 @@ export declare abstract class SMath {
|
|
|
231
276
|
* const y = SMath.lim(Math.log, 0); // -Infinity
|
|
232
277
|
* ```
|
|
233
278
|
*/
|
|
234
|
-
|
|
279
|
+
function lim(f: (x: number) => number, x: number, h?: number, discontinuity_cutoff?: number): number;
|
|
235
280
|
/**
|
|
236
281
|
* Take the derivative of a function.
|
|
237
282
|
* @param f Function `f(x)`
|
|
@@ -243,7 +288,7 @@ export declare abstract class SMath {
|
|
|
243
288
|
* const y = SMath.differentiate(x => 3 * x ** 2, 2); // 12
|
|
244
289
|
* ```
|
|
245
290
|
*/
|
|
246
|
-
|
|
291
|
+
function differentiate(f: (x: number) => number, x: number, h?: number): number;
|
|
247
292
|
/**
|
|
248
293
|
* Compute the definite integral of a function.
|
|
249
294
|
* @param f Function `f(x)`
|
|
@@ -256,5 +301,5 @@ export declare abstract class SMath {
|
|
|
256
301
|
* const y = SMath.integrate(x => 3 * x ** 2, 1, 2); // 7
|
|
257
302
|
* ```
|
|
258
303
|
*/
|
|
259
|
-
|
|
304
|
+
function integrate(f: (x: number) => number, a: number, b: number, Ndx?: number): number;
|
|
260
305
|
}
|