@thi.ng/math 5.7.6 → 5.7.8
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/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/abs.js +6 -2
- package/angle.js +54 -122
- package/api.js +42 -20
- package/crossing.js +8 -58
- package/eqdelta.js +6 -22
- package/extrema.js +27 -69
- package/fit.js +14 -57
- package/int.js +128 -64
- package/interval.js +73 -205
- package/libc.js +38 -107
- package/min-error.js +27 -39
- package/mix.js +109 -391
- package/package.json +8 -5
- package/permutations.js +18 -47
- package/prec.js +18 -31
- package/prime.js +31 -48
- package/ratio.js +18 -16
- package/safe-div.js +4 -7
- package/solve.js +37 -92
- package/step.js +14 -63
package/min-error.js
CHANGED
|
@@ -1,42 +1,30 @@
|
|
|
1
1
|
import { EPS } from "./api.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
*
|
|
17
|
-
* @param fn - function to evaluate
|
|
18
|
-
* @param error - error function
|
|
19
|
-
* @param q - target value
|
|
20
|
-
* @param res - number of samples per interval
|
|
21
|
-
* @param iter - max number of iterations / recursion limit
|
|
22
|
-
* @param start - interval start
|
|
23
|
-
* @param end - interval end
|
|
24
|
-
*/
|
|
25
|
-
export const minError = (fn, error, q, res = 16, iter = 8, start = 0, end = 1, eps = EPS) => {
|
|
26
|
-
if (iter <= 0)
|
|
27
|
-
return (start + end) / 2;
|
|
28
|
-
const delta = (end - start) / res;
|
|
29
|
-
let minT = start;
|
|
30
|
-
let minE = Infinity;
|
|
31
|
-
for (let i = 0; i <= res; i++) {
|
|
32
|
-
const t = start + i * delta;
|
|
33
|
-
const e = error(q, fn(t));
|
|
34
|
-
if (e < minE) {
|
|
35
|
-
if (e <= eps)
|
|
36
|
-
return t;
|
|
37
|
-
minE = e;
|
|
38
|
-
minT = t;
|
|
39
|
-
}
|
|
2
|
+
const minError = (fn, error, q, res = 16, iter = 8, start = 0, end = 1, eps = EPS) => {
|
|
3
|
+
if (iter <= 0)
|
|
4
|
+
return (start + end) / 2;
|
|
5
|
+
const delta = (end - start) / res;
|
|
6
|
+
let minT = start;
|
|
7
|
+
let minE = Infinity;
|
|
8
|
+
for (let i = 0; i <= res; i++) {
|
|
9
|
+
const t = start + i * delta;
|
|
10
|
+
const e = error(q, fn(t));
|
|
11
|
+
if (e < minE) {
|
|
12
|
+
if (e <= eps)
|
|
13
|
+
return t;
|
|
14
|
+
minE = e;
|
|
15
|
+
minT = t;
|
|
40
16
|
}
|
|
41
|
-
|
|
17
|
+
}
|
|
18
|
+
return minError(
|
|
19
|
+
fn,
|
|
20
|
+
error,
|
|
21
|
+
q,
|
|
22
|
+
res,
|
|
23
|
+
iter - 1,
|
|
24
|
+
Math.max(minT - delta, 0),
|
|
25
|
+
Math.min(minT + delta, 1)
|
|
26
|
+
);
|
|
27
|
+
};
|
|
28
|
+
export {
|
|
29
|
+
minError
|
|
42
30
|
};
|
package/mix.js
CHANGED
|
@@ -1,403 +1,121 @@
|
|
|
1
1
|
import { EPS, HALF_PI, PI } from "./api.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
* @param t - interpolation factor [0..1]
|
|
8
|
-
*/
|
|
9
|
-
export const mix = (a, b, t) => a + (b - a) * t;
|
|
10
|
-
/**
|
|
11
|
-
* Bilinear interpolation of given values (`a`,`b`,`c`,`d`).
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* c d
|
|
16
|
-
* +----+
|
|
17
|
-
* | |
|
|
18
|
-
* +----+
|
|
19
|
-
* a b
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* @param a - BL value
|
|
23
|
-
* @param b - BR value
|
|
24
|
-
* @param c - TL value
|
|
25
|
-
* @param d - TR value
|
|
26
|
-
* @param u - 1st interpolation factor
|
|
27
|
-
* @param v - 2nd interpolation factor
|
|
28
|
-
*/
|
|
29
|
-
export const mixBilinear = (a, b, c, d, u, v) => {
|
|
30
|
-
const iu = 1 - u;
|
|
31
|
-
const iv = 1 - v;
|
|
32
|
-
return a * iu * iv + b * u * iv + c * iu * v + d * u * v;
|
|
2
|
+
const mix = (a, b, t) => a + (b - a) * t;
|
|
3
|
+
const mixBilinear = (a, b, c, d, u, v) => {
|
|
4
|
+
const iu = 1 - u;
|
|
5
|
+
const iv = 1 - v;
|
|
6
|
+
return a * iu * iv + b * u * iv + c * iu * v + d * u * v;
|
|
33
7
|
};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
*
|
|
37
|
-
* @param a
|
|
38
|
-
* @param b
|
|
39
|
-
* @param c
|
|
40
|
-
* @param t
|
|
41
|
-
*/
|
|
42
|
-
export const mixQuadratic = (a, b, c, t) => {
|
|
43
|
-
const s = 1 - t;
|
|
44
|
-
return a * s * s + b * 2 * s * t + c * t * t;
|
|
8
|
+
const mixQuadratic = (a, b, c, t) => {
|
|
9
|
+
const s = 1 - t;
|
|
10
|
+
return a * s * s + b * 2 * s * t + c * t * t;
|
|
45
11
|
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* @param c
|
|
52
|
-
* @param d
|
|
53
|
-
* @param t
|
|
54
|
-
*/
|
|
55
|
-
export const mixCubic = (a, b, c, d, t) => {
|
|
56
|
-
const t2 = t * t;
|
|
57
|
-
const s = 1 - t;
|
|
58
|
-
const s2 = s * s;
|
|
59
|
-
return a * s2 * s + b * 3 * s2 * t + c * 3 * t2 * s + d * t2 * t;
|
|
12
|
+
const mixCubic = (a, b, c, d, t) => {
|
|
13
|
+
const t2 = t * t;
|
|
14
|
+
const s = 1 - t;
|
|
15
|
+
const s2 = s * s;
|
|
16
|
+
return a * s2 * s + b * 3 * s2 * t + c * 3 * t2 * s + d * t2 * t;
|
|
60
17
|
};
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
*
|
|
65
|
-
* that of `b` and `c`.
|
|
66
|
-
*
|
|
67
|
-
* Assumes all inputs are uniformly spaced. If that's not the case, use
|
|
68
|
-
* {@link mixCubicHermite} with one of the tangent generators supporting
|
|
69
|
-
* non-uniform spacing of points.
|
|
70
|
-
*
|
|
71
|
-
* See: https://www.desmos.com/calculator/j4gf8g9vkr
|
|
72
|
-
*
|
|
73
|
-
* Source:
|
|
74
|
-
* https://www.musicdsp.org/en/latest/Other/93-hermite-interpollation.html
|
|
75
|
-
*
|
|
76
|
-
* - {@link mixCubicHermite}
|
|
77
|
-
* - {@link tangentCardinal}
|
|
78
|
-
* - {@link tangentDiff3}
|
|
79
|
-
*
|
|
80
|
-
* @param a -
|
|
81
|
-
* @param b -
|
|
82
|
-
* @param c -
|
|
83
|
-
* @param d -
|
|
84
|
-
* @param t -
|
|
85
|
-
*/
|
|
86
|
-
export const mixHermite = (a, b, c, d, t) => {
|
|
87
|
-
const y1 = 0.5 * (c - a);
|
|
88
|
-
const y2 = 1.5 * (b - c) + 0.5 * (d - a);
|
|
89
|
-
return ((y2 * t + a - b + y1 - y2) * t + y1) * t + b;
|
|
18
|
+
const mixHermite = (a, b, c, d, t) => {
|
|
19
|
+
const y1 = 0.5 * (c - a);
|
|
20
|
+
const y2 = 1.5 * (b - c) + 0.5 * (d - a);
|
|
21
|
+
return ((y2 * t + a - b + y1 - y2) * t + y1) * t + b;
|
|
90
22
|
};
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* -
|
|
98
|
-
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
* @param a -
|
|
102
|
-
* @param ta -
|
|
103
|
-
* @param b -
|
|
104
|
-
* @param tb -
|
|
105
|
-
* @param t -
|
|
106
|
-
*/
|
|
107
|
-
export const mixCubicHermite = (a, ta, b, tb, t) => {
|
|
108
|
-
const s = t - 1;
|
|
109
|
-
const t2 = t * t;
|
|
110
|
-
const s2 = s * s;
|
|
111
|
-
const h00 = (1 + 2 * t) * s2;
|
|
112
|
-
const h10 = t * s2;
|
|
113
|
-
const h01 = t2 * (3 - 2 * t);
|
|
114
|
-
const h11 = t2 * s;
|
|
115
|
-
return h00 * a + h10 * ta + h01 * b + h11 * tb;
|
|
23
|
+
const mixCubicHermite = (a, ta, b, tb, t) => {
|
|
24
|
+
const s = t - 1;
|
|
25
|
+
const t2 = t * t;
|
|
26
|
+
const s2 = s * s;
|
|
27
|
+
const h00 = (1 + 2 * t) * s2;
|
|
28
|
+
const h10 = t * s2;
|
|
29
|
+
const h01 = t2 * (3 - 2 * t);
|
|
30
|
+
const h11 = t2 * s;
|
|
31
|
+
return h00 * a + h10 * ta + h01 * b + h11 * tb;
|
|
116
32
|
};
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
*
|
|
125
|
-
* @param d -
|
|
126
|
-
* @param t -
|
|
127
|
-
*/
|
|
128
|
-
export const mixCubicHermiteFromPoints = (a, b, c, d, t) => {
|
|
129
|
-
d *= 0.5;
|
|
130
|
-
const aa = -0.5 * a + 1.5 * b - 1.5 * c + d;
|
|
131
|
-
const bb = a - 2.5 * b + 2 * c - d;
|
|
132
|
-
const cc = -0.5 * a + 0.5 * c;
|
|
133
|
-
const dd = b;
|
|
134
|
-
const t2 = t * t;
|
|
135
|
-
return t * t2 * aa + t2 * bb + t * cc + dd;
|
|
33
|
+
const mixCubicHermiteFromPoints = (a, b, c, d, t) => {
|
|
34
|
+
d *= 0.5;
|
|
35
|
+
const aa = -0.5 * a + 1.5 * b - 1.5 * c + d;
|
|
36
|
+
const bb = a - 2.5 * b + 2 * c - d;
|
|
37
|
+
const cc = -0.5 * a + 0.5 * c;
|
|
38
|
+
const dd = b;
|
|
39
|
+
const t2 = t * t;
|
|
40
|
+
return t * t2 * aa + t2 * bb + t * cc + dd;
|
|
136
41
|
};
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
*
|
|
145
|
-
*
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
* @param s11 -
|
|
151
|
-
* @param s12 -
|
|
152
|
-
* @param s13 -
|
|
153
|
-
* @param s20 -
|
|
154
|
-
* @param s21 -
|
|
155
|
-
* @param s22 -
|
|
156
|
-
* @param s23 -
|
|
157
|
-
* @param s30 -
|
|
158
|
-
* @param s31 -
|
|
159
|
-
* @param s32 -
|
|
160
|
-
* @param s33 -
|
|
161
|
-
* @param u -
|
|
162
|
-
* @param v -
|
|
163
|
-
*/
|
|
164
|
-
export const mixBicubic = (s00, s01, s02, s03, s10, s11, s12, s13, s20, s21, s22, s23, s30, s31, s32, s33, u, v) => mixCubicHermiteFromPoints(mixCubicHermiteFromPoints(s00, s01, s02, s03, u), mixCubicHermiteFromPoints(s10, s11, s12, s13, u), mixCubicHermiteFromPoints(s20, s21, s22, s23, u), mixCubicHermiteFromPoints(s30, s31, s32, s33, u), v);
|
|
165
|
-
/**
|
|
166
|
-
* Helper function for {@link mixCubicHermite}. Computes cardinal tangents based
|
|
167
|
-
* on point neighbors of a point B (not given), i.e. `a` (predecessor) and `c`
|
|
168
|
-
* (successor) and their times (defaults to uniformly spaced). The optional
|
|
169
|
-
* `tension` parameter can be used to scale the tangent where 0.0 produces a
|
|
170
|
-
* Cardinal spline tangent and 1.0 a Catmull-Rom (opposite to the Wikipedia
|
|
171
|
-
* ref).
|
|
172
|
-
*
|
|
173
|
-
* https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
|
|
174
|
-
*
|
|
175
|
-
* @param prev -
|
|
176
|
-
* @param next -
|
|
177
|
-
* @param scale -
|
|
178
|
-
* @param ta -
|
|
179
|
-
* @param tc -
|
|
180
|
-
*/
|
|
181
|
-
export const tangentCardinal = (prev, next, scale = 0.5, ta = 0, tc = 2) => scale * ((next - prev) / (tc - ta));
|
|
182
|
-
/**
|
|
183
|
-
* Helper function for {@link mixCubicHermite}. Computes tangent for `curr`,
|
|
184
|
-
* based on 3-point finite difference, where `prev` & `next` are `curr`'s
|
|
185
|
-
* neighbors and the `tX` the three points' respective time values. The latter
|
|
186
|
-
* are equally spaced by default (each 1.0 apart).
|
|
187
|
-
*
|
|
188
|
-
* Using this function with equal spacing of 1.0 and together with
|
|
189
|
-
* {@link mixCubicHermite} will produce same results as the somewhat optimized
|
|
190
|
-
* variant {@link mixHermite}.
|
|
191
|
-
*
|
|
192
|
-
* https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Finite_difference
|
|
193
|
-
*
|
|
194
|
-
* @param prev -
|
|
195
|
-
* @param curr -
|
|
196
|
-
* @param next -
|
|
197
|
-
* @param ta -
|
|
198
|
-
* @param tb -
|
|
199
|
-
* @param tc -
|
|
200
|
-
*/
|
|
201
|
-
export const tangentDiff3 = (prev, curr, next, ta = 0, tb = 1, tc = 2) => 0.5 * ((next - curr) / (tc - tb) + (curr - prev) / (tb - ta));
|
|
202
|
-
/**
|
|
203
|
-
* HOF interpolator. Takes a timing function `f` and interval `[from,to]`.
|
|
204
|
-
* Returns function which takes normalized time (in [0,1] range) as single arg
|
|
205
|
-
* and returns interpolated value.
|
|
206
|
-
*
|
|
207
|
-
* @param f -
|
|
208
|
-
* @param from -
|
|
209
|
-
* @param to -
|
|
210
|
-
*/
|
|
211
|
-
export const tween = (f, from, to) => (t) => mix(from, to, f(t));
|
|
212
|
-
/**
|
|
213
|
-
* Circular interpolation (ease out): `sqrt(1 - (1 - t)^2)`
|
|
214
|
-
*
|
|
215
|
-
* @remarks
|
|
216
|
-
* Reference: https://www.desmos.com/calculator/tisoiazdrw
|
|
217
|
-
*
|
|
218
|
-
* @param t - interpolation factor [0..1]
|
|
219
|
-
*/
|
|
220
|
-
export const circular = (t) => {
|
|
221
|
-
t = 1 - t;
|
|
222
|
-
return Math.sqrt(1 - t * t);
|
|
42
|
+
const mixBicubic = (s00, s01, s02, s03, s10, s11, s12, s13, s20, s21, s22, s23, s30, s31, s32, s33, u, v) => mixCubicHermiteFromPoints(
|
|
43
|
+
mixCubicHermiteFromPoints(s00, s01, s02, s03, u),
|
|
44
|
+
mixCubicHermiteFromPoints(s10, s11, s12, s13, u),
|
|
45
|
+
mixCubicHermiteFromPoints(s20, s21, s22, s23, u),
|
|
46
|
+
mixCubicHermiteFromPoints(s30, s31, s32, s33, u),
|
|
47
|
+
v
|
|
48
|
+
);
|
|
49
|
+
const tangentCardinal = (prev, next, scale = 0.5, ta = 0, tc = 2) => scale * ((next - prev) / (tc - ta));
|
|
50
|
+
const tangentDiff3 = (prev, curr, next, ta = 0, tb = 1, tc = 2) => 0.5 * ((next - curr) / (tc - tb) + (curr - prev) / (tb - ta));
|
|
51
|
+
const tween = (f, from, to) => (t) => mix(from, to, f(t));
|
|
52
|
+
const circular = (t) => {
|
|
53
|
+
t = 1 - t;
|
|
54
|
+
return Math.sqrt(1 - t * t);
|
|
223
55
|
};
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
*
|
|
229
|
-
|
|
230
|
-
* @param t - interpolation factor [0..1]
|
|
231
|
-
*/
|
|
232
|
-
export const invCircular = (t) => 1 - circular(1 - t);
|
|
233
|
-
/**
|
|
234
|
-
* Zoomlens interpolation with customizable lens position, behavior and
|
|
235
|
-
* strength.
|
|
236
|
-
*
|
|
237
|
-
* @remarks
|
|
238
|
-
* Lens position must be given in (0..1) interval. Lens strength must be in
|
|
239
|
-
* [-1,1] range. If negative, the lens will be bundling values near `pos`, if
|
|
240
|
-
* positive the lens has dilating characteristics and will spread values near
|
|
241
|
-
* `pos` towards the edges.
|
|
242
|
-
*
|
|
243
|
-
* Also see {@link schlick} for an alternative approach.
|
|
244
|
-
*
|
|
245
|
-
* @example
|
|
246
|
-
* ```ts
|
|
247
|
-
* // interpolated position in [100..400] interval for given `t`
|
|
248
|
-
* y = mix(100, 400, lens(0.5, 1, t));
|
|
249
|
-
*
|
|
250
|
-
* // or build tween function via `tween()`
|
|
251
|
-
* f = tween(partial(lens, 0.5, 1), 100, 400);
|
|
252
|
-
*
|
|
253
|
-
* f(t)
|
|
254
|
-
* ```
|
|
255
|
-
*
|
|
256
|
-
* @param pos - lens pos
|
|
257
|
-
* @param strength - lens strength
|
|
258
|
-
* @param t - interpolation factor [0..1]
|
|
259
|
-
*/
|
|
260
|
-
export const lens = (pos, strength, t) => {
|
|
261
|
-
const impl = strength > 0 ? invCircular : circular;
|
|
262
|
-
const tp = 1 - pos;
|
|
263
|
-
const tl = t <= pos ? impl(t / pos) * pos : 1 - impl((1 - t) / tp) * tp;
|
|
264
|
-
return mix(t, tl, Math.abs(strength));
|
|
56
|
+
const invCircular = (t) => 1 - circular(1 - t);
|
|
57
|
+
const lens = (pos, strength, t) => {
|
|
58
|
+
const impl = strength > 0 ? invCircular : circular;
|
|
59
|
+
const tp = 1 - pos;
|
|
60
|
+
const tl = t <= pos ? impl(t / pos) * pos : 1 - impl((1 - t) / tp) * tp;
|
|
61
|
+
return mix(t, tl, Math.abs(strength));
|
|
265
62
|
};
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
*
|
|
271
|
-
* @remarks
|
|
272
|
-
* Interactive graph:
|
|
273
|
-
* https://www.desmos.com/calculator/tywbpw8pck
|
|
274
|
-
*
|
|
275
|
-
* @param k
|
|
276
|
-
* @param amp
|
|
277
|
-
* @param t
|
|
278
|
-
*/
|
|
279
|
-
export const bounce = (k, amp, t) => {
|
|
280
|
-
const tk = t * k;
|
|
281
|
-
return 1 - ((amp * Math.sin(tk)) / tk) * Math.cos(t * HALF_PI);
|
|
63
|
+
const cosine = (t) => 1 - (Math.cos(t * PI) * 0.5 + 0.5);
|
|
64
|
+
const decimated = (n, t) => Math.floor(t * n) / n;
|
|
65
|
+
const bounce = (k, amp, t) => {
|
|
66
|
+
const tk = t * k;
|
|
67
|
+
return 1 - amp * Math.sin(tk) / tk * Math.cos(t * HALF_PI);
|
|
282
68
|
};
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
*
|
|
286
|
-
*
|
|
287
|
-
* - `ease > 1` -> ease in
|
|
288
|
-
* - `ease < 1` -> ease out
|
|
289
|
-
*
|
|
290
|
-
* @param ease - easing behavior [0.0 .. ∞]
|
|
291
|
-
* @param t -
|
|
292
|
-
*/
|
|
293
|
-
export const ease = (ease, t) => Math.pow(t, ease);
|
|
294
|
-
/**
|
|
295
|
-
* Impulse generator. Peaks at `t = 1/k`
|
|
296
|
-
*
|
|
297
|
-
* @param k - impulse width (higher values => shorter impulse)
|
|
298
|
-
*/
|
|
299
|
-
export const impulse = (k, t) => {
|
|
300
|
-
const h = k * t;
|
|
301
|
-
return h * Math.exp(1 - h);
|
|
69
|
+
const ease = (ease2, t) => Math.pow(t, ease2);
|
|
70
|
+
const impulse = (k, t) => {
|
|
71
|
+
const h = k * t;
|
|
72
|
+
return h * Math.exp(1 - h);
|
|
302
73
|
};
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
74
|
+
const gain = (k, t) => t < 0.5 ? 0.5 * Math.pow(2 * t, k) : 1 - 0.5 * Math.pow(2 - 2 * t, k);
|
|
75
|
+
const parabola = (k, t) => Math.pow(4 * t * (1 - t), k);
|
|
76
|
+
const cubicPulse = (w, c, t) => {
|
|
77
|
+
t = Math.abs(t - c);
|
|
78
|
+
return t > w ? 0 : (t /= w, 1 - t * t * (3 - 2 * t));
|
|
79
|
+
};
|
|
80
|
+
const sinc = (t) => t !== 0 ? Math.sin(t) / t : 1;
|
|
81
|
+
const sincNormalized = (k, t) => sinc(PI * k * t);
|
|
82
|
+
const lanczos = (a, t) => t !== 0 ? -a < t && t < a ? sinc(PI * t) * sinc(PI * t / a) : 0 : 1;
|
|
83
|
+
const sigmoid = (bias, k, t) => t != bias ? 1 / (1 + Math.exp(-k * (t - bias))) : 0.5;
|
|
84
|
+
const sigmoid01 = (k, t) => sigmoid(0.5, k, t);
|
|
85
|
+
const sigmoid11 = (k, t) => sigmoid(0, k, t);
|
|
86
|
+
const schlick = (a, b, t) => t <= b ? b * t / (t + a * (b - t) + EPS) : (1 - b) * (t - 1) / (1 - t - a * (b - t) + EPS) + 1;
|
|
87
|
+
const expFactor = (a, b, num) => (b / a) ** (1 / num);
|
|
88
|
+
const gaussian = (bias, sigma, t) => Math.exp(-((t - bias) ** 2) / (2 * sigma * sigma));
|
|
89
|
+
export {
|
|
90
|
+
bounce,
|
|
91
|
+
circular,
|
|
92
|
+
cosine,
|
|
93
|
+
cubicPulse,
|
|
94
|
+
decimated,
|
|
95
|
+
ease,
|
|
96
|
+
expFactor,
|
|
97
|
+
gain,
|
|
98
|
+
gaussian,
|
|
99
|
+
impulse,
|
|
100
|
+
invCircular,
|
|
101
|
+
lanczos,
|
|
102
|
+
lens,
|
|
103
|
+
mix,
|
|
104
|
+
mixBicubic,
|
|
105
|
+
mixBilinear,
|
|
106
|
+
mixCubic,
|
|
107
|
+
mixCubicHermite,
|
|
108
|
+
mixCubicHermiteFromPoints,
|
|
109
|
+
mixHermite,
|
|
110
|
+
mixQuadratic,
|
|
111
|
+
parabola,
|
|
112
|
+
schlick,
|
|
113
|
+
sigmoid,
|
|
114
|
+
sigmoid01,
|
|
115
|
+
sigmoid11,
|
|
116
|
+
sinc,
|
|
117
|
+
sincNormalized,
|
|
118
|
+
tangentCardinal,
|
|
119
|
+
tangentDiff3,
|
|
120
|
+
tween
|
|
308
121
|
};
|
|
309
|
-
/**
|
|
310
|
-
* Unnormalized Sinc function: sin(x)/x. Returns 1 for t=0.
|
|
311
|
-
*
|
|
312
|
-
* @remarks
|
|
313
|
-
* https://en.wikipedia.org/wiki/Sinc_function
|
|
314
|
-
*
|
|
315
|
-
* @param k -
|
|
316
|
-
* @param t -
|
|
317
|
-
*/
|
|
318
|
-
export const sinc = (t) => (t !== 0 ? Math.sin(t) / t : 1);
|
|
319
|
-
/**
|
|
320
|
-
* Normalized Sinc function, returns sinc(π*k*t).
|
|
321
|
-
*
|
|
322
|
-
* @remarks
|
|
323
|
-
* https://en.wikipedia.org/wiki/Sinc_function
|
|
324
|
-
*
|
|
325
|
-
* @see {@link sinc}
|
|
326
|
-
*
|
|
327
|
-
* @param k -
|
|
328
|
-
* @param t -
|
|
329
|
-
*/
|
|
330
|
-
export const sincNormalized = (k, t) => sinc(PI * k * t);
|
|
331
|
-
/**
|
|
332
|
-
* Lanczos filter. Returns `sinc(πt)sinc(πt/a)` iff `t` in (-a,a) interval, else
|
|
333
|
-
* returns 0.
|
|
334
|
-
*
|
|
335
|
-
* @remarks
|
|
336
|
-
* Interactive graph: https://www.desmos.com/calculator/pmypqgefle
|
|
337
|
-
*
|
|
338
|
-
* @param a -
|
|
339
|
-
* @param t -
|
|
340
|
-
*/
|
|
341
|
-
export const lanczos = (a, t) => t !== 0 ? (-a < t && t < a ? sinc(PI * t) * sinc((PI * t) / a) : 0) : 1;
|
|
342
|
-
/**
|
|
343
|
-
* Sigmoid function for inputs arounds center bias.
|
|
344
|
-
*
|
|
345
|
-
* @remarks
|
|
346
|
-
* Updated in v3.0.0 to add bias value to satisfy more use cases. Use
|
|
347
|
-
* {@link sigmoid01} for old behavior.
|
|
348
|
-
*
|
|
349
|
-
* @param bias - center value (for which result = 0.5)
|
|
350
|
-
* @param k - steepness
|
|
351
|
-
* @param t - input value
|
|
352
|
-
*/
|
|
353
|
-
export const sigmoid = (bias, k, t) => t != bias ? 1 / (1 + Math.exp(-k * (t - bias))) : 0.5;
|
|
354
|
-
/**
|
|
355
|
-
* Sigmoid function for inputs in [0..1] interval. Center bias = 0.5.
|
|
356
|
-
*
|
|
357
|
-
* @param k - steepness
|
|
358
|
-
* @param t - input value
|
|
359
|
-
*/
|
|
360
|
-
export const sigmoid01 = (k, t) => sigmoid(0.5, k, t);
|
|
361
|
-
/**
|
|
362
|
-
* Sigmoid function for inputs in [-1..+1] interval. Center bias = 0
|
|
363
|
-
*
|
|
364
|
-
* @param k -
|
|
365
|
-
* @param t -
|
|
366
|
-
*/
|
|
367
|
-
export const sigmoid11 = (k, t) => sigmoid(0, k, t);
|
|
368
|
-
/**
|
|
369
|
-
* Generalized Schlick bias gain curve, based on:
|
|
370
|
-
* https://arxiv.org/abs/2010.09714
|
|
371
|
-
*
|
|
372
|
-
* @remarks
|
|
373
|
-
* Interactive graph:
|
|
374
|
-
* https://www.desmos.com/calculator/u6bkm5rb7t
|
|
375
|
-
*
|
|
376
|
-
* @param a - curve strength. recommended (0..64]
|
|
377
|
-
* @param b - pivot position [0..1]
|
|
378
|
-
* @param t - input val [0..1]
|
|
379
|
-
*/
|
|
380
|
-
export const schlick = (a, b, t) => t <= b
|
|
381
|
-
? (b * t) / (t + a * (b - t) + EPS)
|
|
382
|
-
: ((1 - b) * (t - 1)) / (1 - t - a * (b - t) + EPS) + 1;
|
|
383
|
-
/**
|
|
384
|
-
* Computes exponential factor to interpolate from `a` to `b` over
|
|
385
|
-
* `num` steps. I.e. multiplying `a` with the returned factor will yield
|
|
386
|
-
* `b` after `num` steps. All args must be > 0.
|
|
387
|
-
*
|
|
388
|
-
* @param a -
|
|
389
|
-
* @param b -
|
|
390
|
-
* @param num -
|
|
391
|
-
*/
|
|
392
|
-
export const expFactor = (a, b, num) => (b / a) ** (1 / num);
|
|
393
|
-
/**
|
|
394
|
-
* Computes gaussian bell curve for given center `bias` and `sigma` (spread).
|
|
395
|
-
*
|
|
396
|
-
* @remarks
|
|
397
|
-
* Interactive graph: https://www.desmos.com/calculator/aq6hdzxprv
|
|
398
|
-
*
|
|
399
|
-
* @param bias -
|
|
400
|
-
* @param sigma -
|
|
401
|
-
* @param t -
|
|
402
|
-
*/
|
|
403
|
-
export const gaussian = (bias, sigma, t) => Math.exp(-((t - bias) ** 2) / (2 * sigma * sigma));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/math",
|
|
3
|
-
"version": "5.7.
|
|
3
|
+
"version": "5.7.8",
|
|
4
4
|
"description": "Assorted common math functions & utilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -27,7 +27,9 @@
|
|
|
27
27
|
],
|
|
28
28
|
"license": "Apache-2.0",
|
|
29
29
|
"scripts": {
|
|
30
|
-
"build": "yarn
|
|
30
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
31
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
32
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
31
33
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
32
34
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
33
35
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -36,10 +38,11 @@
|
|
|
36
38
|
"test": "bun test"
|
|
37
39
|
},
|
|
38
40
|
"dependencies": {
|
|
39
|
-
"@thi.ng/api": "^8.9.
|
|
41
|
+
"@thi.ng/api": "^8.9.13"
|
|
40
42
|
},
|
|
41
43
|
"devDependencies": {
|
|
42
44
|
"@microsoft/api-extractor": "^7.38.3",
|
|
45
|
+
"esbuild": "^0.19.8",
|
|
43
46
|
"rimraf": "^5.0.5",
|
|
44
47
|
"tools": "^0.0.1",
|
|
45
48
|
"typedoc": "^0.25.4",
|
|
@@ -65,7 +68,7 @@
|
|
|
65
68
|
"access": "public"
|
|
66
69
|
},
|
|
67
70
|
"engines": {
|
|
68
|
-
"node": ">=
|
|
71
|
+
"node": ">=18"
|
|
69
72
|
},
|
|
70
73
|
"files": [
|
|
71
74
|
"./*.js",
|
|
@@ -136,5 +139,5 @@
|
|
|
136
139
|
"thi.ng": {
|
|
137
140
|
"year": 2013
|
|
138
141
|
},
|
|
139
|
-
"gitHead": "
|
|
142
|
+
"gitHead": "25a42a81fac8603a1e440a7aa8bc343276211ff4\n"
|
|
140
143
|
}
|