-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.ts
52 lines (48 loc) · 1.32 KB
/
memory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Node, Window } from './types.ts';
import { add, conv, decayingSum, div, exp, inv, log, mul, sub, toConst } from './base.ts';
import { r } from './config.ts';
export const decay = (
window: number | Window | Node,
data: Node,
backfillZeros?: boolean,
) =>
typeof window === 'number'
? decayingSum(data, r(1 / window))
: window instanceof Array
? decayingSum(data, window)
: conv(window, data, backfillZeros);
export const lse = (
window: number | Window | Node,
data: Node,
c: Node | number,
backfillZeros?: boolean,
base = r(0),
) =>
toConst(c) !== 0
? add(div(log(decay(window, exp(mul(sub(data, base), r(c))), backfillZeros)), r(c)), base)
: decay(window, data, backfillZeros);
export const persist = (
window: number | Window | Node,
trigger: Node,
value: Node,
eps = 1e-9,
backfillZeros?: boolean,
) =>
div(
decay(window, mul(trigger, value), backfillZeros),
add(r(eps), decay(window, trigger, backfillZeros)),
);
export const persistLse = (
window: number | Window | Node,
trigger: Node,
value: Node,
c: Node | number,
eps = 1e-9,
backfillZeros?: boolean,
) =>
toConst(c) !== 0
? mul(
log(persist(window, trigger, exp(mul(value, r(c))), eps, backfillZeros)),
inv(r(c)),
)
: persist(window, trigger, value, eps, backfillZeros);