thursday/thursday/utilities/math.py

28 lines
560 B
Python

import math
feps = 2.0**-23.0
tiniest = 2.0**-1022.0
def scalar_softplus(x):
if x >= 33.276435657655455:
return float(x)
elif x <= -745.13330078125:
return 0.0
else:
return math.log1p(math.exp(x))
def phi(d):
# phi(1) = golden ratio
# phi(2) = plastic constant
# phi(3) = the positive real root of x**4-x-1
x = 2.0
for i in range(30 if d == 1 else max(10, 28 - d)):
x = pow(1 + x, 1 / (d + 1))
return x
def fib(n):
return pow(2 << n, n + 1, (4 << 2 * n) - (2 << n) - 1) % (2 << n)