extra-math 1.1.17 → 1.2.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 (6) hide show
  1. package/LICENSE +1 -1
  2. package/index.d.ts +131 -78
  3. package/index.js +155 -109
  4. package/index.mjs +145 -110
  5. package/package.json +22 -9
  6. package/example.js +0 -9
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2018-21 Subhajit Sahu
3
+ Copyright (c) 2018-22 Subhajit Sahu
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/index.d.ts CHANGED
@@ -1,117 +1,170 @@
1
- declare module "extra-math" {
2
-
3
1
  /**
4
- * Finds ways to choose k elements from a set of n elements.
5
- * [📘](https://github.com/nodef/extra-math/wiki/binomial)
2
+ * Find the remainder of x/y with sign of x (truncated division).
3
+ * @param x dividend
4
+ * @param y divisor
5
+ */
6
+ declare function rem(x: number, y: number): number;
7
+ /**
8
+ * Find the remainder of x/y with sign of y (floored division).
9
+ * @param x dividend
10
+ * @param y divisor
11
+ */
12
+ declare function mod(x: number, y: number): number;
13
+ /**
14
+ * Find the remainder of x/y with +ve sign (euclidean division).
15
+ * @param x dividend
16
+ * @param y divisor
17
+ */
18
+ declare function modp(x: number, y: number): number;
19
+ /**
20
+ * Find the greatest common divisor of numbers.
21
+ * @param xs a list of numbers
22
+ */
23
+ declare function gcd(...xs: number[]): number;
24
+ /**
25
+ * Find the least common multiple of numbers.
26
+ * @param xs a list of numbers
27
+ */
28
+ declare function lcm(...xs: number[]): number;
29
+ /**
30
+ * Find the factorial of a number.
31
+ * @param x a number
32
+ * @param k denominator factorial [0]
33
+ * @returns k=0: x!, x!/k!
34
+ */
35
+ declare function factorial(x: number, k?: number): number;
36
+ /**
37
+ * Find the number of ways to choose k elements from a set of n elements.
6
38
  * @param n elements in source set
7
39
  * @param k elements in choose set
8
40
  */
9
- export function binomial(n: number, k: number): number;
41
+ declare function binomial(n: number, k: number): number;
10
42
  /**
11
- * Converts radians to degrees.
12
- * [📘](https://github.com/nodef/extra-math/wiki/degrees)
13
- * @param x radians
43
+ * TODO: Find the number of ways to put n objects in m bins (n=sum(ki)).
44
+ * @param k objects per bin (ki)
14
45
  */
15
- export function degrees(x: number): number;
46
+ declare function multinomial(...k: number[]): number;
16
47
  /**
17
- * Finds error function value of number (approximation).
18
- * [📘](https://github.com/nodef/extra-math/wiki/erf)
19
- * @param n a number
48
+ * Constrain a number within a minimum and maximum value.
49
+ * @param x a number
50
+ * @param min minimum value
51
+ * @param max maximum value
52
+ * @returns x<min: min, x>max: max, x
20
53
  */
21
- export function erf(n: number): number;
54
+ declare function constrain(x: number, min: number, max: number): number;
22
55
  /**
23
- * Finds complementary error function value of number (approximation).
24
- * [📘](https://github.com/nodef/extra-math/wiki/erfc)
25
- * @param n a number
56
+ * Find the nth root of a number (ⁿ√).
57
+ * @param x a number
58
+ * @param n root
59
+ * @returns ⁿ√x
26
60
  */
27
- export function erfc(n: number): number;
61
+ declare function root(x: number, n: number): number;
28
62
  /**
29
- * Finds greatest common divisor of numbers.
30
- * [📘](https://github.com/nodef/extra-math/wiki/gcd)
31
- * @param n a list of numbers
63
+ * TODO: (log?) Find the logarithm of a number with a given base.
64
+ * @param x a number
65
+ * @param b logarithm base
66
+ * @returns log_b (x)
32
67
  */
33
- export function gcd(...n: number[]): number;
68
+ declare function logb(x: number, b: number): number;
34
69
  /**
35
- * Finds least common multiple of numbers.
36
- * [📘](https://github.com/nodef/extra-math/wiki/lcm)
37
- * @param n a list of numbers
70
+ * Normalize a number from its current range into a value between 0 and 1.
71
+ * @param x a number
72
+ * @param min lower bound of current range
73
+ * @param max upper bound of current range
74
+ * @returns ∈ [0, 1]
38
75
  */
39
- export function lcm(...n: number[]): number;
76
+ declare function norm(x: number, min: number, max: number): number;
40
77
  /**
41
- * Finds average of numbers.
42
- * [📘](https://github.com/nodef/extra-math/wiki/mean)
43
- * @param n a list of numbers
78
+ * Re-map a number from one range to another.
79
+ * @param x a number
80
+ * @param min lower bound of current range
81
+ * @param max upper bound of current range
82
+ * @param amin lower bound of target range
83
+ * @param amax upper bound of target range
84
+ * @returns ∈ [ymin, ymax]
44
85
  */
45
- export function mean(...n: number[]): number;
86
+ declare function map(x: number, min: number, max: number, amin: number, amax: number): number;
46
87
  /**
47
- * Finds the value separating the higher and lower halves of numbers.
48
- * [📘](https://github.com/nodef/extra-math/wiki/median)
49
- * @param n a list of numbers
88
+ * TODO: (argument order ok?) Calculate a number between two numbers at a specific increment.
89
+ * @param min minimum value
90
+ * @param max maximum value
91
+ * @param x a number between 0 to 1
92
+ * @returns ∈ [min, max]
50
93
  */
51
- export function median(...n: number[]): number;
94
+ declare function lerp(min: number, max: number, x: number): number;
52
95
  /**
53
- * Finds remainder of x/y with sign of y (floored division).
54
- * [📘](https://github.com/nodef/extra-math/wiki/mod)
55
- * @param x dividend
56
- * @param y divisor
96
+ * Convert radians to degrees.
97
+ * @param x radians
57
98
  */
58
- export function mod(x: number, y: number): number;
99
+ declare function degrees(x: number): number;
59
100
  /**
60
- * Finds the values that appear most often.
61
- * [📘](https://github.com/nodef/extra-math/wiki/modes)
62
- * @param n a list of numbers
101
+ * Convert degrees to radians.
102
+ * @param x degrees
63
103
  */
64
- export function modes(...n: number[]): number[];
104
+ declare function radians(x: number): number;
65
105
  /**
66
- * Finds remainder of x/y with +ve sign (euclidean division).
67
- * [📘](https://github.com/nodef/extra-math/wiki/modp)
68
- * @param x dividend
69
- * @param y divisor
106
+ * TODO: (use array?) Calculate the magnitude (length) of a vector.
107
+ * @param x length on x-axis
108
+ * @param y length on y-axis
109
+ * @param z length on x-axis
110
+ * @returns √(x² + y² + z²)
70
111
  */
71
- export function modp(x: number, y: number): number;
112
+ declare function mag(x: number, y: number, z: number): number;
72
113
  /**
73
- * Finds ways to put n objects in m bins (n=sum(ki)).
74
- * [📘](https://github.com/nodef/extra-math/wiki/multinomial)
75
- * @param k objects per bin (ki)
114
+ * TODO: (use array?) Calculate the distance between points.
115
+ * @param x1 x-coordinate of first point
116
+ * @param y1 y-coordinate of first point
117
+ * @param z1 z-coordinate of first point
118
+ * @param x2 x-coordinate of second point
119
+ * @param y2 y-coordinate of second point
120
+ * @param z2 z-coordinate of second point
121
+ * @returns √(Δx² + Δy² + Δz²)
76
122
  */
77
- export function multinomial(...k: number[]): number;
123
+ declare function dist(x1: number, y1: number, z1: number, x2: number, y2: number, z2: number): number;
78
124
  /**
79
- * Finds product of numbers (∏).
80
- * [📘](https://github.com/nodef/extra-math/wiki/product)
81
- * @param n a list of numbers
125
+ * Find the sum of numbers (Σ).
126
+ * @param xs a list of numbers
82
127
  */
83
- export function product(...n: number[]): number;
128
+ declare function sum(...xs: number[]): number;
84
129
  /**
85
- * Converts degrees to radians.
86
- * [📘](https://github.com/nodef/extra-math/wiki/radians)
87
- * @param x degrees
130
+ * Find the product of numbers (∏).
131
+ * @param xs a list of numbers
88
132
  */
89
- export function radians(x: number): number;
133
+ declare function product(...xs: number[]): number;
90
134
  /**
91
- * Finds the difference between the largest and smallest values.
92
- * [📘](https://github.com/nodef/extra-math/wiki/range)
93
- * @param n a list of numbers
135
+ * Find the average of numbers.
136
+ * @param xs a list of numbers
94
137
  */
95
- export function range(...n: number[]): number;
138
+ declare function mean(...xs: number[]): number;
96
139
  /**
97
- * Finds remainder of x/y with sign of x (truncated division).
98
- * [📘](https://github.com/nodef/extra-math/wiki/rem)
99
- * @param x dividend
100
- * @param y divisor
140
+ * Find the value separating the higher and lower halves of numbers.
141
+ * @param xs a list of numbers
142
+ */
143
+ declare function median(...xs: number[]): number;
144
+ /**
145
+ * Find the values that appear most often.
146
+ * @param xs a list of numbers
147
+ */
148
+ declare function modes(...xs: number[]): number[];
149
+ /**
150
+ * Find the difference between the largest and smallest values.
151
+ * @param xs a list of numbers
152
+ */
153
+ declare function range(...xs: number[]): number;
154
+ /**
155
+ * Find the mean of squared deviation of numbers from its mean.
156
+ * @param xs a list of numbers
101
157
  */
102
- export function rem(x: number, y: number): number;
158
+ declare function variance(...xs: number[]): number;
103
159
  /**
104
- * Finds sum of numbers (Σ).
105
- * [📘](https://github.com/nodef/extra-math/wiki/sum)
106
- * @param n a list of numbers
160
+ * TODO: Find error function value of number (approximation).
161
+ * @param x a number
107
162
  */
108
- export function sum(...n: number[]): number;
163
+ declare function erf(x: number): number;
109
164
  /**
110
- * Finds the mean of squared deviation of numbers from its mean.
111
- * [📘](https://github.com/nodef/extra-math/wiki/variance)
112
- * @param n a list of numbers
165
+ * TODO: Find the complementary error function value of number (approximation).
166
+ * @param x a number
113
167
  */
114
- export function variance(...n: number[]): number;
168
+ declare function erfc(x: number): number;
115
169
 
116
- export {};
117
- }
170
+ export { binomial, constrain, degrees, dist, erf, erfc, factorial, gcd, lcm, lerp, logb, mag, map, mean, median, mod, modes, modp, multinomial, norm, product, radians, range, rem, root, sum, variance };
package/index.js CHANGED
@@ -1,5 +1,40 @@
1
1
  'use strict';
2
+
2
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function rem(x, y) {
6
+ return x % y;
7
+ }
8
+ function mod(x, y) {
9
+ return x - y * Math.floor(x / y);
10
+ }
11
+ function modp(x, y) {
12
+ return x - Math.abs(y) * Math.floor(x / Math.abs(y));
13
+ }
14
+ function gcdPair(x, y) {
15
+ while (y !== 0) {
16
+ var t = y;
17
+ y = x % y;
18
+ x = t;
19
+ }
20
+ return x;
21
+ }
22
+ function gcd(...xs) {
23
+ var a = xs[0] || 1;
24
+ for (var i = 1, I = xs.length; i < I; i++)
25
+ a = gcdPair(a, xs[i]);
26
+ return a;
27
+ }
28
+ function lcm(...xs) {
29
+ return product(...xs) / gcd(...xs);
30
+ }
31
+ function factorial(x, k = 0) {
32
+ if (x < 0)
33
+ return 0;
34
+ for (var i = k + 1, a = 1; i <= x; i++)
35
+ a *= x;
36
+ return a;
37
+ }
3
38
  function binomial(n, k) {
4
39
  if (k < 0 || k > Math.abs(n))
5
40
  return 0;
@@ -10,158 +45,169 @@ function binomial(n, k) {
10
45
  a *= n / i;
11
46
  return a;
12
47
  }
48
+ function multinomial(...k) {
49
+ var n = sum(...k), a = 1;
50
+ for (var i = 0, j = 0, I = k.length; i < I;) {
51
+ if (j <= 0)
52
+ j = k[i++];
53
+ else
54
+ a *= n-- / j--;
55
+ }
56
+ return a;
57
+ }
58
+ function constrain(x, min, max) {
59
+ return Math.min(Math.max(x, min), max);
60
+ }
61
+ function root(x, n) {
62
+ return Math.pow(x, 1 / n);
63
+ }
64
+ function logb(x, b) {
65
+ return Math.log(x) / Math.log(b);
66
+ }
67
+ function norm(x, min, max) {
68
+ return (x - min) / (max - min);
69
+ }
70
+ function map(x, min, max, amin, amax) {
71
+ return amin + ((x - min) / (max - min)) * (amax - amin);
72
+ }
73
+ function lerp(min, max, x) {
74
+ return min + x * (max - min);
75
+ }
13
76
  function degrees(x) {
14
77
  return x * (180 / Math.PI);
15
78
  }
16
- const A1 = 0.254829592;
17
- const A2 = -0.284496736;
18
- const A3 = 1.421413741;
19
- const A4 = -1.453152027;
20
- const A5 = 1.061405429;
21
- const P = 0.3275911;
22
- function erf(n) {
23
- var sgn = n < 0 ? -1 : 1;
24
- var n = Math.abs(n);
25
- var t = 1 / (1 + P * n);
26
- var y = 1 - (((((A5 * t + A4) * t) + A3) * t + A2) * t + A1) * t * Math.exp(-n * n);
27
- return sgn * y;
79
+ function radians(x) {
80
+ return x * (Math.PI / 180);
28
81
  }
29
- const A = [
30
- -2.8e-17, 1.21e-16, -9.4e-17, -1.523e-15, 7.106e-15,
31
- 3.81e-16, -1.12708e-13, 3.13092e-13, 8.94487e-13,
32
- -6.886027e-12, 2.394038e-12, 9.6467911e-11,
33
- -2.27365122e-10, -9.91364156e-10, 5.059343495e-9,
34
- 6.529054439e-9, -8.5238095915e-8, 1.5626441722e-8,
35
- 1.303655835580e-6, -1.624290004647e-6,
36
- -2.0278578112534e-5, 4.2523324806907e-5,
37
- 3.66839497852761e-4, -9.46595344482036e-4,
38
- -9.561514786808631e-3, 1.9476473204185836e-2,
39
- 6.4196979235649026e-1, -1.3026537197817094
40
- ];
41
- function erfc(n) {
42
- if (n < 0)
43
- return 2 - erfc(-n);
44
- var c = 0, d = 0, y = 2 / (2 + n), z = 4 * y - 2;
45
- for (var i = 0, I = A.length - 1; i < I; i++) {
46
- var t = d;
47
- d = z * d - c + A[i];
48
- c = t;
49
- }
50
- return y * Math.exp(-n * n + 0.5 * (A[I] + z * d) - c);
82
+ function mag(x, y, z) {
83
+ return Math.sqrt(x * x + y * y + z * z);
51
84
  }
52
- function gcdPair(x, y) {
53
- while (y !== 0) {
54
- var t = y;
55
- y = x % y;
56
- x = t;
57
- }
58
- return x;
85
+ function dist(x1, y1, z1, x2, y2, z2) {
86
+ return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2);
59
87
  }
60
- function gcd(...n) {
61
- var a = n[0] || 1;
62
- for (var i = 1, I = n.length; i < I; i++)
63
- a = gcdPair(a, n[i]);
88
+ function sum(...xs) {
89
+ var a = 0;
90
+ for (var x of xs)
91
+ a += x;
64
92
  return a;
65
93
  }
66
- function product(...n) {
94
+ function product(...xs) {
67
95
  var a = 1;
68
- for (var i = 0, I = n.length; i < I; i++)
69
- a *= n[i];
70
- return a;
71
- }
72
- function lcm(...n) {
73
- return product(...n) / gcd(...n);
74
- }
75
- function sum(...n) {
76
- var a = 0;
77
- for (var i = 0, I = n.length; i < I; i++)
78
- a += n[i];
96
+ for (var x of xs)
97
+ a *= x;
79
98
  return a;
80
99
  }
81
- function mean(...n) {
82
- if (n.length === 0)
100
+ function mean(...xs) {
101
+ if (xs.length === 0)
83
102
  return 0;
84
- return sum(...n) / n.length;
103
+ return sum(...xs) / xs.length;
85
104
  }
86
- function median(...n) {
87
- if (n.length === 0)
105
+ function median(...xs) {
106
+ if (xs.length === 0)
88
107
  return 0;
89
- n.sort((a, b) => a - b);
90
- var i = n.length >> 1;
91
- if ((n.length & 1) === 1)
92
- return n[i];
93
- return (n[i - 1] + n[i]) / 2;
94
- }
95
- function mod(x, y) {
96
- return x - y * Math.floor(x / y);
97
- }
98
- function maxRepeat(ns) {
99
- var count = 0, max = 0;
100
- for (var i = 1, I = ns.length; i < I; i++) {
101
- if (ns[i - 1] === ns[i])
108
+ xs.sort((a, b) => a - b);
109
+ var i = xs.length >> 1;
110
+ if ((xs.length & 1) === 1)
111
+ return xs[i];
112
+ return (xs[i - 1] + xs[i]) / 2;
113
+ }
114
+ function maxRepeat(xs) {
115
+ var count = Math.min(xs.length, 1), max = count;
116
+ for (var i = 1, I = xs.length; i < I; i++) {
117
+ if (xs[i - 1] === xs[i])
102
118
  count++;
103
119
  else {
104
120
  max = Math.max(max, count);
105
- count = 0;
121
+ count = 1;
106
122
  }
107
123
  }
108
124
  return Math.max(max, count);
109
125
  }
110
- function getRepeats(ns, r) {
126
+ function getRepeats(xs, r) {
111
127
  var a = [];
112
- for (var i = 0, I = ns.length - r; i < I; i++)
113
- if (ns[i] === ns[i + r])
114
- a.push(ns[i += r]);
115
- return a;
116
- }
117
- function modes(...n) {
118
- n.sort((a, b) => a - b);
119
- var r = maxRepeat(n);
120
- return getRepeats(n, r);
121
- }
122
- function modp(x, y) {
123
- return x - Math.abs(y) * Math.floor(x / Math.abs(y));
124
- }
125
- function multinomial(...k) {
126
- var n = sum(...k), a = 1;
127
- for (var i = 0, j = 0, I = k.length; i < I;) {
128
- if (j <= 0)
129
- j = k[i++];
130
- else
131
- a *= n-- / j--;
132
- }
128
+ r--;
129
+ for (var i = 0, I = xs.length - r; i < I; i++)
130
+ if (xs[i] === xs[i + r])
131
+ a.push(xs[i += r]);
133
132
  return a;
134
133
  }
135
- function radians(x) {
136
- return x * (Math.PI / 180);
137
- }
138
- function range(...n) {
139
- return Math.max(...n) - Math.min(...n);
134
+ function modes(...xs) {
135
+ xs.sort((a, b) => a - b);
136
+ var r = maxRepeat(xs);
137
+ return getRepeats(xs, r);
140
138
  }
141
- function rem(x, y) {
142
- return x % y;
139
+ function range(...xs) {
140
+ return Math.max(...xs) - Math.min(...xs);
143
141
  }
144
- function variance(...n) {
145
- var m = mean(...n), a = 0;
146
- for (var x of n)
142
+ function variance(...xs) {
143
+ if (xs.length === 0)
144
+ return 0;
145
+ var m = mean(...xs), a = 0;
146
+ for (var x of xs)
147
147
  a += (x - m) ** 2;
148
- return n.length > 0 ? a / n.length : 0;
148
+ return a / xs.length;
149
+ }
150
+ const ERF_A1 = 0.254829592;
151
+ const ERF_A2 = -0.284496736;
152
+ const ERF_A3 = 1.421413741;
153
+ const ERF_A4 = -1.453152027;
154
+ const ERF_A5 = 1.061405429;
155
+ const ERF_P = 0.3275911;
156
+ function erf(x) {
157
+ var sgn = x < 0 ? -1 : 1;
158
+ var x = Math.abs(x);
159
+ var t = 1 / (1 + ERF_P * x);
160
+ var y = 1 - (((((ERF_A5 * t + ERF_A4) * t) + ERF_A3) * t + ERF_A2) * t + ERF_A1) * t * Math.exp(-x * x);
161
+ return sgn * y;
162
+ }
163
+ const ERFC_TABLE = [
164
+ -2.8e-17, 1.21e-16, -9.4e-17, -1.523e-15, 7.106e-15,
165
+ 3.81e-16, -1.12708e-13, 3.13092e-13, 8.94487e-13,
166
+ -6.886027e-12, 2.394038e-12, 9.6467911e-11,
167
+ -2.27365122e-10, -9.91364156e-10, 5.059343495e-9,
168
+ 6.529054439e-9, -8.5238095915e-8, 1.5626441722e-8,
169
+ 1.303655835580e-6, -1.624290004647e-6,
170
+ -2.0278578112534e-5, 4.2523324806907e-5,
171
+ 3.66839497852761e-4, -9.46595344482036e-4,
172
+ -9.561514786808631e-3, 1.9476473204185836e-2,
173
+ 6.4196979235649026e-1, -1.3026537197817094
174
+ ];
175
+ function erfc(x) {
176
+ if (x < 0)
177
+ return 2 - erfc(-x);
178
+ var c = 0, d = 0, y = 2 / (2 + x), z = 4 * y - 2;
179
+ for (var i = 0, I = ERFC_TABLE.length - 1; i < I; i++) {
180
+ var t = d;
181
+ d = z * d - c + ERFC_TABLE[i];
182
+ c = t;
183
+ }
184
+ return y * Math.exp(-x * x + 0.5 * (ERFC_TABLE[I] + z * d) - c);
149
185
  }
186
+
150
187
  exports.binomial = binomial;
188
+ exports.constrain = constrain;
151
189
  exports.degrees = degrees;
190
+ exports.dist = dist;
152
191
  exports.erf = erf;
153
192
  exports.erfc = erfc;
193
+ exports.factorial = factorial;
154
194
  exports.gcd = gcd;
155
195
  exports.lcm = lcm;
196
+ exports.lerp = lerp;
197
+ exports.logb = logb;
198
+ exports.mag = mag;
199
+ exports.map = map;
156
200
  exports.mean = mean;
157
201
  exports.median = median;
158
202
  exports.mod = mod;
159
203
  exports.modes = modes;
160
204
  exports.modp = modp;
161
205
  exports.multinomial = multinomial;
206
+ exports.norm = norm;
162
207
  exports.product = product;
163
208
  exports.radians = radians;
164
209
  exports.range = range;
165
210
  exports.rem = rem;
211
+ exports.root = root;
166
212
  exports.sum = sum;
167
213
  exports.variance = variance;
package/index.mjs CHANGED
@@ -1,3 +1,36 @@
1
+ function rem(x, y) {
2
+ return x % y;
3
+ }
4
+ function mod(x, y) {
5
+ return x - y * Math.floor(x / y);
6
+ }
7
+ function modp(x, y) {
8
+ return x - Math.abs(y) * Math.floor(x / Math.abs(y));
9
+ }
10
+ function gcdPair(x, y) {
11
+ while (y !== 0) {
12
+ var t = y;
13
+ y = x % y;
14
+ x = t;
15
+ }
16
+ return x;
17
+ }
18
+ function gcd(...xs) {
19
+ var a = xs[0] || 1;
20
+ for (var i = 1, I = xs.length; i < I; i++)
21
+ a = gcdPair(a, xs[i]);
22
+ return a;
23
+ }
24
+ function lcm(...xs) {
25
+ return product(...xs) / gcd(...xs);
26
+ }
27
+ function factorial(x, k = 0) {
28
+ if (x < 0)
29
+ return 0;
30
+ for (var i = k + 1, a = 1; i <= x; i++)
31
+ a *= x;
32
+ return a;
33
+ }
1
34
  function binomial(n, k) {
2
35
  if (k < 0 || k > Math.abs(n))
3
36
  return 0;
@@ -8,141 +41,143 @@ function binomial(n, k) {
8
41
  a *= n / i;
9
42
  return a;
10
43
  }
44
+ function multinomial(...k) {
45
+ var n = sum(...k), a = 1;
46
+ for (var i = 0, j = 0, I = k.length; i < I;) {
47
+ if (j <= 0)
48
+ j = k[i++];
49
+ else
50
+ a *= n-- / j--;
51
+ }
52
+ return a;
53
+ }
54
+ function constrain(x, min, max) {
55
+ return Math.min(Math.max(x, min), max);
56
+ }
57
+ function root(x, n) {
58
+ return Math.pow(x, 1 / n);
59
+ }
60
+ function logb(x, b) {
61
+ return Math.log(x) / Math.log(b);
62
+ }
63
+ function norm(x, min, max) {
64
+ return (x - min) / (max - min);
65
+ }
66
+ function map(x, min, max, amin, amax) {
67
+ return amin + ((x - min) / (max - min)) * (amax - amin);
68
+ }
69
+ function lerp(min, max, x) {
70
+ return min + x * (max - min);
71
+ }
11
72
  function degrees(x) {
12
73
  return x * (180 / Math.PI);
13
74
  }
14
- const A1 = 0.254829592;
15
- const A2 = -0.284496736;
16
- const A3 = 1.421413741;
17
- const A4 = -1.453152027;
18
- const A5 = 1.061405429;
19
- const P = 0.3275911;
20
- function erf(n) {
21
- var sgn = n < 0 ? -1 : 1;
22
- var n = Math.abs(n);
23
- var t = 1 / (1 + P * n);
24
- var y = 1 - (((((A5 * t + A4) * t) + A3) * t + A2) * t + A1) * t * Math.exp(-n * n);
25
- return sgn * y;
75
+ function radians(x) {
76
+ return x * (Math.PI / 180);
26
77
  }
27
- const A = [
28
- -2.8e-17, 1.21e-16, -9.4e-17, -1.523e-15, 7.106e-15,
29
- 3.81e-16, -1.12708e-13, 3.13092e-13, 8.94487e-13,
30
- -6.886027e-12, 2.394038e-12, 9.6467911e-11,
31
- -2.27365122e-10, -9.91364156e-10, 5.059343495e-9,
32
- 6.529054439e-9, -8.5238095915e-8, 1.5626441722e-8,
33
- 1.303655835580e-6, -1.624290004647e-6,
34
- -2.0278578112534e-5, 4.2523324806907e-5,
35
- 3.66839497852761e-4, -9.46595344482036e-4,
36
- -9.561514786808631e-3, 1.9476473204185836e-2,
37
- 6.4196979235649026e-1, -1.3026537197817094
38
- ];
39
- function erfc(n) {
40
- if (n < 0)
41
- return 2 - erfc(-n);
42
- var c = 0, d = 0, y = 2 / (2 + n), z = 4 * y - 2;
43
- for (var i = 0, I = A.length - 1; i < I; i++) {
44
- var t = d;
45
- d = z * d - c + A[i];
46
- c = t;
47
- }
48
- return y * Math.exp(-n * n + 0.5 * (A[I] + z * d) - c);
78
+ function mag(x, y, z) {
79
+ return Math.sqrt(x * x + y * y + z * z);
49
80
  }
50
- function gcdPair(x, y) {
51
- while (y !== 0) {
52
- var t = y;
53
- y = x % y;
54
- x = t;
55
- }
56
- return x;
81
+ function dist(x1, y1, z1, x2, y2, z2) {
82
+ return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2);
57
83
  }
58
- function gcd(...n) {
59
- var a = n[0] || 1;
60
- for (var i = 1, I = n.length; i < I; i++)
61
- a = gcdPair(a, n[i]);
84
+ function sum(...xs) {
85
+ var a = 0;
86
+ for (var x of xs)
87
+ a += x;
62
88
  return a;
63
89
  }
64
- function product(...n) {
90
+ function product(...xs) {
65
91
  var a = 1;
66
- for (var i = 0, I = n.length; i < I; i++)
67
- a *= n[i];
68
- return a;
69
- }
70
- function lcm(...n) {
71
- return product(...n) / gcd(...n);
72
- }
73
- function sum(...n) {
74
- var a = 0;
75
- for (var i = 0, I = n.length; i < I; i++)
76
- a += n[i];
92
+ for (var x of xs)
93
+ a *= x;
77
94
  return a;
78
95
  }
79
- function mean(...n) {
80
- if (n.length === 0)
96
+ function mean(...xs) {
97
+ if (xs.length === 0)
81
98
  return 0;
82
- return sum(...n) / n.length;
99
+ return sum(...xs) / xs.length;
83
100
  }
84
- function median(...n) {
85
- if (n.length === 0)
101
+ function median(...xs) {
102
+ if (xs.length === 0)
86
103
  return 0;
87
- n.sort((a, b) => a - b);
88
- var i = n.length >> 1;
89
- if ((n.length & 1) === 1)
90
- return n[i];
91
- return (n[i - 1] + n[i]) / 2;
92
- }
93
- function mod(x, y) {
94
- return x - y * Math.floor(x / y);
95
- }
96
- function maxRepeat(ns) {
97
- var count = 0, max = 0;
98
- for (var i = 1, I = ns.length; i < I; i++) {
99
- if (ns[i - 1] === ns[i])
104
+ xs.sort((a, b) => a - b);
105
+ var i = xs.length >> 1;
106
+ if ((xs.length & 1) === 1)
107
+ return xs[i];
108
+ return (xs[i - 1] + xs[i]) / 2;
109
+ }
110
+ function maxRepeat(xs) {
111
+ var count = Math.min(xs.length, 1), max = count;
112
+ for (var i = 1, I = xs.length; i < I; i++) {
113
+ if (xs[i - 1] === xs[i])
100
114
  count++;
101
115
  else {
102
116
  max = Math.max(max, count);
103
- count = 0;
117
+ count = 1;
104
118
  }
105
119
  }
106
120
  return Math.max(max, count);
107
121
  }
108
- function getRepeats(ns, r) {
122
+ function getRepeats(xs, r) {
109
123
  var a = [];
110
- for (var i = 0, I = ns.length - r; i < I; i++)
111
- if (ns[i] === ns[i + r])
112
- a.push(ns[i += r]);
113
- return a;
114
- }
115
- function modes(...n) {
116
- n.sort((a, b) => a - b);
117
- var r = maxRepeat(n);
118
- return getRepeats(n, r);
119
- }
120
- function modp(x, y) {
121
- return x - Math.abs(y) * Math.floor(x / Math.abs(y));
122
- }
123
- function multinomial(...k) {
124
- var n = sum(...k), a = 1;
125
- for (var i = 0, j = 0, I = k.length; i < I;) {
126
- if (j <= 0)
127
- j = k[i++];
128
- else
129
- a *= n-- / j--;
130
- }
124
+ r--;
125
+ for (var i = 0, I = xs.length - r; i < I; i++)
126
+ if (xs[i] === xs[i + r])
127
+ a.push(xs[i += r]);
131
128
  return a;
132
129
  }
133
- function radians(x) {
134
- return x * (Math.PI / 180);
135
- }
136
- function range(...n) {
137
- return Math.max(...n) - Math.min(...n);
130
+ function modes(...xs) {
131
+ xs.sort((a, b) => a - b);
132
+ var r = maxRepeat(xs);
133
+ return getRepeats(xs, r);
138
134
  }
139
- function rem(x, y) {
140
- return x % y;
135
+ function range(...xs) {
136
+ return Math.max(...xs) - Math.min(...xs);
141
137
  }
142
- function variance(...n) {
143
- var m = mean(...n), a = 0;
144
- for (var x of n)
138
+ function variance(...xs) {
139
+ if (xs.length === 0)
140
+ return 0;
141
+ var m = mean(...xs), a = 0;
142
+ for (var x of xs)
145
143
  a += (x - m) ** 2;
146
- return n.length > 0 ? a / n.length : 0;
144
+ return a / xs.length;
145
+ }
146
+ const ERF_A1 = 0.254829592;
147
+ const ERF_A2 = -0.284496736;
148
+ const ERF_A3 = 1.421413741;
149
+ const ERF_A4 = -1.453152027;
150
+ const ERF_A5 = 1.061405429;
151
+ const ERF_P = 0.3275911;
152
+ function erf(x) {
153
+ var sgn = x < 0 ? -1 : 1;
154
+ var x = Math.abs(x);
155
+ var t = 1 / (1 + ERF_P * x);
156
+ var y = 1 - (((((ERF_A5 * t + ERF_A4) * t) + ERF_A3) * t + ERF_A2) * t + ERF_A1) * t * Math.exp(-x * x);
157
+ return sgn * y;
158
+ }
159
+ const ERFC_TABLE = [
160
+ -2.8e-17, 1.21e-16, -9.4e-17, -1.523e-15, 7.106e-15,
161
+ 3.81e-16, -1.12708e-13, 3.13092e-13, 8.94487e-13,
162
+ -6.886027e-12, 2.394038e-12, 9.6467911e-11,
163
+ -2.27365122e-10, -9.91364156e-10, 5.059343495e-9,
164
+ 6.529054439e-9, -8.5238095915e-8, 1.5626441722e-8,
165
+ 1.303655835580e-6, -1.624290004647e-6,
166
+ -2.0278578112534e-5, 4.2523324806907e-5,
167
+ 3.66839497852761e-4, -9.46595344482036e-4,
168
+ -9.561514786808631e-3, 1.9476473204185836e-2,
169
+ 6.4196979235649026e-1, -1.3026537197817094
170
+ ];
171
+ function erfc(x) {
172
+ if (x < 0)
173
+ return 2 - erfc(-x);
174
+ var c = 0, d = 0, y = 2 / (2 + x), z = 4 * y - 2;
175
+ for (var i = 0, I = ERFC_TABLE.length - 1; i < I; i++) {
176
+ var t = d;
177
+ d = z * d - c + ERFC_TABLE[i];
178
+ c = t;
179
+ }
180
+ return y * Math.exp(-x * x + 0.5 * (ERFC_TABLE[I] + z * d) - c);
147
181
  }
148
- export { binomial, degrees, erf, erfc, gcd, lcm, mean, median, mod, modes, modp, multinomial, product, radians, range, rem, sum, variance };
182
+
183
+ export { binomial, constrain, degrees, dist, erf, erfc, factorial, gcd, lcm, lerp, logb, mag, map, mean, median, mod, modes, modp, multinomial, norm, product, radians, range, rem, root, sum, variance };
package/package.json CHANGED
@@ -1,52 +1,65 @@
1
1
  {
2
2
  "name": "extra-math",
3
- "version": "1.1.17",
3
+ "version": "1.2.4",
4
4
  "description": "Mathematics is the classification and study of all possible patterns.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
7
+ "sideEffects": false,
7
8
  "exports": {
8
9
  "require": "./index.js",
9
10
  "import": "./index.mjs"
10
11
  },
11
12
  "scripts": {
12
13
  "test": "exit",
13
- "build": "ebuild export+main+example+wiki+readme --wikiExample=false --asciinema=false",
14
- "deploy": "ebuild jsdoc+export+main+meta+example+github+publish"
15
- },
16
- "runkitExampleFilename": "example.js",
17
- "repository": {
18
- "type": "git",
19
- "url": "git+https://github.com/nodef/extra-math.git"
14
+ "build": "node ./build",
15
+ "deploy": "node ./build deploy"
20
16
  },
21
17
  "keywords": [
22
18
  "extra",
23
19
  "math",
24
20
  "binomial",
21
+ "constrain",
25
22
  "degrees",
23
+ "dist",
26
24
  "erf",
27
25
  "erfc",
26
+ "factorial",
28
27
  "gcd",
29
28
  "lcm",
29
+ "lerp",
30
+ "logb",
31
+ "mag",
32
+ "map",
30
33
  "mean",
31
34
  "median",
32
35
  "mod",
33
36
  "modes",
34
37
  "modp",
35
38
  "multinomial",
39
+ "norm",
36
40
  "product",
37
41
  "radians",
38
42
  "range",
39
43
  "rem",
44
+ "root",
40
45
  "sum",
41
46
  "variance"
42
47
  ],
43
48
  "author": "wolfram77@gmail.com",
44
49
  "license": "MIT",
50
+ "repository": {
51
+ "type": "git",
52
+ "url": "git+https://github.com/nodef/extra-math.git"
53
+ },
45
54
  "bugs": {
46
55
  "url": "https://github.com/nodef/extra-math/issues"
47
56
  },
48
57
  "homepage": "https://github.com/nodef/extra-math#readme",
49
58
  "devDependencies": {
50
- "extra-build": "^1.3.26"
59
+ "@rollup/plugin-commonjs": "^22.0.1",
60
+ "@rollup/plugin-node-resolve": "^13.3.0",
61
+ "extra-build": "^2.1.28",
62
+ "rollup-plugin-cleanup": "^3.2.1",
63
+ "rollup-plugin-dts": "^4.2.2"
51
64
  }
52
65
  }
package/example.js DELETED
@@ -1,9 +0,0 @@
1
- const math = require("extra-math");
2
-
3
- math.sum(1, 2, 3, 4);
4
-
5
- math.median(1, 7, 8);
6
-
7
- math.variance(1, 2, 3, 4);
8
-
9
- math.lcm(2, 3, 4);