extra-math 1.3.1 → 1.3.4

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.
Files changed (4) hide show
  1. package/index.d.ts +20 -14
  2. package/index.js +23 -4
  3. package/index.mjs +22 -5
  4. package/package.json +3 -1
package/index.d.ts CHANGED
@@ -112,24 +112,18 @@ declare function degrees(x: number): number;
112
112
  */
113
113
  declare function radians(x: number): number;
114
114
  /**
115
- * TODO: (use array?) Calculate the magnitude (length) of a vector.
116
- * @param x length on x-axis
117
- * @param y length on y-axis
118
- * @param z length on x-axis
115
+ * Calculate the magnitude (length) of a vector.
116
+ * @param xs vector ([x, y, z, ...])
119
117
  * @returns √(x² + y² + z²)
120
118
  */
121
- declare function mag(x: number, y: number, z: number): number;
119
+ declare function mag(xs: number[]): number;
122
120
  /**
123
- * TODO: (use array?) Calculate the distance between points.
124
- * @param x1 x-coordinate of first point
125
- * @param y1 y-coordinate of first point
126
- * @param z1 z-coordinate of first point
127
- * @param x2 x-coordinate of second point
128
- * @param y2 y-coordinate of second point
129
- * @param z2 z-coordinate of second point
121
+ * Calculate the distance between two points.
122
+ * @param xs first point ([x, y, z, ...])
123
+ * @param ys second point ([x, y, z, ...])
130
124
  * @returns √(Δx² + Δy² + Δz²)
131
125
  */
132
- declare function dist(x1: number, y1: number, z1: number, x2: number, y2: number, z2: number): number;
126
+ declare function dist(xs: number[], ys: number[]): number;
133
127
  /**
134
128
  * Find the sum of numbers (Σ).
135
129
  * @param xs a list of numbers
@@ -172,6 +166,18 @@ declare function range(...xs: number[]): number;
172
166
  * @returns σ² = E[(xs - µ)²] | µ = mean(xs)
173
167
  */
174
168
  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;
175
181
  /**
176
182
  * TODO: Find error function value of number (approximation).
177
183
  * @param x a number
@@ -183,4 +189,4 @@ declare function erf(x: number): number;
183
189
  */
184
190
  declare function erfc(x: number): number;
185
191
 
186
- export { binomial, constrain, degrees, dist, erf, erfc, factorial, gcd, lcm, lerp, log, mag, map, mean, median, mod, modes, modp, multinomial, norm, product, radians, range, rem, root, sum, variance };
192
+ 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
@@ -79,11 +79,17 @@ function degrees(x) {
79
79
  function radians(x) {
80
80
  return x * (Math.PI / 180);
81
81
  }
82
- function mag(x, y, z) {
83
- return Math.sqrt(x * x + y * y + z * z);
82
+ function mag(xs) {
83
+ var a = 0;
84
+ for (var i = 0, I = xs.length; i < I; i++)
85
+ a += xs[i] ** 2;
86
+ return Math.sqrt(a);
84
87
  }
85
- function dist(x1, y1, z1, x2, y2, z2) {
86
- return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2);
88
+ function dist(xs, ys) {
89
+ var a = 0;
90
+ for (var i = 0, I = xs.length; i < I; i++)
91
+ a += (xs[i] - ys[i]) ** 2;
92
+ return Math.sqrt(a);
87
93
  }
88
94
  function sum(...xs) {
89
95
  var a = 0;
@@ -147,6 +153,17 @@ function variance(...xs) {
147
153
  a += (x - m) ** 2;
148
154
  return a / xs.length;
149
155
  }
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
+ }
150
167
  const ERF_A1 = 0.254829592;
151
168
  const ERF_A2 = -0.284496736;
152
169
  const ERF_A3 = 1.421413741;
@@ -191,9 +208,11 @@ exports.dist = dist;
191
208
  exports.erf = erf;
192
209
  exports.erfc = erfc;
193
210
  exports.factorial = factorial;
211
+ exports.gamma = gamma;
194
212
  exports.gcd = gcd;
195
213
  exports.lcm = lcm;
196
214
  exports.lerp = lerp;
215
+ exports.lgamma = lgamma;
197
216
  exports.log = log;
198
217
  exports.mag = mag;
199
218
  exports.map = map;
package/index.mjs CHANGED
@@ -75,11 +75,17 @@ function degrees(x) {
75
75
  function radians(x) {
76
76
  return x * (Math.PI / 180);
77
77
  }
78
- function mag(x, y, z) {
79
- return Math.sqrt(x * x + y * y + z * z);
78
+ function mag(xs) {
79
+ var a = 0;
80
+ for (var i = 0, I = xs.length; i < I; i++)
81
+ a += xs[i] ** 2;
82
+ return Math.sqrt(a);
80
83
  }
81
- function dist(x1, y1, z1, x2, y2, z2) {
82
- return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2);
84
+ function dist(xs, ys) {
85
+ var a = 0;
86
+ for (var i = 0, I = xs.length; i < I; i++)
87
+ a += (xs[i] - ys[i]) ** 2;
88
+ return Math.sqrt(a);
83
89
  }
84
90
  function sum(...xs) {
85
91
  var a = 0;
@@ -143,6 +149,17 @@ function variance(...xs) {
143
149
  a += (x - m) ** 2;
144
150
  return a / xs.length;
145
151
  }
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
+ }
146
163
  const ERF_A1 = 0.254829592;
147
164
  const ERF_A2 = -0.284496736;
148
165
  const ERF_A3 = 1.421413741;
@@ -180,4 +197,4 @@ function erfc(x) {
180
197
  return y * Math.exp(-x * x + 0.5 * (ERFC_TABLE[I] + z * d) - c);
181
198
  }
182
199
 
183
- export { binomial, constrain, degrees, dist, erf, erfc, factorial, gcd, lcm, lerp, log, mag, map, mean, median, mod, modes, modp, multinomial, norm, product, radians, range, rem, root, sum, variance };
200
+ 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.1",
3
+ "version": "1.3.4",
4
4
  "description": "Mathematics is the classification and study of all possible patterns.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -24,9 +24,11 @@
24
24
  "erf",
25
25
  "erfc",
26
26
  "factorial",
27
+ "gamma",
27
28
  "gcd",
28
29
  "lcm",
29
30
  "lerp",
31
+ "lgamma",
30
32
  "log",
31
33
  "mag",
32
34
  "map",