File tree Expand file tree Collapse file tree 1 file changed +23
-20
lines changed Expand file tree Collapse file tree 1 file changed +23
-20
lines changed Original file line number Diff line number Diff line change 33from pycppad import AD , Independent , ADFun , Value
44import numpy as np
55
6-
76# domain space vector
8- n = 1 ;
7+ n = 1
98x0 = 0.5
109
11- x = np .array ([AD (x0 ),] * n )
10+ x = np .array ([AD (x0 )] * n )
1211
1312# declare independent variables and start tape recording
1413Independent (x )
1514
1615# range space vector
17- m = 2 ;
18- y = np .zeros ((m ,),dtype = AD )
19- y [0 ] = x [0 ]; # initial value
20- y [0 ] += AD (2 ); # AD<double> += int
21- y [0 ] += AD (4. ); # AD<double> += double
16+ m = 2
17+ y = np .zeros (m , dtype = AD )
18+ y [0 ] = x [0 ]
19+ # initial value
20+ y [0 ] += AD (2 )
21+ # AD<double> += int
22+ y [0 ] += AD (4. )
23+ # AD<double> += double
2224y [0 ] += x [0 ]
23- y [1 ] = y [0 ]; # use the result of a compound assignment
25+ y [1 ] = y [0 ]
26+ # use the result of a compound assignment
2427
2528# create f: x -> y and stop tape recording
26- f = ADFun (x ,y );
27- input ("raw_input" )
29+ f = ADFun (x , y )
30+ # input("raw_input")
2831
29- #f.optimize("no_compare_op")
32+ # f.optimize("no_compare_op")
3033
31- assert ( Value (y [0 ]) == x0 + 2. + 4. + x0 )
32- assert ( Value (y [1 ]) == Value (y [0 ]) )
34+ assert Value (y [0 ]) == x0 + 2. + 4. + x0
35+ assert Value (y [1 ]) == Value (y [0 ])
3336
3437dx = np .zeros (n )
3538dy = np .zeros (m )
3639
3740dx [0 ] = 1.
38- dy = f .Forward (1 , dx )
39- assert ( dy [0 ] == 2. )
40- assert ( dy [1 ] == 2. )
41+ dy = f .Forward (1 , dx )
42+ assert dy [0 ] == 2.
43+ assert dy [1 ] == 2.
4144
4245# reverse computation of derivative of y[0]
43- w = np .zeros (( m ,) )
44- dw = np .zeros (( n ), )
46+ w = np .zeros (m )
47+ dw = np .zeros (n )
4548
4649w [0 ] = 1.
4750w [1 ] = 0.
4851dw = f .Reverse (1 , w )
4952
50- assert ( dw [0 ] == 2. )
53+ assert dw [0 ] == 2.
You can’t perform that action at this time.
0 commit comments