extra-math 1.1.18 โ†’ 1.2.5

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 +134 -270
  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,309 +1,173 @@
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
+ * @returns trunc(x/y)
6
+ */
7
+ declare function rem(x: number, y: number): number;
8
+ /**
9
+ * Find the remainder of x/y with sign of y (floored division).
10
+ * @param x dividend
11
+ * @param y divisor
12
+ * @returns floor(x/y)
13
+ */
14
+ declare function mod(x: number, y: number): number;
15
+ /**
16
+ * Find the remainder of x/y with +ve sign (euclidean division).
17
+ * @param x dividend
18
+ * @param y divisor
19
+ * @returns n>0: floor(x/y), n<0: ceil(x/y)
20
+ */
21
+ declare function modp(x: number, y: number): number;
22
+ /**
23
+ * Find the greatest common divisor of numbers.
24
+ * @param xs a list of numbers
25
+ */
26
+ declare function gcd(...xs: number[]): number;
27
+ /**
28
+ * Find the least common multiple of numbers.
29
+ * @param xs a list of numbers
30
+ */
31
+ declare function lcm(...xs: number[]): number;
32
+ /**
33
+ * Find the factorial of a number.
34
+ * @param x a number
35
+ * @param k denominator factorial [0]
36
+ * @returns k=0: x!, x!/k!
37
+ */
38
+ declare function factorial(x: number, k?: number): number;
39
+ /**
40
+ * Find the number of ways to choose k elements from a set of n elements.
6
41
  * @param n elements in source set
7
42
  * @param k elements in choose set
8
- * @example
9
- * ```javascript
10
- * binomial(4, 1);
11
- * // 4
12
- *
13
- * binomial(4, 2);
14
- * // 6
15
- *
16
- * binomial(4, 3);
17
- * // 4
18
- * ```
19
43
  */
20
- export function binomial(n: number, k: number): number;
44
+ declare function binomial(n: number, k: number): number;
21
45
  /**
22
- * Converts radians to degrees.
23
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/degrees)
24
- * @param x radians
25
- * @example
26
- * ```javascript
27
- * degrees(3);
28
- * // 171.88733853924697
29
- *
30
- * degrees(1);
31
- * // 57.29577951308232
32
- * ```
46
+ * Find the number of ways to put n objects in m bins (n=sum(kแตข)).
47
+ * @param ks objects per bin (kแตข)
33
48
  */
34
- export function degrees(x: number): number;
49
+ declare function multinomial(...ks: number[]): number;
35
50
  /**
36
- * Finds error function value of number (approximation).
37
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/erf)
38
- * @param n a number
39
- * @example
40
- * ```javascript
41
- * erf(0);
42
- * // 9.999999717180685e-10
43
- *
44
- * erf(0.1);
45
- * // 0.11246296562219549
46
- *
47
- * erf(1);
48
- * // 0.8427006897475899
49
- * ```
51
+ * Constrain a number within a minimum and a maximum value.
52
+ * @param x a number
53
+ * @param min minimum value
54
+ * @param max maximum value
55
+ * @returns x<min: min, x>max: max, x
50
56
  */
51
- export function erf(n: number): number;
57
+ declare function constrain(x: number, min: number, max: number): number;
52
58
  /**
53
- * Finds complementary error function value of number (approximation).
54
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/erfc)
55
- * @param n a number
56
- * @example
57
- * ```javascript
58
- * erfc(0);
59
- * // 0.9999999999999999
60
- *
61
- * erfc(0.1);
62
- * // 0.887537083981715
63
- *
64
- * erfc(1);
65
- * // 0.1572992070502851
66
- * ```
59
+ * Find the nth root of a number (โฟโˆš).
60
+ * @param x a number
61
+ * @param n root
62
+ * @returns โฟโˆšx
67
63
  */
68
- export function erfc(n: number): number;
64
+ declare function root(x: number, n: number): number;
69
65
  /**
70
- * Finds greatest common divisor of numbers.
71
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/gcd)
72
- * @param n a list of numbers
73
- * @example
74
- * ```javascript
75
- * gcd(6, 15);
76
- * // 3
77
- *
78
- * gcd(6, 15, 21);
79
- * // 3
80
- *
81
- * gcd(6, 15, 20);
82
- * // 1
83
- * ```
66
+ * Find the logarithm of a number with a given base.
67
+ * @param x a number
68
+ * @param b logarithm base
69
+ * @returns log_b (x)
84
70
  */
85
- export function gcd(...n: number[]): number;
71
+ declare function logb(x: number, b: number): number;
86
72
  /**
87
- * Finds least common multiple of numbers.
88
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/lcm)
89
- * @param n a list of numbers
90
- * @example
91
- * ```javascript
92
- * lcm(2, 3);
93
- * // 6
94
- *
95
- * lcm(2, 3, 4);
96
- * // 12
97
- *
98
- * lcm(2, 3, 4, 5);
99
- * // 60
100
- * ```
73
+ * Normalize a number from its current range into a value between 0 and 1.
74
+ * @param x a number
75
+ * @param min lower bound of current range
76
+ * @param max upper bound of current range
77
+ * @returns โˆˆ [0, 1]
101
78
  */
102
- export function lcm(...n: number[]): number;
79
+ declare function norm(x: number, min: number, max: number): number;
103
80
  /**
104
- * Finds average of numbers.
105
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/mean)
106
- * @param n a list of numbers
107
- * @example
108
- * ```javascript
109
- * mean(1, 2);
110
- * // 1.5
111
- *
112
- * mean(1, 2, 3);
113
- * // 2
114
- *
115
- * mean(1, 2, 3, 4);
116
- * // 2.5
117
- * ```
81
+ * Re-map a number from one range to another.
82
+ * @param x a number
83
+ * @param min lower bound of current range
84
+ * @param max upper bound of current range
85
+ * @param amin lower bound of target range
86
+ * @param amax upper bound of target range
87
+ * @returns โˆˆ [ymin, ymax]
118
88
  */
119
- export function mean(...n: number[]): number;
89
+ declare function map(x: number, min: number, max: number, amin: number, amax: number): number;
120
90
  /**
121
- * Finds the value separating the higher and lower halves of numbers.
122
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/median)
123
- * @param n a list of numbers
124
- * @example
125
- * ```javascript
126
- * median(1, 7);
127
- * // 4
128
- *
129
- * median(1, 7, 8);
130
- * // 7
131
- *
132
- * median(1, 7, 8, 10);
133
- * // 7.5
134
- * ```
91
+ * TODO: (argument order ok?) Calculate a number between two numbers at a specific increment.
92
+ * @param min minimum value
93
+ * @param max maximum value
94
+ * @param x a number between 0 to 1
95
+ * @returns โˆˆ [min, max]
135
96
  */
136
- export function median(...n: number[]): number;
97
+ declare function lerp(min: number, max: number, x: number): number;
137
98
  /**
138
- * Finds remainder of x/y with sign of y (floored division).
139
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/mod)
140
- * @param x dividend
141
- * @param y divisor
142
- * @example
143
- * ```javascript
144
- * mod(1, 10);
145
- * // 1
146
- *
147
- * mod(-1, 10);
148
- * // 9
149
- *
150
- * mod(1, -10);
151
- * // -9
152
- * ```
99
+ * Convert radians to degrees.
100
+ * @param x radians
153
101
  */
154
- export function mod(x: number, y: number): number;
102
+ declare function degrees(x: number): number;
155
103
  /**
156
- * Finds the values that appear most often.
157
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/modes)
158
- * @param n a list of numbers
159
- * @example
160
- * ```javascript
161
- * modes(1, 2);
162
- * // [1, 2]
163
- *
164
- * modes(1, 2, 2);
165
- * // [2]
166
- *
167
- * modes(1, 2, 2, 3, 3);
168
- * // [2, 3]
169
- * ```
104
+ * Convert degrees to radians.
105
+ * @param x degrees
170
106
  */
171
- export function modes(...n: number[]): number[];
107
+ declare function radians(x: number): number;
172
108
  /**
173
- * Finds remainder of x/y with +ve sign (euclidean division).
174
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/modp)
175
- * @param x dividend
176
- * @param y divisor
177
- * @example
178
- * ```javascript
179
- * modp(1, 10);
180
- * // 1
181
- *
182
- * modp(-1, 10);
183
- * // 9
184
- *
185
- * modp(1, -10);
186
- * // 1
187
- * ```
109
+ * TODO: (use array?) Calculate the magnitude (length) of a vector.
110
+ * @param x length on x-axis
111
+ * @param y length on y-axis
112
+ * @param z length on x-axis
113
+ * @returns โˆš(xยฒ + yยฒ + zยฒ)
188
114
  */
189
- export function modp(x: number, y: number): number;
115
+ declare function mag(x: number, y: number, z: number): number;
190
116
  /**
191
- * Finds ways to put n objects in m bins (n=sum(ki)).
192
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/multinomial)
193
- * @param k objects per bin (ki)
194
- * @example
195
- * ```javascript
196
- * multinomial(1, 2);
197
- * // 3
198
- *
199
- * multinomial(1, 2, 3);
200
- * // 60
201
- *
202
- * multinomial(1, 2, 3, 4);
203
- * // 12600
204
- * ```
117
+ * TODO: (use array?) Calculate the distance between points.
118
+ * @param x1 x-coordinate of first point
119
+ * @param y1 y-coordinate of first point
120
+ * @param z1 z-coordinate of first point
121
+ * @param x2 x-coordinate of second point
122
+ * @param y2 y-coordinate of second point
123
+ * @param z2 z-coordinate of second point
124
+ * @returns โˆš(ฮ”xยฒ + ฮ”yยฒ + ฮ”zยฒ)
205
125
  */
206
- export function multinomial(...k: number[]): number;
126
+ declare function dist(x1: number, y1: number, z1: number, x2: number, y2: number, z2: number): number;
207
127
  /**
208
- * Finds product of numbers (โˆ).
209
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/product)
210
- * @param n a list of numbers
211
- * @example
212
- * ```javascript
213
- * product(1, 2);
214
- * // 2
215
- *
216
- * product(1, 2, 3);
217
- * // 6
218
- *
219
- * product(1, 2, 3, 4);
220
- * // 24
221
- * ```
128
+ * Find the sum of numbers (ฮฃ).
129
+ * @param xs a list of numbers
222
130
  */
223
- export function product(...n: number[]): number;
131
+ declare function sum(...xs: number[]): number;
224
132
  /**
225
- * Converts degrees to radians.
226
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/radians)
227
- * @param x degrees
228
- * @example
229
- * ```javascript
230
- * radians(180);
231
- * // 3.141592653589793
232
- *
233
- * radians(60);
234
- * // 1.0471975511965976
235
- * ```
133
+ * Find the product of numbers (โˆ).
134
+ * @param xs a list of numbers
236
135
  */
237
- export function radians(x: number): number;
136
+ declare function product(...xs: number[]): number;
238
137
  /**
239
- * Finds the difference between the largest and smallest values.
240
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/range)
241
- * @param n a list of numbers
242
- * @example
243
- * ```javascript
244
- * range(1, 7);
245
- * // 6
246
- *
247
- * range(1, 7, 6);
248
- * // 6
249
- *
250
- * range(1, 7, 8, 6);
251
- * // 7
252
- * ```
138
+ * Find the average of numbers.
139
+ * @param xs a list of numbers
253
140
  */
254
- export function range(...n: number[]): number;
141
+ declare function mean(...xs: number[]): number;
255
142
  /**
256
- * Finds remainder of x/y with sign of x (truncated division).
257
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/rem)
258
- * @param x dividend
259
- * @param y divisor
260
- * @example
261
- * ```javascript
262
- * rem(1, 10);
263
- * // 1
264
- *
265
- * rem(-1, 10);
266
- * // -1
267
- *
268
- * rem(1, -10);
269
- * // 1
270
- * ```
143
+ * Find the value separating the higher and lower halves of numbers.
144
+ * @param xs a list of numbers
145
+ */
146
+ declare function median(...xs: number[]): number;
147
+ /**
148
+ * Find the values that appear most often.
149
+ * @param xs a list of numbers
150
+ */
151
+ declare function modes(...xs: number[]): number[];
152
+ /**
153
+ * Find the difference between the largest and smallest values.
154
+ * @param xs a list of numbers
155
+ */
156
+ declare function range(...xs: number[]): number;
157
+ /**
158
+ * Find the mean of squared deviation of numbers from its mean.
159
+ * @param xs a list of numbers
271
160
  */
272
- export function rem(x: number, y: number): number;
161
+ declare function variance(...xs: number[]): number;
273
162
  /**
274
- * Finds sum of numbers (ฮฃ).
275
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/sum)
276
- * @param n a list of numbers
277
- * @example
278
- * ```javascript
279
- * sum(1, 2);
280
- * // 3
281
- *
282
- * sum(1, 2, 3);
283
- * // 6
284
- *
285
- * sum(1, 2, 3, 4);
286
- * // 10
287
- * ```
163
+ * TODO: Find error function value of number (approximation).
164
+ * @param x a number
288
165
  */
289
- export function sum(...n: number[]): number;
166
+ declare function erf(x: number): number;
290
167
  /**
291
- * Finds the mean of squared deviation of numbers from its mean.
292
- * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/variance)
293
- * @param n a list of numbers
294
- * @example
295
- * ```javascript
296
- * variance(1, 2);
297
- * // 0.25
298
- *
299
- * variance(1, 2, 3);
300
- * // 0.6666666666666666
301
- *
302
- * variance(1, 2, 3, 4);
303
- * // 1.25
304
- * ```
168
+ * TODO: Find the complementary error function value of number (approximation).
169
+ * @param x a number
305
170
  */
306
- export function variance(...n: number[]): number;
171
+ declare function erfc(x: number): number;
307
172
 
308
- export {};
309
- }
173
+ 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(...ks) {
49
+ var n = sum(...ks), a = 1;
50
+ for (var i = 0, j = 0, I = ks.length; i < I;) {
51
+ if (j <= 0)
52
+ j = ks[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];
96
+ for (var x of xs)
97
+ a *= x;
70
98
  return a;
71
99
  }
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];
79
- return a;
80
- }
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);
134
+ function modes(...xs) {
135
+ xs.sort((a, b) => a - b);
136
+ var r = maxRepeat(xs);
137
+ return getRepeats(xs, r);
137
138
  }
138
- function range(...n) {
139
- return Math.max(...n) - Math.min(...n);
139
+ function range(...xs) {
140
+ return Math.max(...xs) - Math.min(...xs);
140
141
  }
141
- function rem(x, y) {
142
- return x % y;
143
- }
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(...ks) {
45
+ var n = sum(...ks), a = 1;
46
+ for (var i = 0, j = 0, I = ks.length; i < I;) {
47
+ if (j <= 0)
48
+ j = ks[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];
92
+ for (var x of xs)
93
+ a *= x;
68
94
  return a;
69
95
  }
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];
77
- return a;
78
- }
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);
130
+ function modes(...xs) {
131
+ xs.sort((a, b) => a - b);
132
+ var r = maxRepeat(xs);
133
+ return getRepeats(xs, r);
135
134
  }
136
- function range(...n) {
137
- return Math.max(...n) - Math.min(...n);
135
+ function range(...xs) {
136
+ return Math.max(...xs) - Math.min(...xs);
138
137
  }
139
- function rem(x, y) {
140
- return x % y;
141
- }
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.18",
3
+ "version": "1.2.5",
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.30"
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);