extra-math 1.3.12 โ 1.3.15
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 +35 -12
- package/index.d.ts +17 -17
- package/index.js +14 -11
- package/index.mjs +13 -10
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,11 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
A collection of common mathematical functions.<br>
|
|
2
2
|
๐ฆ [Node.js](https://www.npmjs.com/package/extra-math),
|
|
3
3
|
๐ [Web](https://www.npmjs.com/package/extra-math.web),
|
|
4
4
|
๐ [Files](https://unpkg.com/extra-math/),
|
|
5
5
|
๐ฐ [JSDoc](https://nodef.github.io/extra-math/),
|
|
6
6
|
๐ [Wiki](https://github.com/nodef/extra-math/wiki/).
|
|
7
7
|
|
|
8
|
-
Mathematics is the classification and study of all possible patterns. [(1)]
|
|
8
|
+
Mathematics is the classification and study of all possible patterns. [(1)] This
|
|
9
|
+
package includes common mathematical functions related to *natural numbers*, *real*
|
|
10
|
+
*numbers*, *geometry*, and *statistics*.
|
|
11
|
+
|
|
12
|
+
**Natural numbers**: There are 3 different ways of performing the *modulo*
|
|
13
|
+
operation: [rem], [mod], and [modp]. [gcd]/`HCF` and [lcm] of a list of numbers
|
|
14
|
+
can be obtained. The calculate the number of ways of *ordering items*, use
|
|
15
|
+
[factorial]/`P(n, k)`, [binomial]/`C(n, k)`, or [multinomial]/`n!/kโ!kโ!...`.
|
|
16
|
+
|
|
17
|
+
**Real numbers**: *Range* of a number can be controlled with [constrain],
|
|
18
|
+
[norm], or [map]. Use [lerp] for *linear interpolation* (or extrapolation);
|
|
19
|
+
[root] for calculating the *n-th root* of a number; and [log] to find the
|
|
20
|
+
*logarithm* of a number with a given base. Results of *special mathematical
|
|
21
|
+
functions* (approximations) for a given number can be obtained with [gamma],
|
|
22
|
+
[lgamma], [erf], and [erfc].
|
|
23
|
+
|
|
24
|
+
**Geometry**: Perform conversion from [degrees] to [radians] and vice versa.
|
|
25
|
+
Find the [magnitude] of a vector or [distance] between two points.
|
|
26
|
+
|
|
27
|
+
**Statistics**: For a list of numbers, we can calculate the [sum], [product],
|
|
28
|
+
[mean]. [median] gives the value lying in the middle when the numbers are
|
|
29
|
+
sorted, and [modes] gives the values which are repeated most often. The
|
|
30
|
+
difference between the largest and the smallest values is the [range].
|
|
31
|
+
[variance] is a measure of variability of numbers.
|
|
9
32
|
|
|
10
33
|
This package is available in both *Node.js* and *Web* formats. The web format is
|
|
11
34
|
exposed as `extra_math` standalone variable and can be loaded from [jsDelivr CDN].
|
|
@@ -24,16 +47,16 @@ const math = require('extra-math');
|
|
|
24
47
|
// import * as math from "https://unpkg.com/extra-math/index.mjs"; (deno)
|
|
25
48
|
|
|
26
49
|
math.sum(1, 2, 3, 4);
|
|
27
|
-
// 10
|
|
50
|
+
// โ 10
|
|
28
51
|
|
|
29
52
|
math.median(1, 7, 8);
|
|
30
|
-
// 7
|
|
53
|
+
// โ 7
|
|
31
54
|
|
|
32
55
|
math.variance(1, 2, 3, 4);
|
|
33
|
-
// 1.25
|
|
56
|
+
// โ 1.25
|
|
34
57
|
|
|
35
58
|
math.lcm(2, 3, 4);
|
|
36
|
-
// 12
|
|
59
|
+
// โ 12
|
|
37
60
|
```
|
|
38
61
|
|
|
39
62
|
<br>
|
|
@@ -54,11 +77,11 @@ math.lcm(2, 3, 4);
|
|
|
54
77
|
| [multinomial] | Find the number of ways to put n objects in m bins (n=sum(kแตข)). |
|
|
55
78
|
| | |
|
|
56
79
|
| [constrain] | Constrain a number within a minimum and a maximum value. |
|
|
57
|
-
| [root] | Find the nth root of a number (โฟโ). |
|
|
58
|
-
| [log] | Find the logarithm of a number with a given base. |
|
|
59
80
|
| [norm] | Normalize a number from its current range into a value between 0 and 1. |
|
|
60
81
|
| [map] | Re-map a number from one range to another. |
|
|
61
82
|
| [lerp] | Linearly interpolate a number between two numbers. |
|
|
83
|
+
| [root] | Find the nth root of a number (โฟโ). |
|
|
84
|
+
| [log] | Find the logarithm of a number with a given base. |
|
|
62
85
|
| [gamma] | Compute the gamma function of a number (ฮ). |
|
|
63
86
|
| [lgamma] | Compute the natural logarithm of the absolute value of the gamma function of a number (log-ฮ). |
|
|
64
87
|
| [erf] | Find error function value of number (approximation). |
|
|
@@ -66,8 +89,8 @@ math.lcm(2, 3, 4);
|
|
|
66
89
|
| | |
|
|
67
90
|
| [degrees] | Convert radians to degrees. |
|
|
68
91
|
| [radians] | Convert degrees to radians. |
|
|
69
|
-
| [
|
|
70
|
-
| [
|
|
92
|
+
| [magnitude] | Calculate the magnitude (length) of a vector. |
|
|
93
|
+
| [distance] | Calculate the distance between two points. |
|
|
71
94
|
| | |
|
|
72
95
|
| [sum] | Find the sum of numbers (ฮฃ). |
|
|
73
96
|
| [product] | Find the product of numbers (โ). |
|
|
@@ -121,8 +144,8 @@ math.lcm(2, 3, 4);
|
|
|
121
144
|
[erfc]: https://nodef.github.io/extra-math/modules.html#erfc
|
|
122
145
|
[degrees]: https://nodef.github.io/extra-math/modules.html#degrees
|
|
123
146
|
[radians]: https://nodef.github.io/extra-math/modules.html#radians
|
|
124
|
-
[
|
|
125
|
-
[
|
|
147
|
+
[magnitude]: https://nodef.github.io/extra-math/modules.html#magnitude
|
|
148
|
+
[distance]: https://nodef.github.io/extra-math/modules.html#distance
|
|
126
149
|
[sum]: https://nodef.github.io/extra-math/modules.html#sum
|
|
127
150
|
[product]: https://nodef.github.io/extra-math/modules.html#product
|
|
128
151
|
[mean]: https://nodef.github.io/extra-math/modules.html#mean
|
package/index.d.ts
CHANGED
|
@@ -59,20 +59,6 @@ declare function multinomial(...ks: number[]): number;
|
|
|
59
59
|
* @returns x<min: min, x>max: max, x
|
|
60
60
|
*/
|
|
61
61
|
declare function constrain(x: number, min: number, max: number): number;
|
|
62
|
-
/**
|
|
63
|
-
* Find the nth root of a number (โฟโ).
|
|
64
|
-
* @param x a number
|
|
65
|
-
* @param n root
|
|
66
|
-
* @returns โฟโx
|
|
67
|
-
*/
|
|
68
|
-
declare function root(x: number, n: number): number;
|
|
69
|
-
/**
|
|
70
|
-
* Find the logarithm of a number with a given base.
|
|
71
|
-
* @param x a number
|
|
72
|
-
* @param b logarithm base [e]
|
|
73
|
-
* @returns log_b (x)
|
|
74
|
-
*/
|
|
75
|
-
declare function log(x: number, b?: number): number;
|
|
76
62
|
/**
|
|
77
63
|
* Normalize a number from its current range into a value between 0 and 1.
|
|
78
64
|
* @param x a number
|
|
@@ -99,6 +85,20 @@ declare function map(x: number, r: number, R: number, t: number, T: number): num
|
|
|
99
85
|
* @returns โ [x, y]
|
|
100
86
|
*/
|
|
101
87
|
declare function lerp(x: number, y: number, t: number): number;
|
|
88
|
+
/**
|
|
89
|
+
* Find the nth root of a number (โฟโ).
|
|
90
|
+
* @param x a number
|
|
91
|
+
* @param n root
|
|
92
|
+
* @returns โฟโx
|
|
93
|
+
*/
|
|
94
|
+
declare function root(x: number, n: number): number;
|
|
95
|
+
/**
|
|
96
|
+
* Find the logarithm of a number with a given base.
|
|
97
|
+
* @param x a number
|
|
98
|
+
* @param b logarithm base [e]
|
|
99
|
+
* @returns log_b (x)
|
|
100
|
+
*/
|
|
101
|
+
declare function log(x: number, b?: number): number;
|
|
102
102
|
/**
|
|
103
103
|
* Compute the gamma function of a number (ฮ).
|
|
104
104
|
* @param x a number
|
|
@@ -140,14 +140,14 @@ declare function radians(x: number): number;
|
|
|
140
140
|
* @param xs vector ([x, y, z, ...])
|
|
141
141
|
* @returns โ(xยฒ + yยฒ + zยฒ)
|
|
142
142
|
*/
|
|
143
|
-
declare function
|
|
143
|
+
declare function magnitude(xs: number[]): number;
|
|
144
144
|
/**
|
|
145
145
|
* Calculate the distance between two points.
|
|
146
146
|
* @param xs first point ([x, y, z, ...])
|
|
147
147
|
* @param ys second point ([x, y, z, ...])
|
|
148
148
|
* @returns โ(ฮxยฒ + ฮyยฒ + ฮzยฒ)
|
|
149
149
|
*/
|
|
150
|
-
declare function
|
|
150
|
+
declare function distance(xs: number[], ys: number[]): number;
|
|
151
151
|
/**
|
|
152
152
|
* Find the sum of numbers (ฮฃ).
|
|
153
153
|
* @param xs a list of numbers
|
|
@@ -191,4 +191,4 @@ declare function range(...xs: number[]): number;
|
|
|
191
191
|
*/
|
|
192
192
|
declare function variance(...xs: number[]): number;
|
|
193
193
|
|
|
194
|
-
export { binomial, constrain, degrees,
|
|
194
|
+
export { binomial, constrain, degrees, distance, erf, erfc, factorial, gamma, gcd, lcm, lerp, lgamma, log, magnitude, map, mean, median, mod, modes, modp, multinomial, norm, product, radians, range, rem, root, sum, variance };
|
package/index.js
CHANGED
|
@@ -26,7 +26,10 @@ function gcd(...xs) {
|
|
|
26
26
|
return a;
|
|
27
27
|
}
|
|
28
28
|
function lcm(...xs) {
|
|
29
|
-
|
|
29
|
+
var a = xs[0] || 1;
|
|
30
|
+
for (var i = 1, I = xs.length; i < I; i++)
|
|
31
|
+
a = a * xs[i] / gcdPair(a, xs[i]);
|
|
32
|
+
return a;
|
|
30
33
|
}
|
|
31
34
|
function factorial(n, k = 0) {
|
|
32
35
|
if (n < 0)
|
|
@@ -58,12 +61,6 @@ function multinomial(...ks) {
|
|
|
58
61
|
function constrain(x, min, max) {
|
|
59
62
|
return Math.min(Math.max(x, min), max);
|
|
60
63
|
}
|
|
61
|
-
function root(x, n) {
|
|
62
|
-
return Math.pow(x, 1 / n);
|
|
63
|
-
}
|
|
64
|
-
function log(x, b = Math.E) {
|
|
65
|
-
return Math.log(x) / Math.log(b);
|
|
66
|
-
}
|
|
67
64
|
function norm(x, r, R) {
|
|
68
65
|
return (x - r) / (R - r);
|
|
69
66
|
}
|
|
@@ -73,6 +70,12 @@ function map(x, r, R, t, T) {
|
|
|
73
70
|
function lerp(x, y, t) {
|
|
74
71
|
return x + t * (y - x);
|
|
75
72
|
}
|
|
73
|
+
function root(x, n) {
|
|
74
|
+
return Math.pow(x, 1 / n);
|
|
75
|
+
}
|
|
76
|
+
function log(x, b = Math.E) {
|
|
77
|
+
return Math.log(x) / Math.log(b);
|
|
78
|
+
}
|
|
76
79
|
function gamma(x) {
|
|
77
80
|
var x = x - 1;
|
|
78
81
|
var e1 = Math.sqrt(2 * Math.PI * x);
|
|
@@ -126,13 +129,13 @@ function degrees(x) {
|
|
|
126
129
|
function radians(x) {
|
|
127
130
|
return x * (Math.PI / 180);
|
|
128
131
|
}
|
|
129
|
-
function
|
|
132
|
+
function magnitude(xs) {
|
|
130
133
|
var a = 0;
|
|
131
134
|
for (var i = 0, I = xs.length; i < I; i++)
|
|
132
135
|
a += xs[i] ** 2;
|
|
133
136
|
return Math.sqrt(a);
|
|
134
137
|
}
|
|
135
|
-
function
|
|
138
|
+
function distance(xs, ys) {
|
|
136
139
|
var a = 0;
|
|
137
140
|
for (var i = 0, I = xs.length; i < I; i++)
|
|
138
141
|
a += (xs[i] - ys[i]) ** 2;
|
|
@@ -204,7 +207,7 @@ function variance(...xs) {
|
|
|
204
207
|
exports.binomial = binomial;
|
|
205
208
|
exports.constrain = constrain;
|
|
206
209
|
exports.degrees = degrees;
|
|
207
|
-
exports.
|
|
210
|
+
exports.distance = distance;
|
|
208
211
|
exports.erf = erf;
|
|
209
212
|
exports.erfc = erfc;
|
|
210
213
|
exports.factorial = factorial;
|
|
@@ -214,7 +217,7 @@ exports.lcm = lcm;
|
|
|
214
217
|
exports.lerp = lerp;
|
|
215
218
|
exports.lgamma = lgamma;
|
|
216
219
|
exports.log = log;
|
|
217
|
-
exports.
|
|
220
|
+
exports.magnitude = magnitude;
|
|
218
221
|
exports.map = map;
|
|
219
222
|
exports.mean = mean;
|
|
220
223
|
exports.median = median;
|
package/index.mjs
CHANGED
|
@@ -22,7 +22,10 @@ function gcd(...xs) {
|
|
|
22
22
|
return a;
|
|
23
23
|
}
|
|
24
24
|
function lcm(...xs) {
|
|
25
|
-
|
|
25
|
+
var a = xs[0] || 1;
|
|
26
|
+
for (var i = 1, I = xs.length; i < I; i++)
|
|
27
|
+
a = a * xs[i] / gcdPair(a, xs[i]);
|
|
28
|
+
return a;
|
|
26
29
|
}
|
|
27
30
|
function factorial(n, k = 0) {
|
|
28
31
|
if (n < 0)
|
|
@@ -54,12 +57,6 @@ function multinomial(...ks) {
|
|
|
54
57
|
function constrain(x, min, max) {
|
|
55
58
|
return Math.min(Math.max(x, min), max);
|
|
56
59
|
}
|
|
57
|
-
function root(x, n) {
|
|
58
|
-
return Math.pow(x, 1 / n);
|
|
59
|
-
}
|
|
60
|
-
function log(x, b = Math.E) {
|
|
61
|
-
return Math.log(x) / Math.log(b);
|
|
62
|
-
}
|
|
63
60
|
function norm(x, r, R) {
|
|
64
61
|
return (x - r) / (R - r);
|
|
65
62
|
}
|
|
@@ -69,6 +66,12 @@ function map(x, r, R, t, T) {
|
|
|
69
66
|
function lerp(x, y, t) {
|
|
70
67
|
return x + t * (y - x);
|
|
71
68
|
}
|
|
69
|
+
function root(x, n) {
|
|
70
|
+
return Math.pow(x, 1 / n);
|
|
71
|
+
}
|
|
72
|
+
function log(x, b = Math.E) {
|
|
73
|
+
return Math.log(x) / Math.log(b);
|
|
74
|
+
}
|
|
72
75
|
function gamma(x) {
|
|
73
76
|
var x = x - 1;
|
|
74
77
|
var e1 = Math.sqrt(2 * Math.PI * x);
|
|
@@ -122,13 +125,13 @@ function degrees(x) {
|
|
|
122
125
|
function radians(x) {
|
|
123
126
|
return x * (Math.PI / 180);
|
|
124
127
|
}
|
|
125
|
-
function
|
|
128
|
+
function magnitude(xs) {
|
|
126
129
|
var a = 0;
|
|
127
130
|
for (var i = 0, I = xs.length; i < I; i++)
|
|
128
131
|
a += xs[i] ** 2;
|
|
129
132
|
return Math.sqrt(a);
|
|
130
133
|
}
|
|
131
|
-
function
|
|
134
|
+
function distance(xs, ys) {
|
|
132
135
|
var a = 0;
|
|
133
136
|
for (var i = 0, I = xs.length; i < I; i++)
|
|
134
137
|
a += (xs[i] - ys[i]) ** 2;
|
|
@@ -197,4 +200,4 @@ function variance(...xs) {
|
|
|
197
200
|
return a / xs.length;
|
|
198
201
|
}
|
|
199
202
|
|
|
200
|
-
export { binomial, constrain, degrees,
|
|
203
|
+
export { binomial, constrain, degrees, distance, erf, erfc, factorial, gamma, gcd, lcm, lerp, lgamma, log, magnitude, map, mean, median, mod, modes, modp, multinomial, norm, product, radians, range, rem, root, sum, variance };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extra-math",
|
|
3
|
-
"version": "1.3.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.3.15",
|
|
4
|
+
"description": "A collection of common mathematical functions.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"binomial",
|
|
21
21
|
"constrain",
|
|
22
22
|
"degrees",
|
|
23
|
-
"
|
|
23
|
+
"distance",
|
|
24
24
|
"erf",
|
|
25
25
|
"erfc",
|
|
26
26
|
"factorial",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"lerp",
|
|
31
31
|
"lgamma",
|
|
32
32
|
"log",
|
|
33
|
-
"
|
|
33
|
+
"magnitude",
|
|
34
34
|
"map",
|
|
35
35
|
"mean",
|
|
36
36
|
"median",
|