extra-math 1.3.29 → 1.3.32
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 +27 -27
- package/index.d.ts +279 -80
- package/index.js +139 -78
- package/index.mjs +123 -76
- package/package.json +21 -6
- package/jest.config.js +0 -8
package/README.md
CHANGED
|
@@ -127,30 +127,30 @@ math.lcm(2, 3, 4);
|
|
|
127
127
|
[](https://codeclimate.com/github/nodef/extra-math/maintainability)
|
|
128
128
|
|
|
129
129
|
|
|
130
|
-
[rem]: https://nodef.github.io/extra-math/
|
|
131
|
-
[mod]: https://nodef.github.io/extra-math/
|
|
132
|
-
[modp]: https://nodef.github.io/extra-math/
|
|
133
|
-
[gcd]: https://nodef.github.io/extra-math/
|
|
134
|
-
[lcm]: https://nodef.github.io/extra-math/
|
|
135
|
-
[factorial]: https://nodef.github.io/extra-math/
|
|
136
|
-
[binomial]: https://nodef.github.io/extra-math/
|
|
137
|
-
[multinomial]: https://nodef.github.io/extra-math/
|
|
138
|
-
[constrain]: https://nodef.github.io/extra-math/
|
|
139
|
-
[root]: https://nodef.github.io/extra-math/
|
|
140
|
-
[log]: https://nodef.github.io/extra-math/
|
|
141
|
-
[normalize]: https://nodef.github.io/extra-math/
|
|
142
|
-
[remap]: https://nodef.github.io/extra-math/
|
|
143
|
-
[lerp]: https://nodef.github.io/extra-math/
|
|
144
|
-
[erf]: https://nodef.github.io/extra-math/
|
|
145
|
-
[erfc]: https://nodef.github.io/extra-math/
|
|
146
|
-
[degrees]: https://nodef.github.io/extra-math/
|
|
147
|
-
[radians]: https://nodef.github.io/extra-math/
|
|
148
|
-
[magnitude]: https://nodef.github.io/extra-math/
|
|
149
|
-
[distance]: https://nodef.github.io/extra-math/
|
|
150
|
-
[sum]: https://nodef.github.io/extra-math/
|
|
151
|
-
[product]: https://nodef.github.io/extra-math/
|
|
152
|
-
[mean]: https://nodef.github.io/extra-math/
|
|
153
|
-
[median]: https://nodef.github.io/extra-math/
|
|
154
|
-
[modes]: https://nodef.github.io/extra-math/
|
|
155
|
-
[range]: https://nodef.github.io/extra-math/
|
|
156
|
-
[variance]: https://nodef.github.io/extra-math/
|
|
130
|
+
[rem]: https://nodef.github.io/extra-math/functions/rem.html
|
|
131
|
+
[mod]: https://nodef.github.io/extra-math/functions/mod.html
|
|
132
|
+
[modp]: https://nodef.github.io/extra-math/functions/modp.html
|
|
133
|
+
[gcd]: https://nodef.github.io/extra-math/functions/gcd.html
|
|
134
|
+
[lcm]: https://nodef.github.io/extra-math/functions/lcm.html
|
|
135
|
+
[factorial]: https://nodef.github.io/extra-math/functions/factorial.html
|
|
136
|
+
[binomial]: https://nodef.github.io/extra-math/functions/binomial.html
|
|
137
|
+
[multinomial]: https://nodef.github.io/extra-math/functions/multinomial.html
|
|
138
|
+
[constrain]: https://nodef.github.io/extra-math/functions/constrain.html
|
|
139
|
+
[root]: https://nodef.github.io/extra-math/functions/root.html
|
|
140
|
+
[log]: https://nodef.github.io/extra-math/functions/log.html
|
|
141
|
+
[normalize]: https://nodef.github.io/extra-math/functions/normalize.html
|
|
142
|
+
[remap]: https://nodef.github.io/extra-math/functions/remap.html
|
|
143
|
+
[lerp]: https://nodef.github.io/extra-math/functions/lerp.html
|
|
144
|
+
[erf]: https://nodef.github.io/extra-math/functions/erf.html
|
|
145
|
+
[erfc]: https://nodef.github.io/extra-math/functions/erfc.html
|
|
146
|
+
[degrees]: https://nodef.github.io/extra-math/functions/degrees.html
|
|
147
|
+
[radians]: https://nodef.github.io/extra-math/functions/radians.html
|
|
148
|
+
[magnitude]: https://nodef.github.io/extra-math/functions/magnitude.html
|
|
149
|
+
[distance]: https://nodef.github.io/extra-math/functions/distance.html
|
|
150
|
+
[sum]: https://nodef.github.io/extra-math/functions/sum.html
|
|
151
|
+
[product]: https://nodef.github.io/extra-math/functions/product.html
|
|
152
|
+
[mean]: https://nodef.github.io/extra-math/functions/mean.html
|
|
153
|
+
[median]: https://nodef.github.io/extra-math/functions/median.html
|
|
154
|
+
[modes]: https://nodef.github.io/extra-math/functions/modes.html
|
|
155
|
+
[range]: https://nodef.github.io/extra-math/functions/range.html
|
|
156
|
+
[variance]: https://nodef.github.io/extra-math/functions/variance.html
|
package/index.d.ts
CHANGED
|
@@ -1,75 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Round down a number to specific precision.
|
|
3
|
+
* [📘](https://github.com/nodef/extra-number/wiki/floor)
|
|
4
|
+
* [📘](https://github.com/nodef/extra-math/wiki/floor)
|
|
5
|
+
* @param x a number
|
|
6
|
+
* @param pre to precision (1)
|
|
7
|
+
* */
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
declare function floor(x: number, pre?: number): number;
|
|
11
|
+
/**
|
|
12
|
+
* Round up a number to specific precision.
|
|
13
|
+
* [📘](https://github.com/nodef/extra-number/wiki/ceil)
|
|
14
|
+
* [📘](https://github.com/nodef/extra-math/wiki/ceil)
|
|
15
|
+
* @param x a number
|
|
16
|
+
* @param pre to precision [1]
|
|
17
|
+
* */
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
declare function ceil(x: number, pre?: number): number;
|
|
21
|
+
/**
|
|
22
|
+
* Round a number to specific precision.
|
|
23
|
+
* [📘](https://github.com/nodef/extra-number/wiki/round)
|
|
24
|
+
* [📘](https://github.com/nodef/extra-math/wiki/round)
|
|
25
|
+
* @param x a number
|
|
26
|
+
* @param pre to precision [1]
|
|
27
|
+
* */
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
declare function round(x: number, pre?: number): number;
|
|
31
|
+
/**
|
|
32
|
+
* Perform floor-divison of two numbers.
|
|
33
|
+
* [📘](https://github.com/nodef/extra-number/wiki/floorDiv)
|
|
34
|
+
* [📘](https://github.com/nodef/extra-math/wiki/floorDiv)
|
|
35
|
+
* @param x divisor
|
|
36
|
+
* @param y dividend
|
|
37
|
+
* @returns ⌊x/y⌋
|
|
38
|
+
* */
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
declare function floorDiv(x: number, y: number): number;
|
|
42
|
+
/**
|
|
43
|
+
* Perform ceiling-divison of two numbers.
|
|
44
|
+
* [📘](https://github.com/nodef/extra-number/wiki/ceilDiv)
|
|
45
|
+
* [📘](https://github.com/nodef/extra-math/wiki/ceilDiv)
|
|
46
|
+
* @param x divisor
|
|
47
|
+
* @param y dividend
|
|
48
|
+
* @returns ⌈x/y⌉
|
|
49
|
+
* */
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
declare function ceilDiv(x: number, y: number): number;
|
|
53
|
+
/**
|
|
54
|
+
* Perform rounded-divison of two numbers.
|
|
55
|
+
* [📘](https://github.com/nodef/extra-number/wiki/roundDiv)
|
|
56
|
+
* [📘](https://github.com/nodef/extra-math/wiki/roundDiv)
|
|
57
|
+
* @param x divisor
|
|
58
|
+
* @param y dividend
|
|
59
|
+
* @returns [x/y]
|
|
60
|
+
* */
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
declare function roundDiv(x: number, y: number): number;
|
|
1
64
|
/**
|
|
2
65
|
* Find the remainder of x/y with sign of x (truncated division).
|
|
66
|
+
* [📘](https://github.com/nodef/extra-number/wiki/rem)
|
|
3
67
|
* [📘](https://github.com/nodef/extra-math/wiki/rem)
|
|
4
68
|
* @param x dividend
|
|
5
69
|
* @param y divisor
|
|
6
70
|
* @returns trunc(x/y)
|
|
7
71
|
* */
|
|
8
72
|
|
|
73
|
+
|
|
9
74
|
declare function rem(x: number, y: number): number;
|
|
10
75
|
/**
|
|
11
76
|
* Find the remainder of x/y with sign of y (floored division).
|
|
77
|
+
* [📘](https://github.com/nodef/extra-number/wiki/mod)
|
|
12
78
|
* [📘](https://github.com/nodef/extra-math/wiki/mod)
|
|
13
79
|
* @param x dividend
|
|
14
80
|
* @param y divisor
|
|
15
81
|
* @returns floor(x/y)
|
|
16
82
|
* */
|
|
17
83
|
|
|
84
|
+
|
|
18
85
|
declare function mod(x: number, y: number): number;
|
|
19
86
|
/**
|
|
20
87
|
* Find the remainder of x/y with +ve sign (euclidean division).
|
|
88
|
+
* [📘](https://github.com/nodef/extra-number/wiki/modp)
|
|
21
89
|
* [📘](https://github.com/nodef/extra-math/wiki/modp)
|
|
22
90
|
* @param x dividend
|
|
23
91
|
* @param y divisor
|
|
24
92
|
* @returns n>0: floor(x/y), n<0: ceil(x/y)
|
|
25
93
|
* */
|
|
26
94
|
|
|
27
|
-
declare function modp(x: number, y: number): number;
|
|
28
|
-
/**
|
|
29
|
-
* Find the greatest common divisor of numbers.
|
|
30
|
-
* [📘](https://github.com/nodef/extra-math/wiki/gcd)
|
|
31
|
-
* @param xs a list of numbers
|
|
32
|
-
* @returns gcd(x₁, x₂, ...)
|
|
33
|
-
* */
|
|
34
|
-
|
|
35
|
-
declare function gcd(...xs: number[]): number;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Find the least common multiple of numbers.
|
|
39
|
-
* [📘](https://github.com/nodef/extra-math/wiki/lcm)
|
|
40
|
-
* @param xs a list of numbers
|
|
41
|
-
* @returns lcm(x₁, x₂, ...)
|
|
42
|
-
* */
|
|
43
|
-
|
|
44
|
-
declare function lcm(...xs: number[]): number;
|
|
45
|
-
/**
|
|
46
|
-
* Find the factorial of a number.
|
|
47
|
-
* [📘](https://github.com/nodef/extra-math/wiki/factorial)
|
|
48
|
-
* @param n a number
|
|
49
|
-
* @param k denominator factorial [0]
|
|
50
|
-
* @returns P(n, k); k=0: n!, k>0: n!/k!
|
|
51
|
-
* */
|
|
52
|
-
|
|
53
|
-
declare function factorial(n: number, k?: number): number;
|
|
54
|
-
/**
|
|
55
|
-
* Find the number of ways to choose k elements from a set of n elements.
|
|
56
|
-
* [📘](https://github.com/nodef/extra-math/wiki/binomial)
|
|
57
|
-
* @param n elements in source set
|
|
58
|
-
* @param k elements in choose set
|
|
59
|
-
* @returns C(n, k)
|
|
60
|
-
* */
|
|
61
|
-
|
|
62
|
-
declare function binomial(n: number, k: number): number;
|
|
63
|
-
/**
|
|
64
|
-
* Find the number of ways to put n objects in m bins (n=sum(kᵢ)).
|
|
65
|
-
* [📘](https://github.com/nodef/extra-math/wiki/multinomial)
|
|
66
|
-
* @param ks objects per bin (kᵢ)
|
|
67
|
-
* @returns n!/(k₁!k₂!...) | n=sum(kᵢ)
|
|
68
|
-
* */
|
|
69
95
|
|
|
70
|
-
declare function
|
|
96
|
+
declare function modp(x: number, y: number): number;
|
|
71
97
|
/**
|
|
72
98
|
* Constrain a number within a minimum and a maximum value.
|
|
99
|
+
* [📘](https://github.com/nodef/extra-number/wiki/constrain)
|
|
73
100
|
* [📘](https://github.com/nodef/extra-math/wiki/constrain)
|
|
74
101
|
* @param x a number
|
|
75
102
|
* @param min minimum value
|
|
@@ -77,10 +104,12 @@ declare function multinomial(...ks: number[]): number;
|
|
|
77
104
|
* @returns x<min: min, x>max: max, x
|
|
78
105
|
* */
|
|
79
106
|
|
|
107
|
+
|
|
80
108
|
declare function constrain(x: number, min: number, max: number): number;
|
|
81
109
|
|
|
82
110
|
/**
|
|
83
111
|
* Normalize a number from its current range into a value between 0 and 1.
|
|
112
|
+
* [📘](https://github.com/nodef/extra-number/wiki/normalize)
|
|
84
113
|
* [📘](https://github.com/nodef/extra-math/wiki/normalize)
|
|
85
114
|
* @param x a number
|
|
86
115
|
* @param r lower bound of current range
|
|
@@ -88,10 +117,12 @@ declare function constrain(x: number, min: number, max: number): number;
|
|
|
88
117
|
* @returns ∈ [0, 1]
|
|
89
118
|
* */
|
|
90
119
|
|
|
120
|
+
|
|
91
121
|
declare function normalize(x: number, r: number, R: number): number;
|
|
92
122
|
|
|
93
123
|
/**
|
|
94
124
|
* Re-map a number from one range to another.
|
|
125
|
+
* [📘](https://github.com/nodef/extra-number/wiki/remap)
|
|
95
126
|
* [📘](https://github.com/nodef/extra-math/wiki/remap)
|
|
96
127
|
* @param x a number
|
|
97
128
|
* @param r lower bound of current range
|
|
@@ -101,10 +132,12 @@ declare function normalize(x: number, r: number, R: number): number;
|
|
|
101
132
|
* @returns ∈ [ymin, ymax]
|
|
102
133
|
* */
|
|
103
134
|
|
|
135
|
+
|
|
104
136
|
declare function remap(x: number, r: number, R: number, t: number, T: number): number;
|
|
105
137
|
|
|
106
138
|
/**
|
|
107
139
|
* Linearly interpolate a number between two numbers.
|
|
140
|
+
* [📘](https://github.com/nodef/extra-number/wiki/lerp)
|
|
108
141
|
* [📘](https://github.com/nodef/extra-math/wiki/lerp)
|
|
109
142
|
* @param x start number
|
|
110
143
|
* @param y stop number
|
|
@@ -112,129 +145,295 @@ declare function remap(x: number, r: number, R: number, t: number, T: number): n
|
|
|
112
145
|
* @returns ∈ [x, y]
|
|
113
146
|
* */
|
|
114
147
|
|
|
148
|
+
|
|
115
149
|
declare function lerp(x: number, y: number, t: number): number;
|
|
150
|
+
/**
|
|
151
|
+
* Check if a number is a power-of-n.
|
|
152
|
+
* [📘](https://github.com/nodef/extra-number/wiki/isPow)
|
|
153
|
+
* [📘](https://github.com/nodef/extra-math/wiki/isPow)
|
|
154
|
+
* @param x a number
|
|
155
|
+
* @param n base
|
|
156
|
+
* @returns nⁱ = x? | i = +ve integer
|
|
157
|
+
* */
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
declare function isPow(x: number, n: number): boolean;
|
|
161
|
+
/**
|
|
162
|
+
* Get previous power-of-n of a number.
|
|
163
|
+
* [📘](https://github.com/nodef/extra-number/wiki/prevPow)
|
|
164
|
+
* [📘](https://github.com/nodef/extra-math/wiki/prevPow)
|
|
165
|
+
* @param x a number
|
|
166
|
+
* @param n base
|
|
167
|
+
* @returns 2ⁱ | 2ⁱ < x and 2ⁱ ≥ x/2
|
|
168
|
+
* */
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
declare function prevPow(x: number, n: number): number;
|
|
172
|
+
/**
|
|
173
|
+
* Find next power-of-n of a number.
|
|
174
|
+
* [📘](https://github.com/nodef/extra-number/wiki/nextPow)
|
|
175
|
+
* [📘](https://github.com/nodef/extra-math/wiki/nextPow)
|
|
176
|
+
* @param x a number
|
|
177
|
+
* @param n base
|
|
178
|
+
* @returns 2ⁱ | 2ⁱ > x and 2ⁱ ≤ 2x
|
|
179
|
+
* */
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
declare function nextPow(x: number, n: number): number;
|
|
116
183
|
/**
|
|
117
184
|
* Find the nth root of a number (ⁿ√).
|
|
185
|
+
* [📘](https://github.com/nodef/extra-number/wiki/root)
|
|
118
186
|
* [📘](https://github.com/nodef/extra-math/wiki/root)
|
|
119
187
|
* @param x a number
|
|
120
188
|
* @param n root
|
|
121
189
|
* @returns ⁿ√x
|
|
122
190
|
* */
|
|
123
191
|
|
|
192
|
+
|
|
124
193
|
declare function root(x: number, n: number): number;
|
|
125
194
|
/**
|
|
126
195
|
* Find the logarithm of a number with a given base.
|
|
196
|
+
* [📘](https://github.com/nodef/extra-number/wiki/log)
|
|
127
197
|
* [📘](https://github.com/nodef/extra-math/wiki/log)
|
|
128
198
|
* @param x a number
|
|
129
199
|
* @param b logarithm base [e]
|
|
130
200
|
* @returns log_b (x)
|
|
131
201
|
* */
|
|
132
202
|
|
|
203
|
+
|
|
133
204
|
declare function log(x: number, b?: number): number;
|
|
134
205
|
/**
|
|
135
|
-
*
|
|
136
|
-
* [📘](https://github.com/nodef/extra-
|
|
206
|
+
* List all divisors of a number, except itself.
|
|
207
|
+
* [📘](https://github.com/nodef/extra-number/wiki/properDivisors)
|
|
208
|
+
* [📘](https://github.com/nodef/extra-math/wiki/properDivisors)
|
|
137
209
|
* @param x a number
|
|
138
|
-
* @returns
|
|
210
|
+
* @returns proper divisors (factors)
|
|
139
211
|
* */
|
|
140
212
|
|
|
141
|
-
|
|
213
|
+
|
|
214
|
+
declare function properDivisors(x: number): number[];
|
|
215
|
+
/**
|
|
216
|
+
* Sum all proper divisors of a number.
|
|
217
|
+
* [📘](https://github.com/nodef/extra-number/wiki/aliquotSum)
|
|
218
|
+
* [📘](https://github.com/nodef/extra-math/wiki/aliquotSum)
|
|
219
|
+
* @param x a number
|
|
220
|
+
* @returns Σdᵢ | dᵢ is a divisor of x and ≠x
|
|
221
|
+
* */
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
declare function aliquotSum(x: number): number;
|
|
142
225
|
/**
|
|
143
|
-
*
|
|
144
|
-
* [📘](https://github.com/nodef/extra-
|
|
226
|
+
* Check if number is prime.
|
|
227
|
+
* [📘](https://github.com/nodef/extra-number/wiki/isPrime)
|
|
228
|
+
* [📘](https://github.com/nodef/extra-math/wiki/isPrime)
|
|
145
229
|
* @param x a number
|
|
146
|
-
* @returns
|
|
230
|
+
* @returns is divisible by 1 and itself only?
|
|
147
231
|
* */
|
|
148
232
|
|
|
149
|
-
|
|
233
|
+
|
|
234
|
+
declare function isPrime(x: number): boolean;
|
|
235
|
+
/**
|
|
236
|
+
* Find the greatest common divisor of numbers.
|
|
237
|
+
* [📘](https://github.com/nodef/extra-number/wiki/gcd)
|
|
238
|
+
* [📘](https://github.com/nodef/extra-math/wiki/gcd)
|
|
239
|
+
* @param xs a list of numbers
|
|
240
|
+
* @returns gcd(x₁, x₂, ...)
|
|
241
|
+
* */
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
declare function gcd(...xs: number[]): number;
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Find the least common multiple of numbers.
|
|
248
|
+
* [📘](https://github.com/nodef/extra-number/wiki/lcm)
|
|
249
|
+
* [📘](https://github.com/nodef/extra-math/wiki/lcm)
|
|
250
|
+
* @param xs a list of numbers
|
|
251
|
+
* @returns lcm(x₁, x₂, ...)
|
|
252
|
+
* */
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
declare function lcm(...xs: number[]): number;
|
|
256
|
+
/**
|
|
257
|
+
* Find the factorial of a number.
|
|
258
|
+
* [📘](https://github.com/nodef/extra-number/wiki/factorial)
|
|
259
|
+
* [📘](https://github.com/nodef/extra-math/wiki/factorial)
|
|
260
|
+
* @param n a number
|
|
261
|
+
* @param k denominator factorial [0]
|
|
262
|
+
* @returns P(n, k); k=0: n!, k>0: n!/k!
|
|
263
|
+
* */
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
declare function factorial(n: number, k?: number): number;
|
|
267
|
+
/**
|
|
268
|
+
* Find the number of ways to choose k elements from a set of n elements.
|
|
269
|
+
* [📘](https://github.com/nodef/extra-number/wiki/binomial)
|
|
270
|
+
* [📘](https://github.com/nodef/extra-math/wiki/binomial)
|
|
271
|
+
* @param n elements in source set
|
|
272
|
+
* @param k elements in choose set
|
|
273
|
+
* @returns C(n, k)
|
|
274
|
+
* */
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
declare function binomial(n: number, k: number): number;
|
|
278
|
+
/**
|
|
279
|
+
* Find the number of ways to put n objects in m bins (n=sum(kᵢ)).
|
|
280
|
+
* [📘](https://github.com/nodef/extra-number/wiki/multinomial)
|
|
281
|
+
* [📘](https://github.com/nodef/extra-math/wiki/multinomial)
|
|
282
|
+
* @param ks objects per bin (kᵢ)
|
|
283
|
+
* @returns n!/(k₁!k₂!...) | n=sum(kᵢ)
|
|
284
|
+
* */
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
declare function multinomial(...ks: number[]): number;
|
|
150
288
|
/**
|
|
151
289
|
* Convert radians to degrees.
|
|
290
|
+
* [📘](https://github.com/nodef/extra-number/wiki/degrees)
|
|
152
291
|
* [📘](https://github.com/nodef/extra-math/wiki/degrees)
|
|
153
292
|
* @param x radians
|
|
154
293
|
* @returns 2π → 360
|
|
155
294
|
* */
|
|
156
295
|
|
|
296
|
+
|
|
157
297
|
declare function degrees(x: number): number;
|
|
158
298
|
/**
|
|
159
299
|
* Convert degrees to radians.
|
|
300
|
+
* [📘](https://github.com/nodef/extra-number/wiki/radians)
|
|
160
301
|
* [📘](https://github.com/nodef/extra-math/wiki/radians)
|
|
161
302
|
* @param x degrees
|
|
162
303
|
* @returns 360 → 2π
|
|
163
304
|
* */
|
|
164
305
|
|
|
165
|
-
declare function radians(x: number): number;
|
|
166
|
-
/**
|
|
167
|
-
* Calculate the magnitude (length) of a vector.
|
|
168
|
-
* [📘](https://github.com/nodef/extra-math/wiki/magnitude)
|
|
169
|
-
* @param xs vector ([x, y, z, ...])
|
|
170
|
-
* @returns √(x² + y² + z²)
|
|
171
|
-
* */
|
|
172
|
-
|
|
173
|
-
declare function magnitude(xs: number[]): number;
|
|
174
|
-
/**
|
|
175
|
-
* Calculate the distance between two points.
|
|
176
|
-
* [📘](https://github.com/nodef/extra-math/wiki/distance)
|
|
177
|
-
* @param xs first point ([x, y, z, ...])
|
|
178
|
-
* @param ys second point ([x, y, z, ...])
|
|
179
|
-
* @returns √(Δx² + Δy² + Δz²)
|
|
180
|
-
* */
|
|
181
306
|
|
|
182
|
-
declare function
|
|
307
|
+
declare function radians(x: number): number;
|
|
183
308
|
/**
|
|
184
309
|
* Find the sum of numbers (Σ).
|
|
310
|
+
* [📘](https://github.com/nodef/extra-number/wiki/sum)
|
|
185
311
|
* [📘](https://github.com/nodef/extra-math/wiki/sum)
|
|
186
312
|
* @param xs a list of numbers
|
|
187
313
|
* @returns Σxᵢ
|
|
188
314
|
* */
|
|
189
315
|
|
|
316
|
+
|
|
190
317
|
declare function sum(...xs: number[]): number;
|
|
191
318
|
/**
|
|
192
319
|
* Find the product of numbers (∏).
|
|
320
|
+
* [📘](https://github.com/nodef/extra-number/wiki/product)
|
|
193
321
|
* [📘](https://github.com/nodef/extra-math/wiki/product)
|
|
194
322
|
* @param xs a list of numbers
|
|
195
323
|
* @returns ∏xᵢ
|
|
196
324
|
* */
|
|
197
325
|
|
|
198
|
-
declare function product(...xs: number[]): number;
|
|
199
|
-
/**
|
|
200
|
-
* Find the average of numbers.
|
|
201
|
-
* [📘](https://github.com/nodef/extra-math/wiki/mean)
|
|
202
|
-
* @param xs a list of numbers
|
|
203
|
-
* @returns Σxᵢ/n | n = size(xs)
|
|
204
|
-
* */
|
|
205
326
|
|
|
206
|
-
declare function
|
|
327
|
+
declare function product(...xs: number[]): number;
|
|
207
328
|
/**
|
|
208
329
|
* Find the value separating the higher and lower halves of numbers.
|
|
330
|
+
* [📘](https://github.com/nodef/extra-number/wiki/median)
|
|
209
331
|
* [📘](https://github.com/nodef/extra-math/wiki/median)
|
|
210
332
|
* @param xs a list of numbers
|
|
211
333
|
* @returns xₘ | sort(xs) = [..., xₘ, ...]
|
|
212
334
|
* */
|
|
213
335
|
|
|
336
|
+
|
|
214
337
|
declare function median(...xs: number[]): number;
|
|
215
338
|
/**
|
|
216
339
|
* Find the values that appear most often.
|
|
340
|
+
* [📘](https://github.com/nodef/extra-number/wiki/modes)
|
|
217
341
|
* [📘](https://github.com/nodef/extra-math/wiki/modes)
|
|
218
342
|
* @param xs a list of numbers
|
|
219
343
|
* @returns [xₘ₁, xₘ₂, ...] | count(xₘᵢ) ≥ count(xᵢ) ∀ xᵢ ∈ xs
|
|
220
344
|
* */
|
|
221
345
|
|
|
346
|
+
|
|
222
347
|
declare function modes(...xs: number[]): number[];
|
|
223
348
|
/**
|
|
224
|
-
* Find the
|
|
349
|
+
* Find the smallest and largest values.
|
|
350
|
+
* [📘](https://github.com/nodef/extra-number/wiki/range)
|
|
225
351
|
* [📘](https://github.com/nodef/extra-math/wiki/range)
|
|
226
352
|
* @param xs a list of numbers
|
|
227
|
-
* @returns
|
|
353
|
+
* @returns [min(xs), max(xs)]
|
|
228
354
|
* */
|
|
229
355
|
|
|
230
|
-
|
|
356
|
+
|
|
357
|
+
declare function range(...xs: number[]): [number, number];
|
|
231
358
|
/**
|
|
232
359
|
* Find the mean of squared deviation of numbers from its mean.
|
|
360
|
+
* [📘](https://github.com/nodef/extra-number/wiki/variance)
|
|
233
361
|
* [📘](https://github.com/nodef/extra-math/wiki/variance)
|
|
234
362
|
* @param xs a list of numbers
|
|
235
363
|
* @returns σ² = E[(xs - µ)²] | µ = mean(xs)
|
|
236
364
|
* */
|
|
237
365
|
|
|
366
|
+
|
|
238
367
|
declare function variance(...xs: number[]): number;
|
|
368
|
+
/**
|
|
369
|
+
* Find the average of numbers.
|
|
370
|
+
* [📘](https://github.com/nodef/extra-number/wiki/arithmeticMean)
|
|
371
|
+
* [📘](https://github.com/nodef/extra-math/wiki/arithmeticMean)
|
|
372
|
+
* @param xs a list of numbers
|
|
373
|
+
* @returns Σxᵢ/n | n = size(xs)
|
|
374
|
+
* */
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
declare function arithmeticMean(...xs: number[]): number;
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Find the geometric mean of numbers.
|
|
381
|
+
* [📘](https://github.com/nodef/extra-number/wiki/geometricMean)
|
|
382
|
+
* [📘](https://github.com/nodef/extra-math/wiki/geometricMean)
|
|
383
|
+
* @param xs a list of numbers
|
|
384
|
+
* @returns ⁿ√(∏xᵢ) | n = size(xs)
|
|
385
|
+
* */
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
declare function geometricMean(...xs: number[]): number;
|
|
389
|
+
/**
|
|
390
|
+
* Find the harmonic mean of numbers.
|
|
391
|
+
* [📘](https://github.com/nodef/extra-number/wiki/harmonicMean)
|
|
392
|
+
* [📘](https://github.com/nodef/extra-math/wiki/harmonicMean)
|
|
393
|
+
* @param xs a list of numbers
|
|
394
|
+
* @returns n/Σ(1/xᵢ) | n = size(xs)
|
|
395
|
+
* */
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
declare function harmonicMean(...xs: number[]): number;
|
|
399
|
+
/**
|
|
400
|
+
* Find the quadriatic mean of numbers.
|
|
401
|
+
* [📘](https://github.com/nodef/extra-number/wiki/quadriaticMean)
|
|
402
|
+
* [📘](https://github.com/nodef/extra-math/wiki/quadriaticMean)
|
|
403
|
+
* @param xs a list of numbers
|
|
404
|
+
* @returns √(Σxᵢ²)/n | n = size(xs)
|
|
405
|
+
* */
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
declare function quadriaticMean(...xs: number[]): number;
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Find the cubic mean of numbers.
|
|
412
|
+
* [📘](https://github.com/nodef/extra-number/wiki/cubicMean)
|
|
413
|
+
* [📘](https://github.com/nodef/extra-math/wiki/cubicMean)
|
|
414
|
+
* @param xs a list of numbers
|
|
415
|
+
* @returns ³√(Σxᵢ³)/n | n = size(xs)
|
|
416
|
+
* */
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
declare function cubicMean(...xs: number[]): number;
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Calculate the magnitude (length) of a vector.
|
|
423
|
+
* [📘](https://github.com/nodef/extra-math/wiki/magnitude)
|
|
424
|
+
* @param xs vector ([x, y, z, ...])
|
|
425
|
+
* @returns √(x² + y² + z²)
|
|
426
|
+
* */
|
|
427
|
+
|
|
428
|
+
declare function magnitude(xs: number[]): number;
|
|
429
|
+
/**
|
|
430
|
+
* Calculate the distance between two points.
|
|
431
|
+
* [📘](https://github.com/nodef/extra-math/wiki/distance)
|
|
432
|
+
* @param xs first point ([x, y, z, ...])
|
|
433
|
+
* @param ys second point ([x, y, z, ...])
|
|
434
|
+
* @returns √(Δx² + Δy² + Δz²)
|
|
435
|
+
* */
|
|
436
|
+
|
|
437
|
+
declare function distance(xs: number[], ys: number[]): number;
|
|
239
438
|
|
|
240
|
-
export { binomial, constrain as clamp, constrain, degrees, distance,
|
|
439
|
+
export { aliquotSum, arithmeticMean, binomial, ceil, ceilDiv, constrain as clamp, constrain, cubicMean, degrees, distance, factorial, floor, floorDiv, gcd, geometricMean, harmonicMean, gcd as hcf, isPow, isPrime, lcm, lerp, log, magnitude, remap as map, median, mod, modes, modp, multinomial, nextPow, normalize as norm, normalize, prevPow, product, properDivisors, quadriaticMean, radians, range, rem, remap, root, round, roundDiv, sum, variance };
|
package/index.js
CHANGED
|
@@ -2,6 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
function floor(x, pre = 1) {
|
|
6
|
+
return Math.floor(x / pre) * pre;
|
|
7
|
+
}
|
|
8
|
+
function ceil(x, pre = 1) {
|
|
9
|
+
return Math.ceil(x / pre) * pre;
|
|
10
|
+
}
|
|
11
|
+
function round(x, pre = 1) {
|
|
12
|
+
return Math.round(x / pre) * pre;
|
|
13
|
+
}
|
|
14
|
+
function floorDiv(x, y) {
|
|
15
|
+
return Math.floor(x / y);
|
|
16
|
+
}
|
|
17
|
+
function ceilDiv(x, y) {
|
|
18
|
+
return Math.ceil(x / y);
|
|
19
|
+
}
|
|
20
|
+
function roundDiv(x, y) {
|
|
21
|
+
return Math.round(x / y);
|
|
22
|
+
}
|
|
5
23
|
function rem(x, y) {
|
|
6
24
|
return x % y;
|
|
7
25
|
}
|
|
@@ -11,6 +29,67 @@ function mod(x, y) {
|
|
|
11
29
|
function modp(x, y) {
|
|
12
30
|
return x - Math.abs(y) * Math.floor(x / Math.abs(y));
|
|
13
31
|
}
|
|
32
|
+
function constrain(x, min, max) {
|
|
33
|
+
return Math.min(Math.max(x, min), max);
|
|
34
|
+
}
|
|
35
|
+
function normalize(x, r, R) {
|
|
36
|
+
return (x - r) / (R - r);
|
|
37
|
+
}
|
|
38
|
+
function remap(x, r, R, t, T) {
|
|
39
|
+
return t + ((x - r) / (R - r)) * (T - t);
|
|
40
|
+
}
|
|
41
|
+
function lerp(x, y, t) {
|
|
42
|
+
return x + t * (y - x);
|
|
43
|
+
}
|
|
44
|
+
function isPow(x, n) {
|
|
45
|
+
if (n === 0)
|
|
46
|
+
return x === 0;
|
|
47
|
+
var p = log(Math.abs(x), Math.abs(n));
|
|
48
|
+
if (p !== Math.floor(p))
|
|
49
|
+
return false;
|
|
50
|
+
return x < 0 ? n < 0 && (p & 1) === 1 : n > 0 || (p & 1) === 0;
|
|
51
|
+
}
|
|
52
|
+
function prevPow(x, n) {
|
|
53
|
+
var p = log(Math.abs(x), Math.abs(n));
|
|
54
|
+
return Math.pow(n, Math.ceil(p - 1));
|
|
55
|
+
}
|
|
56
|
+
function nextPow(x, n) {
|
|
57
|
+
var p = log(Math.abs(x), Math.abs(n));
|
|
58
|
+
return Math.pow(n, Math.floor(p + 1));
|
|
59
|
+
}
|
|
60
|
+
function root(x, n) {
|
|
61
|
+
if ((n & 1) === 0)
|
|
62
|
+
return Math.pow(x, 1 / n);
|
|
63
|
+
return Math.sign(x) * Math.pow(Math.abs(x), 1 / n);
|
|
64
|
+
}
|
|
65
|
+
function log(x, b = Math.E) {
|
|
66
|
+
return Math.log(x) / Math.log(b);
|
|
67
|
+
}
|
|
68
|
+
function properDivisors(x) {
|
|
69
|
+
var x = Math.abs(x), a = [];
|
|
70
|
+
for (var i = 1; i < x; i++)
|
|
71
|
+
if (x % i === 0)
|
|
72
|
+
a.push(i);
|
|
73
|
+
return a;
|
|
74
|
+
}
|
|
75
|
+
function aliquotSum(x) {
|
|
76
|
+
var x = Math.abs(x), a = 0;
|
|
77
|
+
for (var i = 0; i < x; i++)
|
|
78
|
+
if (x % i === 0)
|
|
79
|
+
a += i;
|
|
80
|
+
return a;
|
|
81
|
+
}
|
|
82
|
+
function isPrime(x) {
|
|
83
|
+
var x = Math.abs(x);
|
|
84
|
+
if (x <= 3)
|
|
85
|
+
return x > 1;
|
|
86
|
+
if (x % 2 === 0 || x % 3 === 0)
|
|
87
|
+
return false;
|
|
88
|
+
for (var i = 6, I = Math.sqrt(x) + 1; i <= I; i += 6)
|
|
89
|
+
if (x % (i - 1) === 0 || x % (i + 1) === 0)
|
|
90
|
+
return false;
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
14
93
|
function gcdPair(x, y) {
|
|
15
94
|
while (y !== 0) {
|
|
16
95
|
var t = y;
|
|
@@ -58,80 +137,12 @@ function multinomial(...ks) {
|
|
|
58
137
|
}
|
|
59
138
|
return a;
|
|
60
139
|
}
|
|
61
|
-
function constrain(x, min, max) {
|
|
62
|
-
return Math.min(Math.max(x, min), max);
|
|
63
|
-
}
|
|
64
|
-
function normalize(x, r, R) {
|
|
65
|
-
return (x - r) / (R - r);
|
|
66
|
-
}
|
|
67
|
-
function remap(x, r, R, t, T) {
|
|
68
|
-
return t + ((x - r) / (R - r)) * (T - t);
|
|
69
|
-
}
|
|
70
|
-
function lerp(x, y, t) {
|
|
71
|
-
return x + t * (y - x);
|
|
72
|
-
}
|
|
73
|
-
function root(x, n) {
|
|
74
|
-
if ((n & 1) === 0)
|
|
75
|
-
return Math.pow(x, 1 / n);
|
|
76
|
-
return Math.sign(x) * Math.pow(Math.abs(x), 1 / n);
|
|
77
|
-
}
|
|
78
|
-
function log(x, b = Math.E) {
|
|
79
|
-
return Math.log(x) / Math.log(b);
|
|
80
|
-
}
|
|
81
|
-
const ERF_A1 = 0.254829592;
|
|
82
|
-
const ERF_A2 = -0.284496736;
|
|
83
|
-
const ERF_A3 = 1.421413741;
|
|
84
|
-
const ERF_A4 = -1.453152027;
|
|
85
|
-
const ERF_A5 = 1.061405429;
|
|
86
|
-
const ERF_P = 0.3275911;
|
|
87
|
-
function erf(x) {
|
|
88
|
-
var sgn = x < 0 ? -1 : 1;
|
|
89
|
-
var x = Math.abs(x);
|
|
90
|
-
var t = 1 / (1 + ERF_P * x);
|
|
91
|
-
var y = 1 - (((((ERF_A5 * t + ERF_A4) * t) + ERF_A3) * t + ERF_A2) * t + ERF_A1) * t * Math.exp(-x * x);
|
|
92
|
-
return sgn * y;
|
|
93
|
-
}
|
|
94
|
-
const ERFC_TABLE = [
|
|
95
|
-
-2.8e-17, 1.21e-16, -9.4e-17, -1.523e-15, 7.106e-15,
|
|
96
|
-
3.81e-16, -1.12708e-13, 3.13092e-13, 8.94487e-13,
|
|
97
|
-
-6.886027e-12, 2.394038e-12, 9.6467911e-11,
|
|
98
|
-
-2.27365122e-10, -9.91364156e-10, 5.059343495e-9,
|
|
99
|
-
6.529054439e-9, -8.5238095915e-8, 1.5626441722e-8,
|
|
100
|
-
1.303655835580e-6, -1.624290004647e-6,
|
|
101
|
-
-2.0278578112534e-5, 4.2523324806907e-5,
|
|
102
|
-
3.66839497852761e-4, -9.46595344482036e-4,
|
|
103
|
-
-9.561514786808631e-3, 1.9476473204185836e-2,
|
|
104
|
-
6.4196979235649026e-1, -1.3026537197817094
|
|
105
|
-
];
|
|
106
|
-
function erfc(x) {
|
|
107
|
-
if (x < 0)
|
|
108
|
-
return 2 - erfc(-x);
|
|
109
|
-
var c = 0, d = 0, y = 2 / (2 + x), z = 4 * y - 2;
|
|
110
|
-
for (var i = 0, I = ERFC_TABLE.length - 1; i < I; i++) {
|
|
111
|
-
var t = d;
|
|
112
|
-
d = z * d - c + ERFC_TABLE[i];
|
|
113
|
-
c = t;
|
|
114
|
-
}
|
|
115
|
-
return y * Math.exp(-x * x + 0.5 * (ERFC_TABLE[I] + z * d) - c);
|
|
116
|
-
}
|
|
117
140
|
function degrees(x) {
|
|
118
141
|
return x * (180 / Math.PI);
|
|
119
142
|
}
|
|
120
143
|
function radians(x) {
|
|
121
144
|
return x * (Math.PI / 180);
|
|
122
145
|
}
|
|
123
|
-
function magnitude(xs) {
|
|
124
|
-
var a = 0;
|
|
125
|
-
for (var i = 0, I = xs.length; i < I; i++)
|
|
126
|
-
a += xs[i] ** 2;
|
|
127
|
-
return Math.sqrt(a);
|
|
128
|
-
}
|
|
129
|
-
function distance(xs, ys) {
|
|
130
|
-
var a = 0;
|
|
131
|
-
for (var i = 0, I = xs.length; i < I; i++)
|
|
132
|
-
a += (xs[i] - ys[i]) ** 2;
|
|
133
|
-
return Math.sqrt(a);
|
|
134
|
-
}
|
|
135
146
|
function sum(...xs) {
|
|
136
147
|
var a = 0;
|
|
137
148
|
for (var x of xs)
|
|
@@ -144,11 +155,6 @@ function product(...xs) {
|
|
|
144
155
|
a *= x;
|
|
145
156
|
return a;
|
|
146
157
|
}
|
|
147
|
-
function mean(...xs) {
|
|
148
|
-
if (xs.length === 0)
|
|
149
|
-
return 0;
|
|
150
|
-
return sum(...xs) / xs.length;
|
|
151
|
-
}
|
|
152
158
|
function median(...xs) {
|
|
153
159
|
if (xs.length === 0)
|
|
154
160
|
return 0;
|
|
@@ -184,45 +190,100 @@ function modes(...xs) {
|
|
|
184
190
|
return getRepeats(xs, r);
|
|
185
191
|
}
|
|
186
192
|
function range(...xs) {
|
|
187
|
-
return Math.
|
|
193
|
+
return [Math.min(...xs), Math.max(...xs)];
|
|
188
194
|
}
|
|
189
195
|
function variance(...xs) {
|
|
190
196
|
if (xs.length === 0)
|
|
191
197
|
return 0;
|
|
192
|
-
var m =
|
|
198
|
+
var m = arithmeticMean(...xs), a = 0;
|
|
193
199
|
for (var x of xs)
|
|
194
200
|
a += (x - m) ** 2;
|
|
195
201
|
return a / xs.length;
|
|
196
202
|
}
|
|
203
|
+
function arithmeticMean(...xs) {
|
|
204
|
+
if (xs.length === 0)
|
|
205
|
+
return 0;
|
|
206
|
+
return sum(...xs) / xs.length;
|
|
207
|
+
}
|
|
208
|
+
function geometricMean(...xs) {
|
|
209
|
+
var n = xs.length;
|
|
210
|
+
return root(product(...xs), n);
|
|
211
|
+
}
|
|
212
|
+
function harmonicMean(...xs) {
|
|
213
|
+
var n = xs.length;
|
|
214
|
+
var p = product(...xs), q = 0;
|
|
215
|
+
for (var x of xs)
|
|
216
|
+
q += p / x;
|
|
217
|
+
return n * p / q;
|
|
218
|
+
}
|
|
219
|
+
function quadriaticMean(...xs) {
|
|
220
|
+
var n = xs.length, a = 0;
|
|
221
|
+
for (var x of xs)
|
|
222
|
+
a += x * x;
|
|
223
|
+
return Math.sqrt(a / n);
|
|
224
|
+
}
|
|
225
|
+
function cubicMean(...xs) {
|
|
226
|
+
var n = xs.length, a = 0;
|
|
227
|
+
for (var x of xs)
|
|
228
|
+
a += x ** 3;
|
|
229
|
+
return Math.cbrt(a / n);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function magnitude(xs) {
|
|
233
|
+
var a = 0;
|
|
234
|
+
for (var i = 0, I = xs.length; i < I; i++)
|
|
235
|
+
a += xs[i] ** 2;
|
|
236
|
+
return Math.sqrt(a);
|
|
237
|
+
}
|
|
238
|
+
function distance(xs, ys) {
|
|
239
|
+
var a = 0;
|
|
240
|
+
for (var i = 0, I = xs.length; i < I; i++)
|
|
241
|
+
a += (xs[i] - ys[i]) ** 2;
|
|
242
|
+
return Math.sqrt(a);
|
|
243
|
+
}
|
|
197
244
|
|
|
245
|
+
exports.aliquotSum = aliquotSum;
|
|
246
|
+
exports.arithmeticMean = arithmeticMean;
|
|
198
247
|
exports.binomial = binomial;
|
|
248
|
+
exports.ceil = ceil;
|
|
249
|
+
exports.ceilDiv = ceilDiv;
|
|
199
250
|
exports.clamp = constrain;
|
|
200
251
|
exports.constrain = constrain;
|
|
252
|
+
exports.cubicMean = cubicMean;
|
|
201
253
|
exports.degrees = degrees;
|
|
202
254
|
exports.distance = distance;
|
|
203
|
-
exports.erf = erf;
|
|
204
|
-
exports.erfc = erfc;
|
|
205
255
|
exports.factorial = factorial;
|
|
256
|
+
exports.floor = floor;
|
|
257
|
+
exports.floorDiv = floorDiv;
|
|
206
258
|
exports.gcd = gcd;
|
|
259
|
+
exports.geometricMean = geometricMean;
|
|
260
|
+
exports.harmonicMean = harmonicMean;
|
|
207
261
|
exports.hcf = gcd;
|
|
262
|
+
exports.isPow = isPow;
|
|
263
|
+
exports.isPrime = isPrime;
|
|
208
264
|
exports.lcm = lcm;
|
|
209
265
|
exports.lerp = lerp;
|
|
210
266
|
exports.log = log;
|
|
211
267
|
exports.magnitude = magnitude;
|
|
212
268
|
exports.map = remap;
|
|
213
|
-
exports.mean = mean;
|
|
214
269
|
exports.median = median;
|
|
215
270
|
exports.mod = mod;
|
|
216
271
|
exports.modes = modes;
|
|
217
272
|
exports.modp = modp;
|
|
218
273
|
exports.multinomial = multinomial;
|
|
274
|
+
exports.nextPow = nextPow;
|
|
219
275
|
exports.norm = normalize;
|
|
220
276
|
exports.normalize = normalize;
|
|
277
|
+
exports.prevPow = prevPow;
|
|
221
278
|
exports.product = product;
|
|
279
|
+
exports.properDivisors = properDivisors;
|
|
280
|
+
exports.quadriaticMean = quadriaticMean;
|
|
222
281
|
exports.radians = radians;
|
|
223
282
|
exports.range = range;
|
|
224
283
|
exports.rem = rem;
|
|
225
284
|
exports.remap = remap;
|
|
226
285
|
exports.root = root;
|
|
286
|
+
exports.round = round;
|
|
287
|
+
exports.roundDiv = roundDiv;
|
|
227
288
|
exports.sum = sum;
|
|
228
289
|
exports.variance = variance;
|
package/index.mjs
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
function floor(x, pre = 1) {
|
|
2
|
+
return Math.floor(x / pre) * pre;
|
|
3
|
+
}
|
|
4
|
+
function ceil(x, pre = 1) {
|
|
5
|
+
return Math.ceil(x / pre) * pre;
|
|
6
|
+
}
|
|
7
|
+
function round(x, pre = 1) {
|
|
8
|
+
return Math.round(x / pre) * pre;
|
|
9
|
+
}
|
|
10
|
+
function floorDiv(x, y) {
|
|
11
|
+
return Math.floor(x / y);
|
|
12
|
+
}
|
|
13
|
+
function ceilDiv(x, y) {
|
|
14
|
+
return Math.ceil(x / y);
|
|
15
|
+
}
|
|
16
|
+
function roundDiv(x, y) {
|
|
17
|
+
return Math.round(x / y);
|
|
18
|
+
}
|
|
1
19
|
function rem(x, y) {
|
|
2
20
|
return x % y;
|
|
3
21
|
}
|
|
@@ -7,6 +25,67 @@ function mod(x, y) {
|
|
|
7
25
|
function modp(x, y) {
|
|
8
26
|
return x - Math.abs(y) * Math.floor(x / Math.abs(y));
|
|
9
27
|
}
|
|
28
|
+
function constrain(x, min, max) {
|
|
29
|
+
return Math.min(Math.max(x, min), max);
|
|
30
|
+
}
|
|
31
|
+
function normalize(x, r, R) {
|
|
32
|
+
return (x - r) / (R - r);
|
|
33
|
+
}
|
|
34
|
+
function remap(x, r, R, t, T) {
|
|
35
|
+
return t + ((x - r) / (R - r)) * (T - t);
|
|
36
|
+
}
|
|
37
|
+
function lerp(x, y, t) {
|
|
38
|
+
return x + t * (y - x);
|
|
39
|
+
}
|
|
40
|
+
function isPow(x, n) {
|
|
41
|
+
if (n === 0)
|
|
42
|
+
return x === 0;
|
|
43
|
+
var p = log(Math.abs(x), Math.abs(n));
|
|
44
|
+
if (p !== Math.floor(p))
|
|
45
|
+
return false;
|
|
46
|
+
return x < 0 ? n < 0 && (p & 1) === 1 : n > 0 || (p & 1) === 0;
|
|
47
|
+
}
|
|
48
|
+
function prevPow(x, n) {
|
|
49
|
+
var p = log(Math.abs(x), Math.abs(n));
|
|
50
|
+
return Math.pow(n, Math.ceil(p - 1));
|
|
51
|
+
}
|
|
52
|
+
function nextPow(x, n) {
|
|
53
|
+
var p = log(Math.abs(x), Math.abs(n));
|
|
54
|
+
return Math.pow(n, Math.floor(p + 1));
|
|
55
|
+
}
|
|
56
|
+
function root(x, n) {
|
|
57
|
+
if ((n & 1) === 0)
|
|
58
|
+
return Math.pow(x, 1 / n);
|
|
59
|
+
return Math.sign(x) * Math.pow(Math.abs(x), 1 / n);
|
|
60
|
+
}
|
|
61
|
+
function log(x, b = Math.E) {
|
|
62
|
+
return Math.log(x) / Math.log(b);
|
|
63
|
+
}
|
|
64
|
+
function properDivisors(x) {
|
|
65
|
+
var x = Math.abs(x), a = [];
|
|
66
|
+
for (var i = 1; i < x; i++)
|
|
67
|
+
if (x % i === 0)
|
|
68
|
+
a.push(i);
|
|
69
|
+
return a;
|
|
70
|
+
}
|
|
71
|
+
function aliquotSum(x) {
|
|
72
|
+
var x = Math.abs(x), a = 0;
|
|
73
|
+
for (var i = 0; i < x; i++)
|
|
74
|
+
if (x % i === 0)
|
|
75
|
+
a += i;
|
|
76
|
+
return a;
|
|
77
|
+
}
|
|
78
|
+
function isPrime(x) {
|
|
79
|
+
var x = Math.abs(x);
|
|
80
|
+
if (x <= 3)
|
|
81
|
+
return x > 1;
|
|
82
|
+
if (x % 2 === 0 || x % 3 === 0)
|
|
83
|
+
return false;
|
|
84
|
+
for (var i = 6, I = Math.sqrt(x) + 1; i <= I; i += 6)
|
|
85
|
+
if (x % (i - 1) === 0 || x % (i + 1) === 0)
|
|
86
|
+
return false;
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
10
89
|
function gcdPair(x, y) {
|
|
11
90
|
while (y !== 0) {
|
|
12
91
|
var t = y;
|
|
@@ -54,80 +133,12 @@ function multinomial(...ks) {
|
|
|
54
133
|
}
|
|
55
134
|
return a;
|
|
56
135
|
}
|
|
57
|
-
function constrain(x, min, max) {
|
|
58
|
-
return Math.min(Math.max(x, min), max);
|
|
59
|
-
}
|
|
60
|
-
function normalize(x, r, R) {
|
|
61
|
-
return (x - r) / (R - r);
|
|
62
|
-
}
|
|
63
|
-
function remap(x, r, R, t, T) {
|
|
64
|
-
return t + ((x - r) / (R - r)) * (T - t);
|
|
65
|
-
}
|
|
66
|
-
function lerp(x, y, t) {
|
|
67
|
-
return x + t * (y - x);
|
|
68
|
-
}
|
|
69
|
-
function root(x, n) {
|
|
70
|
-
if ((n & 1) === 0)
|
|
71
|
-
return Math.pow(x, 1 / n);
|
|
72
|
-
return Math.sign(x) * Math.pow(Math.abs(x), 1 / n);
|
|
73
|
-
}
|
|
74
|
-
function log(x, b = Math.E) {
|
|
75
|
-
return Math.log(x) / Math.log(b);
|
|
76
|
-
}
|
|
77
|
-
const ERF_A1 = 0.254829592;
|
|
78
|
-
const ERF_A2 = -0.284496736;
|
|
79
|
-
const ERF_A3 = 1.421413741;
|
|
80
|
-
const ERF_A4 = -1.453152027;
|
|
81
|
-
const ERF_A5 = 1.061405429;
|
|
82
|
-
const ERF_P = 0.3275911;
|
|
83
|
-
function erf(x) {
|
|
84
|
-
var sgn = x < 0 ? -1 : 1;
|
|
85
|
-
var x = Math.abs(x);
|
|
86
|
-
var t = 1 / (1 + ERF_P * x);
|
|
87
|
-
var y = 1 - (((((ERF_A5 * t + ERF_A4) * t) + ERF_A3) * t + ERF_A2) * t + ERF_A1) * t * Math.exp(-x * x);
|
|
88
|
-
return sgn * y;
|
|
89
|
-
}
|
|
90
|
-
const ERFC_TABLE = [
|
|
91
|
-
-2.8e-17, 1.21e-16, -9.4e-17, -1.523e-15, 7.106e-15,
|
|
92
|
-
3.81e-16, -1.12708e-13, 3.13092e-13, 8.94487e-13,
|
|
93
|
-
-6.886027e-12, 2.394038e-12, 9.6467911e-11,
|
|
94
|
-
-2.27365122e-10, -9.91364156e-10, 5.059343495e-9,
|
|
95
|
-
6.529054439e-9, -8.5238095915e-8, 1.5626441722e-8,
|
|
96
|
-
1.303655835580e-6, -1.624290004647e-6,
|
|
97
|
-
-2.0278578112534e-5, 4.2523324806907e-5,
|
|
98
|
-
3.66839497852761e-4, -9.46595344482036e-4,
|
|
99
|
-
-9.561514786808631e-3, 1.9476473204185836e-2,
|
|
100
|
-
6.4196979235649026e-1, -1.3026537197817094
|
|
101
|
-
];
|
|
102
|
-
function erfc(x) {
|
|
103
|
-
if (x < 0)
|
|
104
|
-
return 2 - erfc(-x);
|
|
105
|
-
var c = 0, d = 0, y = 2 / (2 + x), z = 4 * y - 2;
|
|
106
|
-
for (var i = 0, I = ERFC_TABLE.length - 1; i < I; i++) {
|
|
107
|
-
var t = d;
|
|
108
|
-
d = z * d - c + ERFC_TABLE[i];
|
|
109
|
-
c = t;
|
|
110
|
-
}
|
|
111
|
-
return y * Math.exp(-x * x + 0.5 * (ERFC_TABLE[I] + z * d) - c);
|
|
112
|
-
}
|
|
113
136
|
function degrees(x) {
|
|
114
137
|
return x * (180 / Math.PI);
|
|
115
138
|
}
|
|
116
139
|
function radians(x) {
|
|
117
140
|
return x * (Math.PI / 180);
|
|
118
141
|
}
|
|
119
|
-
function magnitude(xs) {
|
|
120
|
-
var a = 0;
|
|
121
|
-
for (var i = 0, I = xs.length; i < I; i++)
|
|
122
|
-
a += xs[i] ** 2;
|
|
123
|
-
return Math.sqrt(a);
|
|
124
|
-
}
|
|
125
|
-
function distance(xs, ys) {
|
|
126
|
-
var a = 0;
|
|
127
|
-
for (var i = 0, I = xs.length; i < I; i++)
|
|
128
|
-
a += (xs[i] - ys[i]) ** 2;
|
|
129
|
-
return Math.sqrt(a);
|
|
130
|
-
}
|
|
131
142
|
function sum(...xs) {
|
|
132
143
|
var a = 0;
|
|
133
144
|
for (var x of xs)
|
|
@@ -140,11 +151,6 @@ function product(...xs) {
|
|
|
140
151
|
a *= x;
|
|
141
152
|
return a;
|
|
142
153
|
}
|
|
143
|
-
function mean(...xs) {
|
|
144
|
-
if (xs.length === 0)
|
|
145
|
-
return 0;
|
|
146
|
-
return sum(...xs) / xs.length;
|
|
147
|
-
}
|
|
148
154
|
function median(...xs) {
|
|
149
155
|
if (xs.length === 0)
|
|
150
156
|
return 0;
|
|
@@ -180,15 +186,56 @@ function modes(...xs) {
|
|
|
180
186
|
return getRepeats(xs, r);
|
|
181
187
|
}
|
|
182
188
|
function range(...xs) {
|
|
183
|
-
return Math.
|
|
189
|
+
return [Math.min(...xs), Math.max(...xs)];
|
|
184
190
|
}
|
|
185
191
|
function variance(...xs) {
|
|
186
192
|
if (xs.length === 0)
|
|
187
193
|
return 0;
|
|
188
|
-
var m =
|
|
194
|
+
var m = arithmeticMean(...xs), a = 0;
|
|
189
195
|
for (var x of xs)
|
|
190
196
|
a += (x - m) ** 2;
|
|
191
197
|
return a / xs.length;
|
|
192
198
|
}
|
|
199
|
+
function arithmeticMean(...xs) {
|
|
200
|
+
if (xs.length === 0)
|
|
201
|
+
return 0;
|
|
202
|
+
return sum(...xs) / xs.length;
|
|
203
|
+
}
|
|
204
|
+
function geometricMean(...xs) {
|
|
205
|
+
var n = xs.length;
|
|
206
|
+
return root(product(...xs), n);
|
|
207
|
+
}
|
|
208
|
+
function harmonicMean(...xs) {
|
|
209
|
+
var n = xs.length;
|
|
210
|
+
var p = product(...xs), q = 0;
|
|
211
|
+
for (var x of xs)
|
|
212
|
+
q += p / x;
|
|
213
|
+
return n * p / q;
|
|
214
|
+
}
|
|
215
|
+
function quadriaticMean(...xs) {
|
|
216
|
+
var n = xs.length, a = 0;
|
|
217
|
+
for (var x of xs)
|
|
218
|
+
a += x * x;
|
|
219
|
+
return Math.sqrt(a / n);
|
|
220
|
+
}
|
|
221
|
+
function cubicMean(...xs) {
|
|
222
|
+
var n = xs.length, a = 0;
|
|
223
|
+
for (var x of xs)
|
|
224
|
+
a += x ** 3;
|
|
225
|
+
return Math.cbrt(a / n);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function magnitude(xs) {
|
|
229
|
+
var a = 0;
|
|
230
|
+
for (var i = 0, I = xs.length; i < I; i++)
|
|
231
|
+
a += xs[i] ** 2;
|
|
232
|
+
return Math.sqrt(a);
|
|
233
|
+
}
|
|
234
|
+
function distance(xs, ys) {
|
|
235
|
+
var a = 0;
|
|
236
|
+
for (var i = 0, I = xs.length; i < I; i++)
|
|
237
|
+
a += (xs[i] - ys[i]) ** 2;
|
|
238
|
+
return Math.sqrt(a);
|
|
239
|
+
}
|
|
193
240
|
|
|
194
|
-
export { binomial, constrain as clamp, constrain, degrees, distance,
|
|
241
|
+
export { aliquotSum, arithmeticMean, binomial, ceil, ceilDiv, constrain as clamp, constrain, cubicMean, degrees, distance, factorial, floor, floorDiv, gcd, geometricMean, harmonicMean, gcd as hcf, isPow, isPrime, lcm, lerp, log, magnitude, remap as map, median, mod, modes, modp, multinomial, nextPow, normalize as norm, normalize, prevPow, product, properDivisors, quadriaticMean, radians, range, rem, remap, root, round, roundDiv, sum, variance };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "extra-math",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.32",
|
|
4
4
|
"description": "A collection of common mathematical functions.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
@@ -22,31 +22,45 @@
|
|
|
22
22
|
"hcf",
|
|
23
23
|
"map",
|
|
24
24
|
"norm",
|
|
25
|
+
"aliquotSum",
|
|
26
|
+
"arithmeticMean",
|
|
25
27
|
"binomial",
|
|
28
|
+
"ceil",
|
|
29
|
+
"ceilDiv",
|
|
26
30
|
"constrain",
|
|
31
|
+
"cubicMean",
|
|
27
32
|
"degrees",
|
|
28
33
|
"distance",
|
|
29
|
-
"erf",
|
|
30
|
-
"erfc",
|
|
31
34
|
"factorial",
|
|
35
|
+
"floor",
|
|
36
|
+
"floorDiv",
|
|
32
37
|
"gcd",
|
|
38
|
+
"geometricMean",
|
|
39
|
+
"harmonicMean",
|
|
40
|
+
"isPow",
|
|
41
|
+
"isPrime",
|
|
33
42
|
"lcm",
|
|
34
43
|
"lerp",
|
|
35
44
|
"log",
|
|
36
45
|
"magnitude",
|
|
37
|
-
"mean",
|
|
38
46
|
"median",
|
|
39
47
|
"mod",
|
|
40
48
|
"modes",
|
|
41
49
|
"modp",
|
|
42
50
|
"multinomial",
|
|
51
|
+
"nextPow",
|
|
43
52
|
"normalize",
|
|
53
|
+
"prevPow",
|
|
44
54
|
"product",
|
|
55
|
+
"properDivisors",
|
|
56
|
+
"quadriaticMean",
|
|
45
57
|
"radians",
|
|
46
58
|
"range",
|
|
47
59
|
"rem",
|
|
48
60
|
"remap",
|
|
49
61
|
"root",
|
|
62
|
+
"round",
|
|
63
|
+
"roundDiv",
|
|
50
64
|
"sum",
|
|
51
65
|
"variance"
|
|
52
66
|
],
|
|
@@ -61,10 +75,11 @@
|
|
|
61
75
|
},
|
|
62
76
|
"homepage": "https://github.com/nodef/extra-math#readme",
|
|
63
77
|
"devDependencies": {
|
|
64
|
-
"@rollup/plugin-commonjs": "^22.0.
|
|
78
|
+
"@rollup/plugin-commonjs": "^22.0.2",
|
|
65
79
|
"@rollup/plugin-node-resolve": "^13.3.0",
|
|
66
80
|
"@types/jest": "^28.1.6",
|
|
67
|
-
"extra-build": "^2.2.
|
|
81
|
+
"extra-build": "^2.2.27",
|
|
82
|
+
"extra-number": "^2.2.33",
|
|
68
83
|
"jest": "^28.1.3",
|
|
69
84
|
"rollup-plugin-cleanup": "^3.2.1",
|
|
70
85
|
"rollup-plugin-dts": "^4.2.2",
|