-
Notifications
You must be signed in to change notification settings - Fork 0
/
nestable_forward_mode.py
117 lines (93 loc) · 3.75 KB
/
nestable_forward_mode.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import math
class bundle:
def __init__(self, e, prim, tg):
self.e = e
self.prim = prim
self.tg = tg
def __pos__(self): return self
def __neg__(self): return 0-self
def __add__(self, y): return plus(self, y)
def __radd__(self, x): return plus(x, self)
def __sub__(self, y): return minus(self, y)
def __rsub__(self, x): return minus(x, self)
def __mul__(self, y): return times(self, y)
def __rmul__(self, x): return times(x, self)
def __div__(self, y): return divide(self, y)
def __rdiv__(self, x): return divide(x, self)
def __truediv__(self, y): return divide(self, y)
def __rtruediv__(self, x): return divide(x, self)
def __lt__(self, x): return lt(self, x)
def __le__(self, x): return le(self, x)
def __gt__(self, x): return gt(self, x)
def __ge__(self, x): return ge(self, x)
def __eq__(self, x): return eq(self, x)
def __ne__(self, x): return ne(self, x)
epsilon = 0
def generate_epsilon():
global epsilon
epsilon += 1
return epsilon
def bun(e, x, x_tangent): return bundle(e, x, x_tangent)
def prim(e, x):
if isinstance(x, bundle):
if x.e==e: return x.prim
else: return bun(x.e, prim(e, x.prim), prim(e, x.tg))
else: return x
def tg(e, x):
if isinstance(x, bundle):
if x.e==e: return x.tg
else: return bun(x.e, tg(e, x.prim), tg(e, x.tg))
else: return 0
def lift_real_to_real(f, dfdx):
def me(x):
if isinstance(x, bundle):
e = x.e
return bun(e, me(prim(e, x)), dfdx(prim(e, x))*tg(e, x))
else: return f(x)
return me
def lift_real_cross_real_to_real(f, dfdx1, dfdx2):
def me(x1, x2):
if isinstance(x1, bundle) or isinstance(x2, bundle):
e = x1.e if isinstance(x1, bundle) else x2.e
return bun(e,
me(prim(e, x1), prim(e, x2)),
(dfdx1(prim(e, x1), prim(e, x2))*tg(e, x1)
+dfdx2(prim(e, x1), prim(e, x2))*tg(e, x2)))
else: return f(x1, x2)
return me
def lift_real_cross_real_to_boolean(f):
def me(x1, x2):
if isinstance(x1, bundle): return me(prim(x1.e, x1), x2)
elif isinstance(x2, bundle): return me(x1, prim(x2.e, x2))
else: return f(x1, x2)
return me
plus = lift_real_cross_real_to_real(lambda x1, x2: x1+x2,
lambda x1, x2: 1,
lambda x1, x2: 1)
minus = lift_real_cross_real_to_real(lambda x1, x2: x1-x2,
lambda x1, x2: 1,
lambda x1, x2: -1)
times = lift_real_cross_real_to_real(lambda x1, x2: x1*x2,
lambda x1, x2: x2,
lambda x1, x2: x1)
divide = lift_real_cross_real_to_real(lambda x1, x2: x1/x2,
lambda x1, x2: 1/x2,
lambda x1, x2: -x1/(x2*x2))
lt = lift_real_cross_real_to_boolean(lambda x1, x2: x1<x2)
le = lift_real_cross_real_to_boolean(lambda x1, x2: x1<=x2)
gt = lift_real_cross_real_to_boolean(lambda x1, x2: x1>x2)
ge = lift_real_cross_real_to_boolean(lambda x1, x2: x1>=x2)
eq = lift_real_cross_real_to_boolean(lambda x1, x2: x1==x2)
ne = lift_real_cross_real_to_boolean(lambda x1, x2: x1!=x2)
exp = lift_real_to_real(math.exp, lambda x: exp(x))
def derivative(f):
def me(x):
e = generate_epsilon()
return tg(e, f(bun(e, x, 1)))
return me
def replace_ith(x, i, xi):
return [xi if j==i else x[j] for j in range(len(x))]
def partial_derivative(f, i):
return lambda x: derivative(lambda xi: f(replace_ith(x, i, xi)))(x[i])
def gradient(f):
return lambda x: [partial_derivative(f, i)(x) for i in range(len(x))]