Repository navigation
Expand file tree
/
Copy pathADnum_rev.py
More file actions
289 lines (270 loc) · 12.1 KB
/
Copy pathADnum_rev.py
File metadata and controls
289 lines (270 loc) · 12.1 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""Module to wrap numbers into ADnum object and do basic calculations.
Take value and specified derivative as given, wrap up as ADnum object, and return ADnum object for each basic calculation function.
"""
import numpy as np
class ADnum:
""" Class to create ADnum objects on which to perform differentiation.
ATTRIBUTES
==========
val : scalar for scalar valued quantities or numpy array for vector valued functions, the value of the ADnum object for a set input value
der : scalar for sclar functions of a single variable or numpy array for functions of multiple variables the derivative
graph : dictionary containing the edges of the computational graph
constant : 0 or 1 indicating whether the ADnum object is constant
METHODS
=======
This class overloads the methods for basic arithmetic operations.
EXAMPLES
========
# >>> x = ADnum(2, der = 1)
# >>> f = 2*x+3
# >>> print(f.val)
# 7.0
# >>> print(f.der)
# 2.0
"""
def __init__(self, value, **kwargs):
try:
scalarinput = (isinstance(value, int) or isinstance(value, float))
value = np.array(value)
value = value.astype(float)
if 'der' not in kwargs:
try:
ins = kwargs['ins']
ind = kwargs['ind']
if scalarinput:
der = np.zeros(ins)
der[ind] = 1.0
else:
if ins>1:
der = np.zeros((ins, len(value)))
der[ind, :] = 1.0 #np.ones(len(value))
else:
der = np.ones(len(value))
except:
raise KeyError('Must provide ins and ind if der not provided.')
else:
der = kwargs['der']
der = np.array(der)
der = der.astype(float)
if 'ins' in kwargs:
ins = kwargs['ins']
if len(der) != ins:
raise ValueError('Shape of derivative does not match number of inputs.')
except:
raise ValueError('Value and derivative of ADnum object must be numeric.')
self.val = value
self.der = der
self.ins = len(self.der)
self.rder = None
if 'graph' not in kwargs:
self.graph = {}
else:
self.graph = kwargs['graph']
if 'constant' not in kwargs:
self.constant = 0
else:
self.constant = kwargs['constant']
if 'ops' not in kwargs:
self.ops = 0
else:
self.ops = kwargs['ops']
if 'rops' not in kwargs:
self.rops = 0
else:
self.rops = kwargs['rops']
if 'tfops' not in kwargs:
self.tfops = 0
else:
self.tfops = kwargs['tfops']
if 'trops' not in kwargs:
self.trops = 0
else:
self.trops = kwargs['trops']
self.counted = 0
def revder(self, f):
f.rder = 1
tolops = 0
if self.rder is None:
try:
children = f.graph[self]
calc = 0
for child in children:
calc = calc + child[2]*child[0].revder(f)[0]
tolops = tolops + child[0].revder(f)[1]+child[0].rops+2*self.ins
self.rder = calc
except KeyError:
self.rder = 0
return self.rder, tolops
def __neg__(self):
y = ADnum(-self.val, der = -self.der, ops = (1-self.counted)*self.ops+1, rops = 0, tfops = self.tfops+self.ins, trops = self.trops+4*self.ins)
self.counted = 1
y.graph = self.graph
if self not in y.graph:
y.graph[self] = []
y.graph[self].append((y, 'neg', -1))
return y
def __mul__(self,other):
try:
graph = merge_dicts(self.graph, other.graph)
if self.constant or other.constant:
opcount = (1-self.counted)*self.ops*(1-self.constant)+(1-other.counted)*other.ops*(1-other.constant)+1
else:
opcount = (1-self.counted)*self.ops*(1-self.constant)+(1-other.counted)*other.ops*(1-other.constant)+3
y = ADnum(self.val*other.val, der = self.val*other.der+self.der*other.val, ops = opcount, rops=0, tfops = 3*self.ins+self.tfops+other.tfops, trops = 4*self.ins+self.trops+other.trops)
self.counted = 1
other.counted = 1
y.graph = graph
if self not in y.graph:
y.graph[self] = []
y.graph[self].append((y, 'multiply', other.val))
if other not in y.graph:
y.graph[other] = []
y.graph[other].append((y, 'multiply', self.val))
return y
except AttributeError:
other = ADnum(other*np.ones(np.shape(self.val)), der = np.zeros(np.shape(self.der)), constant = 1)
return self*other
def __rmul__(self,other):
return self.__mul__(other)
def __add__(self,other):
try:
graph = merge_dicts(self.graph, other.graph)
opcount = (1-self.counted)*self.ops*(1-self.constant)+(1-other.counted)*other.ops*(1-other.constant)+1
y = ADnum(self.val+other.val, der = self.der+other.der, ops = opcount, rops=0, tfops = self.tfops+other.tfops+self.ins, trops = self.trops + other.trops+2*self.ins)
self.counted = 1
other.counted = 1
y.graph = graph
if self not in y.graph:
y.graph[self] = []
y.graph[self].append((y, 'add', 1))
if other not in y.graph:
y.graph[other] = []
y.graph[other].append((y, 'add', 1))
return y
except AttributeError:
other = ADnum(other*np.ones(np.shape(self.val)), der = np.zeros(np.shape(self.der)), constant = 1)
return self + other
def __radd__(self,other):
return self.__add__(other)
def __sub__(self,other):
try:
graph = merge_dicts(self.graph, other.graph)
opcount = (1-self.counted)*self.ops*(1-self.constant)+(1-other.counted)*other.ops*(1-other.constant)+1
y = ADnum(self.val-other.val,der = self.der-other.der, ops = opcount, rops=0, tfops = self.tfops+other.tfops+self.ins, trops = self.trops+other.trops+2*self.ins)
self.counted = 1
other.counted = 1
y.graph = graph
if self not in y.graph:
y.graph[self] = []
y.graph[self].append((y, 'subtract', 1))
if other not in y.graph:
y.graph[other] = []
y.graph[other].append((y, 'subtract', -1))
return y
except AttributeError:
other = ADnum(other*np.ones(np.shape(self.val)), der = np.zeros(np.shape(self.der)), constant = 1)
return self-other
def __rsub__(self, other):
try:
return ADnum(other.val-self.val, der = other.der-self.der)
except AttributeError:
other = ADnum(other*np.ones(np.shape(self.val)), der = np.zeros(np.shape(self.der)), constant = 1)
return other-self
def __truediv__(self, other):
try:
graph = merge_dicts(self.graph, other.graph)
opcount = (1-self.counted)*self.ops*(1-self.constant)+(1-other.counted)*other.ops*(1-other.constant)
if self.constant and not other.constant:
opcount = opcount + 3
elif other.constant and not self.constant:
opcount = opcount + 1
else:
opcount = opcount+5
y = ADnum(self.val/other.val, der = (other.val*self.der-self.val*other.der)/(other.val**2), ops = opcount, rops = 1, tfops = self.tfops+other.tfops+5*self.ins, trops = self.trops+other.trops+4*self.ins)
self.counted = 1
other.counted = 1
y.graph = graph
if self not in y.graph:
y.graph[self] = []
y.graph[self].append((y, 'divide', 1/other.val))
if other not in y.graph:
y.graph[other] = []
y.graph[other].append((y, 'divide', -1*self.val/(other.val**2)))
return y
except AttributeError:
other = ADnum(other*np.ones(np.shape(self.val)), der = np.zeros(np.shape(self.der)), constant = 1)
return self/other
def __rtruediv__(self, other):
try:
opcount = (1-self.counted)*self.ops*(1-self.constant)+(1-other.counted)*other.ops*(1-other.constant)
if self.constant and not other.constant:
opcount = opcount + 3
elif other.constant and not self.constant:
opcount = opcount + 1
else:
opcount = opcount+5
return ADnum(other.val/self.val, der = (self.val*other.der-other.val*self.der)/(self.val**2), ops = opcount, tfops = self.tfops+other.tfops+5*self.ins, trops = self.trops+other.trops+4*self.ins)
except AttributeError:
other = ADnum(other*np.ones(np.shape(self.val)), der = np.zeros(np.shape(self.der)), constant = 1)
return other/self
def __pow__(self, other, modulo=None):
try:
graph = merge_dicts(self.graph, other.graph)
opcount = (1-self.counted)*self.ops*(1-self.constant)+(1-other.counted)*other.ops*(1-other.constant)
if self.constant and not other.constant:
opcount = opcount + 4
elif other.constant and not self.constant:
opcount = opcount + 4
else:
opcount = opcount + 10
if self.val == 0:
y = ADnum(self.val**other.val, der = other.val*(self.val**(other.val-1))*self.der+(self.val**other.val), ops = opcount, rops=3, tfops = self.tfops+other.tfops+2+self.ins, trops = self.trops+other.trops+2+2*self.ins)
self.counted = 1
other.counted =1
else:
y = ADnum(self.val**other.val, der = other.val*(self.val**(other.val-1))*self.der+(self.val**other.val)*np.log(np.abs(self.val))*other.der, ops = opcount, tfops=self.tfops+other.tfops+2+self.ins, trops = self.trops+other.trops+2+2*self.ins)
self.counted = 1
other.counted = 1
y.graph = graph
if self not in y.graph:
y.graph[self] = []
y.graph[self].append((y, 'pow', other.val*self.val**(other.val-1)))
if other not in y.graph:
y.graph[other] = []
y.graph[other].append((y, 'pow', self.val**other.val*np.log(np.abs(self.val))))
return y
except AttributeError:
other = ADnum(other*np.ones(np.shape(self.val)), der = np.zeros(np.shape(self.der)), constant = 1)
return self**other
def __rpow__(self, other):
try:
opcount = (1-self.counted)*self.ops*(1-self.constant)+(1-other.counted)*other.ops*(1-other.constant)
if self.constant and not other.constant:
opcount = opcount + 4
elif other.constant and not self.constant:
opcount = opcount + 4
else:
opcount = opcount + 10
return ADnum(other.val**self.val, der = self.val*(other.val**(self.val-1))*other.der+(other.val**self.val)*np.log(np.abs(other.val))*self.der, ops = (1-self.counted)*self.ops+other.ops+10, tfops=self.tfops+other.tfops+2+self.ins, trops=self.trops+other.trops+2+2*self.ins)
except AttributeError:
other = ADnum(other*np.ones(np.shape(self.val)), der = np.zeros(np.shape(self.der)), constant = 1)
return other**self
def merge_dicts(d1, d2):
''' Function to merge two dictionaries.
INPUTS
======
d1 : dictionary
d2 : dictionary
OUTPUTS
=======
dnew : new dictionary that is a combination of d1 and d2
'''
dnew = d1.copy()
for key in d2:
if key in dnew:
for item in d2[key]:
if item not in dnew[key]:
dnew[key] = dnew[key]+[item]
else:
dnew[key] = d2[key]
return dnew