from pycppad import *
def pycppad_test_two_levels():
# start recording a_float operations
x = numpy.array( [ 2. , 3. ] )
a_x = independent(x)
# start recording a2float operations
a_u = numpy.array( [a_x[0] , ad(1) ] )
a2u = independent(a_u)
# stop a2float recording and store operations if f
a2v = numpy.array( [ a2u[0] * a2u[0] + a2u[1] * a2u[1] ] )
a_f = adfun(a2u, a2v) # F(u0, u1) = u0 * u0 + u1 * u1
# evaluate the gradient of F
a_J = a_f.jacobian(a_u)
# stop a_float recording and store operations in g
a_y = numpy.array( [ a_x[1] * a_J[0,0] + a_x[0] * a_J[0,1] ] )
g = adfun(a_x, a_y) # G(x0, x1) = x1 * F_u0(x0, 1) + x0 * F_u1(x0, 1)
# evaluate the gradient of G
J = g.jacobian(x)
assert J[0,0] == 2. * x[1] + 2
assert J[0,1] == 2. * x[0]