-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumeric.py
51 lines (42 loc) · 958 Bytes
/
numeric.py
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
#!/usr/bin/env python3
__author__ = "John Petersen"
__version__ = "0.0.1"
__license__ = "GNU General Public License 3.0"
from math import *
def clamp(x, lo = 0, hi = 1):
lo2 = min(lo, hi)
hi2 = max(lo, hi)
return min(max(lo2, x), hi2)
def atoi(x):
try:
return int(x)
except ValueError:
return None
def atof(x):
try:
return float(x)
except ValueError:
return None
def aton(src):
x = src.lower()
x = ''.join(x.split())
f = atof(x)
if('.' in x):
return f
if('inf' in x or 'nan' in x):
return f
return atoi(x)
def progress(i, i0, i1):
assert i0 < i1
return (i - i0) / float(i1 - i0)
def logistic(x):
return 1/(1+exp(-x))
def dlogistic(x):
y = exp(-x)
return x*y/pow(1+y, 2)
def ilogistic(x):
return log1p(exp(x))
# linear at s=0
def hyperbolic(x, s = 2, m = 1.0):
y = float(clamp(x, 0, m))/m
return sqrt(y*(y+s)/(1+s))*m