extra-math 1.3.23 → 1.3.26

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 CHANGED
@@ -75,23 +75,21 @@ math.lcm(2, 3, 4);
75
75
  | [factorial] | Find the factorial of a number. |
76
76
  | [binomial] | Find the number of ways to choose k elements from a set of n elements. |
77
77
  | [multinomial] | Find the number of ways to put n objects in m bins (n=sum(kᵢ)). |
78
- | | |
78
+ | | |
79
79
  | [constrain] | Constrain a number within a minimum and a maximum value. |
80
- | [norm] | Normalize a number from its current range into a value between 0 and 1. |
81
- | [map] | Re-map a number from one range to another. |
80
+ | [normalize] | Normalize a number from its current range into a value between 0 and 1. |
81
+ | [remap] | Re-map a number from one range to another. |
82
82
  | [lerp] | Linearly interpolate a number between two numbers. |
83
83
  | [root] | Find the nth root of a number (ⁿ√). |
84
84
  | [log] | Find the logarithm of a number with a given base. |
85
- | [gamma] | Compute the gamma function of a number (Γ). |
86
- | [lgamma] | Compute the natural logarithm of the absolute value of the gamma function of a number (log-Γ). |
87
85
  | [erf] | Find error function value of number (approximation). |
88
86
  | [erfc] | Find the complementary error function value of number (approximation). |
89
- | | |
87
+ | | |
90
88
  | [degrees] | Convert radians to degrees. |
91
89
  | [radians] | Convert degrees to radians. |
92
90
  | [magnitude] | Calculate the magnitude (length) of a vector. |
93
91
  | [distance] | Calculate the distance between two points. |
94
- | | |
92
+ | | |
95
93
  | [sum] | Find the sum of numbers (Σ). |
96
94
  | [product] | Find the product of numbers (∏). |
97
95
  | [mean] | Find the average of numbers. |
@@ -137,11 +135,9 @@ math.lcm(2, 3, 4);
137
135
  [constrain]: https://nodef.github.io/extra-math/modules.html#constrain
138
136
  [root]: https://nodef.github.io/extra-math/modules.html#root
139
137
  [log]: https://nodef.github.io/extra-math/modules.html#log
140
- [norm]: https://nodef.github.io/extra-math/modules.html#norm
141
- [map]: https://nodef.github.io/extra-math/modules.html#map
138
+ [normalize]: https://nodef.github.io/extra-math/modules.html#normalize
139
+ [remap]: https://nodef.github.io/extra-math/modules.html#remap
142
140
  [lerp]: https://nodef.github.io/extra-math/modules.html#lerp
143
- [gamma]: https://nodef.github.io/extra-math/modules.html#gamma
144
- [lgamma]: https://nodef.github.io/extra-math/modules.html#lgamma
145
141
  [erf]: https://nodef.github.io/extra-math/modules.html#erf
146
142
  [erfc]: https://nodef.github.io/extra-math/modules.html#erfc
147
143
  [degrees]: https://nodef.github.io/extra-math/modules.html#degrees
package/index.d.ts CHANGED
@@ -81,17 +81,18 @@ declare function constrain(x: number, min: number, max: number): number;
81
81
 
82
82
  /**
83
83
  * Normalize a number from its current range into a value between 0 and 1.
84
- * [📘](https://github.com/nodef/extra-math/wiki/norm)
84
+ * [📘](https://github.com/nodef/extra-math/wiki/normalize)
85
85
  * @param x a number
86
86
  * @param r lower bound of current range
87
87
  * @param R upper bound of current range
88
88
  * @returns ∈ [0, 1]
89
89
  * */
90
90
 
91
- declare function norm(x: number, r: number, R: number): number;
91
+ declare function normalize(x: number, r: number, R: number): number;
92
+
92
93
  /**
93
94
  * Re-map a number from one range to another.
94
- * [📘](https://github.com/nodef/extra-math/wiki/map)
95
+ * [📘](https://github.com/nodef/extra-math/wiki/remap)
95
96
  * @param x a number
96
97
  * @param r lower bound of current range
97
98
  * @param R upper bound of current range
@@ -100,7 +101,8 @@ declare function norm(x: number, r: number, R: number): number;
100
101
  * @returns ∈ [ymin, ymax]
101
102
  * */
102
103
 
103
- declare function map(x: number, r: number, R: number, t: number, T: number): number;
104
+ declare function remap(x: number, r: number, R: number, t: number, T: number): number;
105
+
104
106
  /**
105
107
  * Linearly interpolate a number between two numbers.
106
108
  * [📘](https://github.com/nodef/extra-math/wiki/lerp)
@@ -129,22 +131,6 @@ declare function root(x: number, n: number): number;
129
131
  * */
130
132
 
131
133
  declare function log(x: number, b?: number): number;
132
- /**
133
- * Compute the gamma function of a number (Γ).
134
- * [📘](https://github.com/nodef/extra-math/wiki/gamma)
135
- * @param x a number
136
- * @returns Γ(x); for +ve integer Γ(x) = x!
137
- * */
138
-
139
- declare function gamma(x: number): number;
140
- /**
141
- * Compute the natural logarithm of the absolute value of the gamma function of a number (log-Γ).
142
- * [📘](https://github.com/nodef/extra-math/wiki/lgamma)
143
- * @param x a number
144
- * @returns log(|Γ(x)|)
145
- * */
146
-
147
- declare function lgamma(x: number): number;
148
134
  /**
149
135
  * Find error function value of number (approximation).
150
136
  * [📘](https://github.com/nodef/extra-math/wiki/erf)
@@ -251,4 +237,4 @@ declare function range(...xs: number[]): number;
251
237
 
252
238
  declare function variance(...xs: number[]): number;
253
239
 
254
- export { binomial, constrain as clamp, constrain, degrees, distance, erf, erfc, factorial, gamma, gcd, gcd as hcf, lcm, lerp, lgamma, log, magnitude, map, mean, median, mod, modes, modp, multinomial, norm, product, radians, range, rem, root, sum, variance };
240
+ export { binomial, constrain as clamp, constrain, degrees, distance, erf, erfc, factorial, gcd, gcd as hcf, lcm, lerp, log, magnitude, remap as map, mean, median, mod, modes, modp, multinomial, normalize as norm, normalize, product, radians, range, rem, remap, root, sum, variance };
package/index.js CHANGED
@@ -35,7 +35,7 @@ function factorial(n, k = 0) {
35
35
  if (n < 0)
36
36
  return 0;
37
37
  for (var i = k + 1, a = 1; i <= n; i++)
38
- a *= n;
38
+ a *= i;
39
39
  return a;
40
40
  }
41
41
  function binomial(n, k) {
@@ -61,32 +61,23 @@ function multinomial(...ks) {
61
61
  function constrain(x, min, max) {
62
62
  return Math.min(Math.max(x, min), max);
63
63
  }
64
- function norm(x, r, R) {
64
+ function normalize(x, r, R) {
65
65
  return (x - r) / (R - r);
66
66
  }
67
- function map(x, r, R, t, T) {
67
+ function remap(x, r, R, t, T) {
68
68
  return t + ((x - r) / (R - r)) * (T - t);
69
69
  }
70
70
  function lerp(x, y, t) {
71
71
  return x + t * (y - x);
72
72
  }
73
73
  function root(x, n) {
74
- return Math.pow(x, 1 / 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);
75
77
  }
76
78
  function log(x, b = Math.E) {
77
79
  return Math.log(x) / Math.log(b);
78
80
  }
79
- function gamma(x) {
80
- var x = x - 1;
81
- var e1 = Math.sqrt(2 * Math.PI * x);
82
- var e2 = Math.pow(x / Math.E, x);
83
- var e3 = Math.pow(x * Math.sinh(1 / x), x / 2);
84
- var e4 = Math.exp((7 / 324) * (1 / ((x ** 3) * (35 * x ** 2 + 33))));
85
- return e1 * e2 * e3 * e4;
86
- }
87
- function lgamma(x) {
88
- return Math.log(Math.abs(gamma(x)));
89
- }
90
81
  const ERF_A1 = 0.254829592;
91
82
  const ERF_A2 = -0.284496736;
92
83
  const ERF_A3 = 1.421413741;
@@ -212,26 +203,26 @@ exports.distance = distance;
212
203
  exports.erf = erf;
213
204
  exports.erfc = erfc;
214
205
  exports.factorial = factorial;
215
- exports.gamma = gamma;
216
206
  exports.gcd = gcd;
217
207
  exports.hcf = gcd;
218
208
  exports.lcm = lcm;
219
209
  exports.lerp = lerp;
220
- exports.lgamma = lgamma;
221
210
  exports.log = log;
222
211
  exports.magnitude = magnitude;
223
- exports.map = map;
212
+ exports.map = remap;
224
213
  exports.mean = mean;
225
214
  exports.median = median;
226
215
  exports.mod = mod;
227
216
  exports.modes = modes;
228
217
  exports.modp = modp;
229
218
  exports.multinomial = multinomial;
230
- exports.norm = norm;
219
+ exports.norm = normalize;
220
+ exports.normalize = normalize;
231
221
  exports.product = product;
232
222
  exports.radians = radians;
233
223
  exports.range = range;
234
224
  exports.rem = rem;
225
+ exports.remap = remap;
235
226
  exports.root = root;
236
227
  exports.sum = sum;
237
228
  exports.variance = variance;
package/index.mjs CHANGED
@@ -31,7 +31,7 @@ function factorial(n, k = 0) {
31
31
  if (n < 0)
32
32
  return 0;
33
33
  for (var i = k + 1, a = 1; i <= n; i++)
34
- a *= n;
34
+ a *= i;
35
35
  return a;
36
36
  }
37
37
  function binomial(n, k) {
@@ -57,32 +57,23 @@ function multinomial(...ks) {
57
57
  function constrain(x, min, max) {
58
58
  return Math.min(Math.max(x, min), max);
59
59
  }
60
- function norm(x, r, R) {
60
+ function normalize(x, r, R) {
61
61
  return (x - r) / (R - r);
62
62
  }
63
- function map(x, r, R, t, T) {
63
+ function remap(x, r, R, t, T) {
64
64
  return t + ((x - r) / (R - r)) * (T - t);
65
65
  }
66
66
  function lerp(x, y, t) {
67
67
  return x + t * (y - x);
68
68
  }
69
69
  function root(x, n) {
70
- return Math.pow(x, 1 / 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);
71
73
  }
72
74
  function log(x, b = Math.E) {
73
75
  return Math.log(x) / Math.log(b);
74
76
  }
75
- function gamma(x) {
76
- var x = x - 1;
77
- var e1 = Math.sqrt(2 * Math.PI * x);
78
- var e2 = Math.pow(x / Math.E, x);
79
- var e3 = Math.pow(x * Math.sinh(1 / x), x / 2);
80
- var e4 = Math.exp((7 / 324) * (1 / ((x ** 3) * (35 * x ** 2 + 33))));
81
- return e1 * e2 * e3 * e4;
82
- }
83
- function lgamma(x) {
84
- return Math.log(Math.abs(gamma(x)));
85
- }
86
77
  const ERF_A1 = 0.254829592;
87
78
  const ERF_A2 = -0.284496736;
88
79
  const ERF_A3 = 1.421413741;
@@ -200,4 +191,4 @@ function variance(...xs) {
200
191
  return a / xs.length;
201
192
  }
202
193
 
203
- export { binomial, constrain as clamp, constrain, degrees, distance, erf, erfc, factorial, gamma, gcd, gcd as hcf, lcm, lerp, lgamma, log, magnitude, map, mean, median, mod, modes, modp, multinomial, norm, product, radians, range, rem, root, sum, variance };
194
+ export { binomial, constrain as clamp, constrain, degrees, distance, erf, erfc, factorial, gcd, gcd as hcf, lcm, lerp, log, magnitude, remap as map, mean, median, mod, modes, modp, multinomial, normalize as norm, normalize, product, radians, range, rem, remap, root, sum, variance };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-math",
3
- "version": "1.3.23",
3
+ "version": "1.3.26",
4
4
  "description": "A collection of common mathematical functions.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -20,6 +20,8 @@
20
20
  "math",
21
21
  "clamp",
22
22
  "hcf",
23
+ "map",
24
+ "norm",
23
25
  "binomial",
24
26
  "constrain",
25
27
  "degrees",
@@ -27,25 +29,23 @@
27
29
  "erf",
28
30
  "erfc",
29
31
  "factorial",
30
- "gamma",
31
32
  "gcd",
32
33
  "lcm",
33
34
  "lerp",
34
- "lgamma",
35
35
  "log",
36
36
  "magnitude",
37
- "map",
38
37
  "mean",
39
38
  "median",
40
39
  "mod",
41
40
  "modes",
42
41
  "modp",
43
42
  "multinomial",
44
- "norm",
43
+ "normalize",
45
44
  "product",
46
45
  "radians",
47
46
  "range",
48
47
  "rem",
48
+ "remap",
49
49
  "root",
50
50
  "sum",
51
51
  "variance"