-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathautodiff.py
More file actions
50 lines (43 loc) · 1.62 KB
/
autodiff.py
File metadata and controls
50 lines (43 loc) · 1.62 KB
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
"""
A basic demo of automatic differentiation. (Almost untested.)
Forward mode, and for simplicity supporting only one variable.
to do: compare https://github.com/masonium/cl-autodiff/blob/master/autodiff.lisp
"""
from __future__ import division
class DualNumber(object):
def __init__(self, v, d):
self.v = v # The value
self.d = d # The derivative
def __neg__(self):
return DualNumber(-self.v, -self.d)
def __add__(self, other):
other = promote(other)
return DualNumber(self.v + other.v, self.d + other.d)
def __sub__(self, other):
other = promote(other)
return DualNumber(self.v - other.v, self.d - other.d)
def __mul__(self, other):
other = promote(other)
return DualNumber(self.v * other.v, self.d*other.v + self.v*other.d)
def __div__(self, other):
other = promote(other)
return DualNumber(self.v / other.v,
(self.d * other.v - self.v * other.d) / (other.v * other.v))
def __radd__(self, other): return Constant(other) + self
def __rsub__(self, other): return Constant(other) - self
def __rmul__(self, other): return Constant(other) * self
def __rdiv__(self, other): return Constant(other) / self
def __repr__(self):
if self.d == 0:
return repr(self.v)
else:
return '(%r, %r)' % (self.v, self.d)
def promote(value):
return value if isinstance(value, DualNumber) else Constant(value)
def Constant(c):
return DualNumber(c, 0)
def Variable(value):
return DualNumber(value, 1)
x = Variable(2)
## x*x*x
#. (8, 12)