extra-math 1.3.10 → 1.3.13

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
@@ -24,16 +24,16 @@ const math = require('extra-math');
24
24
  // import * as math from "https://unpkg.com/extra-math/index.mjs"; (deno)
25
25
 
26
26
  math.sum(1, 2, 3, 4);
27
- // 10
27
+ // → 10
28
28
 
29
29
  math.median(1, 7, 8);
30
- // 7
30
+ // → 7
31
31
 
32
32
  math.variance(1, 2, 3, 4);
33
- // 1.25
33
+ // → 1.25
34
34
 
35
35
  math.lcm(2, 3, 4);
36
- // 12
36
+ // → 12
37
37
  ```
38
38
 
39
39
  <br>
@@ -44,6 +44,38 @@ math.lcm(2, 3, 4);
44
44
 
45
45
  | Property | Description |
46
46
  | ---- | ---- |
47
+ | [rem] | Find the remainder of x/y with sign of x (truncated division). |
48
+ | [mod] | Find the remainder of x/y with sign of y (floored division). |
49
+ | [modp] | Find the remainder of x/y with +ve sign (euclidean division). |
50
+ | [gcd] | Find the greatest common divisor of numbers. |
51
+ | [lcm] | Find the least common multiple of numbers. |
52
+ | [factorial] | Find the factorial of a number. |
53
+ | [binomial] | Find the number of ways to choose k elements from a set of n elements. |
54
+ | [multinomial] | Find the number of ways to put n objects in m bins (n=sum(kᵢ)). |
55
+ | | |
56
+ | [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
+ | [norm] | Normalize a number from its current range into a value between 0 and 1. |
60
+ | [map] | Re-map a number from one range to another. |
61
+ | [lerp] | Linearly interpolate a number between two numbers. |
62
+ | [gamma] | Compute the gamma function of a number (Γ). |
63
+ | [lgamma] | Compute the natural logarithm of the absolute value of the gamma function of a number (log-Γ). |
64
+ | [erf] | Find error function value of number (approximation). |
65
+ | [erfc] | Find the complementary error function value of number (approximation). |
66
+ | | |
67
+ | [degrees] | Convert radians to degrees. |
68
+ | [radians] | Convert degrees to radians. |
69
+ | [mag] | Calculate the magnitude (length) of a vector. |
70
+ | [dist] | Calculate the distance between two points. |
71
+ | | |
72
+ | [sum] | Find the sum of numbers (Σ). |
73
+ | [product] | Find the product of numbers (∏). |
74
+ | [mean] | Find the average of numbers. |
75
+ | [median] | Find the value separating the higher and lower halves of numbers. |
76
+ | [modes] | Find the values that appear most often. |
77
+ | [range] | Find the difference between the largest and smallest values. |
78
+ | [variance] | Find the mean of squared deviation of numbers from its mean. |
47
79
 
48
80
  <br>
49
81
  <br>
@@ -69,4 +101,32 @@ math.lcm(2, 3, 4);
69
101
  [![](https://img.youtube.com/vi/dW8Cy6WrO94/maxresdefault.jpg)](https://www.youtube.com/watch?v=dW8Cy6WrO94)
70
102
 
71
103
 
72
- [a]: https://nodef.github.io/extra-math/modules.html#sum
104
+ [rem]: https://nodef.github.io/extra-math/modules.html#rem
105
+ [mod]: https://nodef.github.io/extra-math/modules.html#mod
106
+ [modp]: https://nodef.github.io/extra-math/modules.html#modp
107
+ [gcd]: https://nodef.github.io/extra-math/modules.html#gcd
108
+ [lcm]: https://nodef.github.io/extra-math/modules.html#lcm
109
+ [factorial]: https://nodef.github.io/extra-math/modules.html#factorial
110
+ [binomial]: https://nodef.github.io/extra-math/modules.html#binomial
111
+ [multinomial]: https://nodef.github.io/extra-math/modules.html#multinomial
112
+ [constrain]: https://nodef.github.io/extra-math/modules.html#constrain
113
+ [root]: https://nodef.github.io/extra-math/modules.html#root
114
+ [log]: https://nodef.github.io/extra-math/modules.html#log
115
+ [norm]: https://nodef.github.io/extra-math/modules.html#norm
116
+ [map]: https://nodef.github.io/extra-math/modules.html#map
117
+ [lerp]: https://nodef.github.io/extra-math/modules.html#lerp
118
+ [gamma]: https://nodef.github.io/extra-math/modules.html#gamma
119
+ [lgamma]: https://nodef.github.io/extra-math/modules.html#lgamma
120
+ [erf]: https://nodef.github.io/extra-math/modules.html#erf
121
+ [erfc]: https://nodef.github.io/extra-math/modules.html#erfc
122
+ [degrees]: https://nodef.github.io/extra-math/modules.html#degrees
123
+ [radians]: https://nodef.github.io/extra-math/modules.html#radians
124
+ [mag]: https://nodef.github.io/extra-math/modules.html#mag
125
+ [dist]: https://nodef.github.io/extra-math/modules.html#dist
126
+ [sum]: https://nodef.github.io/extra-math/modules.html#sum
127
+ [product]: https://nodef.github.io/extra-math/modules.html#product
128
+ [mean]: https://nodef.github.io/extra-math/modules.html#mean
129
+ [median]: https://nodef.github.io/extra-math/modules.html#median
130
+ [modes]: https://nodef.github.io/extra-math/modules.html#modes
131
+ [range]: https://nodef.github.io/extra-math/modules.html#range
132
+ [variance]: https://nodef.github.io/extra-math/modules.html#variance
package/index.d.ts CHANGED
@@ -99,6 +99,30 @@ declare function map(x: number, r: number, R: number, t: number, T: number): num
99
99
  * @returns ∈ [x, y]
100
100
  */
101
101
  declare function lerp(x: number, y: number, t: number): number;
102
+ /**
103
+ * Compute the gamma function of a number (Γ).
104
+ * @param x a number
105
+ * @returns Γ(x); for +ve integer Γ(x) = x!
106
+ */
107
+ declare function gamma(x: number): number;
108
+ /**
109
+ * Compute the natural logarithm of the absolute value of the gamma function of a number (log-Γ).
110
+ * @param x a number
111
+ * @returns log(|Γ(x)|)
112
+ */
113
+ declare function lgamma(x: number): number;
114
+ /**
115
+ * Find error function value of number (approximation).
116
+ * @param x a number
117
+ * @returns erf(x)
118
+ */
119
+ declare function erf(x: number): number;
120
+ /**
121
+ * Find the complementary error function value of number (approximation).
122
+ * @param x a number
123
+ * @returns erfc(x)
124
+ */
125
+ declare function erfc(x: number): number;
102
126
  /**
103
127
  * Convert radians to degrees.
104
128
  * @param x radians
@@ -166,29 +190,5 @@ declare function range(...xs: number[]): number;
166
190
  * @returns σ² = E[(xs - µ)²] | µ = mean(xs)
167
191
  */
168
192
  declare function variance(...xs: number[]): number;
169
- /**
170
- * Compute the gamma function of a number (Γ).
171
- * @param x a number
172
- * @returns Γ(x); for +ve integer Γ(x) = x!
173
- */
174
- declare function gamma(x: number): number;
175
- /**
176
- * Compute the natural logarithm of the absolute value of the gamma function of a number (log-Γ).
177
- * @param x a number
178
- * @returns log(|Γ(x)|)
179
- */
180
- declare function lgamma(x: number): number;
181
- /**
182
- * Find error function value of number (approximation).
183
- * @param x a number
184
- * @returns erf(x)
185
- */
186
- declare function erf(x: number): number;
187
- /**
188
- * Find the complementary error function value of number (approximation).
189
- * @param x a number
190
- * @returns erfc(x)
191
- */
192
- declare function erfc(x: number): number;
193
193
 
194
194
  export { binomial, constrain, degrees, dist, erf, erfc, factorial, gamma, gcd, lcm, lerp, lgamma, log, mag, 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
- return product(...xs) / gcd(...xs);
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)
@@ -73,6 +76,53 @@ function map(x, r, R, t, T) {
73
76
  function lerp(x, y, t) {
74
77
  return x + t * (y - x);
75
78
  }
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
+ const ERF_A1 = 0.254829592;
91
+ const ERF_A2 = -0.284496736;
92
+ const ERF_A3 = 1.421413741;
93
+ const ERF_A4 = -1.453152027;
94
+ const ERF_A5 = 1.061405429;
95
+ const ERF_P = 0.3275911;
96
+ function erf(x) {
97
+ var sgn = x < 0 ? -1 : 1;
98
+ var x = Math.abs(x);
99
+ var t = 1 / (1 + ERF_P * x);
100
+ var y = 1 - (((((ERF_A5 * t + ERF_A4) * t) + ERF_A3) * t + ERF_A2) * t + ERF_A1) * t * Math.exp(-x * x);
101
+ return sgn * y;
102
+ }
103
+ const ERFC_TABLE = [
104
+ -2.8e-17, 1.21e-16, -9.4e-17, -1.523e-15, 7.106e-15,
105
+ 3.81e-16, -1.12708e-13, 3.13092e-13, 8.94487e-13,
106
+ -6.886027e-12, 2.394038e-12, 9.6467911e-11,
107
+ -2.27365122e-10, -9.91364156e-10, 5.059343495e-9,
108
+ 6.529054439e-9, -8.5238095915e-8, 1.5626441722e-8,
109
+ 1.303655835580e-6, -1.624290004647e-6,
110
+ -2.0278578112534e-5, 4.2523324806907e-5,
111
+ 3.66839497852761e-4, -9.46595344482036e-4,
112
+ -9.561514786808631e-3, 1.9476473204185836e-2,
113
+ 6.4196979235649026e-1, -1.3026537197817094
114
+ ];
115
+ function erfc(x) {
116
+ if (x < 0)
117
+ return 2 - erfc(-x);
118
+ var c = 0, d = 0, y = 2 / (2 + x), z = 4 * y - 2;
119
+ for (var i = 0, I = ERFC_TABLE.length - 1; i < I; i++) {
120
+ var t = d;
121
+ d = z * d - c + ERFC_TABLE[i];
122
+ c = t;
123
+ }
124
+ return y * Math.exp(-x * x + 0.5 * (ERFC_TABLE[I] + z * d) - c);
125
+ }
76
126
  function degrees(x) {
77
127
  return x * (180 / Math.PI);
78
128
  }
@@ -153,53 +203,6 @@ function variance(...xs) {
153
203
  a += (x - m) ** 2;
154
204
  return a / xs.length;
155
205
  }
156
- function gamma(x) {
157
- var x = x - 1;
158
- var e1 = Math.sqrt(2 * Math.PI * x);
159
- var e2 = Math.pow(x / Math.E, x);
160
- var e3 = Math.pow(x * Math.sinh(1 / x), x / 2);
161
- var e4 = Math.exp((7 / 324) * (1 / ((x ** 3) * (35 * x ** 2 + 33))));
162
- return e1 * e2 * e3 * e4;
163
- }
164
- function lgamma(x) {
165
- return Math.log(Math.abs(gamma(x)));
166
- }
167
- const ERF_A1 = 0.254829592;
168
- const ERF_A2 = -0.284496736;
169
- const ERF_A3 = 1.421413741;
170
- const ERF_A4 = -1.453152027;
171
- const ERF_A5 = 1.061405429;
172
- const ERF_P = 0.3275911;
173
- function erf(x) {
174
- var sgn = x < 0 ? -1 : 1;
175
- var x = Math.abs(x);
176
- var t = 1 / (1 + ERF_P * x);
177
- var y = 1 - (((((ERF_A5 * t + ERF_A4) * t) + ERF_A3) * t + ERF_A2) * t + ERF_A1) * t * Math.exp(-x * x);
178
- return sgn * y;
179
- }
180
- const ERFC_TABLE = [
181
- -2.8e-17, 1.21e-16, -9.4e-17, -1.523e-15, 7.106e-15,
182
- 3.81e-16, -1.12708e-13, 3.13092e-13, 8.94487e-13,
183
- -6.886027e-12, 2.394038e-12, 9.6467911e-11,
184
- -2.27365122e-10, -9.91364156e-10, 5.059343495e-9,
185
- 6.529054439e-9, -8.5238095915e-8, 1.5626441722e-8,
186
- 1.303655835580e-6, -1.624290004647e-6,
187
- -2.0278578112534e-5, 4.2523324806907e-5,
188
- 3.66839497852761e-4, -9.46595344482036e-4,
189
- -9.561514786808631e-3, 1.9476473204185836e-2,
190
- 6.4196979235649026e-1, -1.3026537197817094
191
- ];
192
- function erfc(x) {
193
- if (x < 0)
194
- return 2 - erfc(-x);
195
- var c = 0, d = 0, y = 2 / (2 + x), z = 4 * y - 2;
196
- for (var i = 0, I = ERFC_TABLE.length - 1; i < I; i++) {
197
- var t = d;
198
- d = z * d - c + ERFC_TABLE[i];
199
- c = t;
200
- }
201
- return y * Math.exp(-x * x + 0.5 * (ERFC_TABLE[I] + z * d) - c);
202
- }
203
206
 
204
207
  exports.binomial = binomial;
205
208
  exports.constrain = constrain;
package/index.mjs CHANGED
@@ -22,7 +22,10 @@ function gcd(...xs) {
22
22
  return a;
23
23
  }
24
24
  function lcm(...xs) {
25
- return product(...xs) / gcd(...xs);
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)
@@ -69,6 +72,53 @@ function map(x, r, R, t, T) {
69
72
  function lerp(x, y, t) {
70
73
  return x + t * (y - x);
71
74
  }
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
+ const ERF_A1 = 0.254829592;
87
+ const ERF_A2 = -0.284496736;
88
+ const ERF_A3 = 1.421413741;
89
+ const ERF_A4 = -1.453152027;
90
+ const ERF_A5 = 1.061405429;
91
+ const ERF_P = 0.3275911;
92
+ function erf(x) {
93
+ var sgn = x < 0 ? -1 : 1;
94
+ var x = Math.abs(x);
95
+ var t = 1 / (1 + ERF_P * x);
96
+ var y = 1 - (((((ERF_A5 * t + ERF_A4) * t) + ERF_A3) * t + ERF_A2) * t + ERF_A1) * t * Math.exp(-x * x);
97
+ return sgn * y;
98
+ }
99
+ const ERFC_TABLE = [
100
+ -2.8e-17, 1.21e-16, -9.4e-17, -1.523e-15, 7.106e-15,
101
+ 3.81e-16, -1.12708e-13, 3.13092e-13, 8.94487e-13,
102
+ -6.886027e-12, 2.394038e-12, 9.6467911e-11,
103
+ -2.27365122e-10, -9.91364156e-10, 5.059343495e-9,
104
+ 6.529054439e-9, -8.5238095915e-8, 1.5626441722e-8,
105
+ 1.303655835580e-6, -1.624290004647e-6,
106
+ -2.0278578112534e-5, 4.2523324806907e-5,
107
+ 3.66839497852761e-4, -9.46595344482036e-4,
108
+ -9.561514786808631e-3, 1.9476473204185836e-2,
109
+ 6.4196979235649026e-1, -1.3026537197817094
110
+ ];
111
+ function erfc(x) {
112
+ if (x < 0)
113
+ return 2 - erfc(-x);
114
+ var c = 0, d = 0, y = 2 / (2 + x), z = 4 * y - 2;
115
+ for (var i = 0, I = ERFC_TABLE.length - 1; i < I; i++) {
116
+ var t = d;
117
+ d = z * d - c + ERFC_TABLE[i];
118
+ c = t;
119
+ }
120
+ return y * Math.exp(-x * x + 0.5 * (ERFC_TABLE[I] + z * d) - c);
121
+ }
72
122
  function degrees(x) {
73
123
  return x * (180 / Math.PI);
74
124
  }
@@ -149,52 +199,5 @@ function variance(...xs) {
149
199
  a += (x - m) ** 2;
150
200
  return a / xs.length;
151
201
  }
152
- function gamma(x) {
153
- var x = x - 1;
154
- var e1 = Math.sqrt(2 * Math.PI * x);
155
- var e2 = Math.pow(x / Math.E, x);
156
- var e3 = Math.pow(x * Math.sinh(1 / x), x / 2);
157
- var e4 = Math.exp((7 / 324) * (1 / ((x ** 3) * (35 * x ** 2 + 33))));
158
- return e1 * e2 * e3 * e4;
159
- }
160
- function lgamma(x) {
161
- return Math.log(Math.abs(gamma(x)));
162
- }
163
- const ERF_A1 = 0.254829592;
164
- const ERF_A2 = -0.284496736;
165
- const ERF_A3 = 1.421413741;
166
- const ERF_A4 = -1.453152027;
167
- const ERF_A5 = 1.061405429;
168
- const ERF_P = 0.3275911;
169
- function erf(x) {
170
- var sgn = x < 0 ? -1 : 1;
171
- var x = Math.abs(x);
172
- var t = 1 / (1 + ERF_P * x);
173
- var y = 1 - (((((ERF_A5 * t + ERF_A4) * t) + ERF_A3) * t + ERF_A2) * t + ERF_A1) * t * Math.exp(-x * x);
174
- return sgn * y;
175
- }
176
- const ERFC_TABLE = [
177
- -2.8e-17, 1.21e-16, -9.4e-17, -1.523e-15, 7.106e-15,
178
- 3.81e-16, -1.12708e-13, 3.13092e-13, 8.94487e-13,
179
- -6.886027e-12, 2.394038e-12, 9.6467911e-11,
180
- -2.27365122e-10, -9.91364156e-10, 5.059343495e-9,
181
- 6.529054439e-9, -8.5238095915e-8, 1.5626441722e-8,
182
- 1.303655835580e-6, -1.624290004647e-6,
183
- -2.0278578112534e-5, 4.2523324806907e-5,
184
- 3.66839497852761e-4, -9.46595344482036e-4,
185
- -9.561514786808631e-3, 1.9476473204185836e-2,
186
- 6.4196979235649026e-1, -1.3026537197817094
187
- ];
188
- function erfc(x) {
189
- if (x < 0)
190
- return 2 - erfc(-x);
191
- var c = 0, d = 0, y = 2 / (2 + x), z = 4 * y - 2;
192
- for (var i = 0, I = ERFC_TABLE.length - 1; i < I; i++) {
193
- var t = d;
194
- d = z * d - c + ERFC_TABLE[i];
195
- c = t;
196
- }
197
- return y * Math.exp(-x * x + 0.5 * (ERFC_TABLE[I] + z * d) - c);
198
- }
199
202
 
200
203
  export { binomial, constrain, degrees, dist, erf, erfc, factorial, gamma, gcd, lcm, lerp, lgamma, log, mag, map, mean, median, mod, modes, modp, multinomial, norm, product, radians, range, rem, root, sum, variance };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-math",
3
- "version": "1.3.10",
3
+ "version": "1.3.13",
4
4
  "description": "Mathematics is the classification and study of all possible patterns.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",