Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save wyojustin/ff58eeca9d37c78c74d34f2c1122f473 to your computer and use it in GitHub Desktop.

Select an option

Save wyojustin/ff58eeca9d37c78c74d34f2c1122f473 to your computer and use it in GitHub Desktop.
import math
import numpy as np
def f(t, v):
return v * (.5**t)
def g(t, v):
return v * (1 -t/1.442)
def h(t):
return .5 ** t
ln2 = 0.6931471805599453
def taylor_half_power_t5(t: float) -> float:
"""
Approximate (1/2)**t by the Taylor series for exp(-t ln 2),
truncated after the t^5 term:
(1/2)**t ≈ ∑_{k=0..5} [(-t·ln2)^k / k!]
Parameters
----------
t : float
The exponent.
Returns
-------
float
5th-order Taylor approximation to (1/2)**t.
"""
# sum terms k = 0,1,2,3,4,5
return sum(((-t * ln2) ** k) / math.factorial(k) for k in range(6))
nframes = 20
framesep = 1/nframes
ts = np.random.uniform(0, 1, nframes)
ts /= np.sum(ts)
v = 1.
for t in ts:
v = f(t, v)
print(t, v)
v = 1.
for t in ts:
v = g(t, v)
print(t, v)
v = 1.
for t in ts:
v = v * h(t)
print(t, v)
v = 1.
for t in ts:
v = v * taylor_half_power_t5(t)
print(t, v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment