-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathadd_eq.py
53 lines (40 loc) · 955 Bytes
/
add_eq.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
# python equivalent of the CppAD example https://coin-or.github.io/CppAD/doc/addeq.cpp.htm
from pycppad import AD, Independent, ADFun, Value
import numpy as np
# domain space vector
n = 1
x0 = 0.5
x = np.array([AD(x0)] * n)
# declare independent variables and start tape recording
Independent(x)
# range space vector
m = 2
y = np.zeros(m, dtype=AD)
y[0] = x[0]
# initial value
y[0] += AD(2)
# AD<double> += int
y[0] += AD(4.)
# AD<double> += double
y[0] += x[0]
y[1] = y[0]
# use the result of a compound assignment
# create f: x -> y and stop tape recording
f = ADFun(x, y)
# input("raw_input")
# f.optimize("no_compare_op")
assert Value(y[0]) == x0 + 2. + 4. + x0
assert Value(y[1]) == Value(y[0])
dx = np.zeros(n)
dy = np.zeros(m)
dx[0] = 1.
dy = f.Forward(1, dx)
assert dy[0] == 2.
assert dy[1] == 2.
# reverse computation of derivative of y[0]
w = np.zeros(m)
dw = np.zeros(n)
w[0] = 1.
w[1] = 0.
dw = f.Reverse(1, w)
assert dw[0] == 2.