Prev Next

Using Multiple Levels of AD

Background
If f is an ADFun<Base> object, the vectors returned by f.Forward , and f.Reverse , have values in the base type (Base) and not AD<Base>. This reflects the fact that operations used to calculate these function values are not recorded by the tape corresponding to AD<Base> operations.

Motivation
Suppose that you uses derivatives of one or more inner functions as part of the operations needed to compute an outer function. For example, the derivatives returned by f.Forward might be used as part of Taylor's method for solving ordinary differential equations. In addition, we might want to differentiate the solution of a differential equation with respect to parameters in the equation. This can be accomplished in the following way:
  1. The operations during the calculations of the function defining the differential equation could be preformed using the a class of the form  AD< AD<double> >.
  2. The operations during the calculation of Taylor's method could be preformed using the  AD<double> class.
  3. The results of the solution of the differential equation could then be preformed using the double class.


General Solution
Provided that we are currently recording  AD<double> operations, and fin is an ADFun< AD<double> > object, the operations used to compute the vectors returned by fin.Forward, fin.Rev, and fin.RevTwo, will be recorded on the tape corresponding to AD<double> operations.

General Procedure

Start ADBaseTape
The first step is to declare the independent variables using
     Independent(
x)
where x is a SimpleVector with elements of type AD<double>. This will start recording a new tape of operations performed using AD<double> class objects.

Start ADDBaseTape
The next step is to declare the independent variables using
     Independent(
X)
where X is a SimpleVector with elements of type CPPAD_TEST_VECTOR< AD< AD<double> > >. This will start recording a new tape of operations performed using AD< AD<double> > class objects.

Inner Function Calculations
The next step is to calculation the inner functions using AD< AD<double> > class objects.

Derivative of Inner Function
The next step is to create the ADFun< AD<double> > function object fin. This will also stop recording of operations performed using AD< AD<double> > class objects. The fin object can then be used to calculate the derivatives needed to compute the outer function.

Outer Function
The next step is to compute the outer function using AD<double> class objects.

Derivative of Outer Function
The next step is to create the ADFun<double> function object fout. This will also stop the recording of operations performed using AD<double> class objects. The fout object can then be used to calculate the derivatives of the outer function.

Example
The file mul_level.cpp contains an example and test of this procedure. It returns true if it succeeds and false otherwise. The file ode_taylor.cpp is a more complex example use of multiple tapes.
Input File: omh/mul_level.omh