exp_2: Second Order Reverse Mode
Keywords: exp_2: second order reverse mode purpose mathematical form f_5 index 5: f_4 4: f_3 3: f_2 2: f_1 verification exercises

| Prev | Next |  |  |  |  |  |  |

exp_2: Second Order Reverse Mode
Purpose
In general, a second order reverse sweep is given the first order expansion for all of the variables in an operation sequence. Given a choice of a particular variable, it computes the derivative, of that variables first order expansion coefficient, with respect to all of the independent variables.
Mathematical Form
Suppose that we use the algorithm exp_2.hpp to compute f ( x ) = 1 + x + x 2 / 2 The corresponding second derivative is ∂ 2 ∂ x 2 f ( x ) = 1
f_5
For our example, we chose to compute the derivative of v 5 ( 1 ) with respect to all the independent variable. For the case computed for the first order sweep , v 5 ( 1 ) is the derivative of the value returned by exp_2.hpp . This the value computed will be the second derivative of the value returned by exp_2.hpp . We begin with the function f 5 where v 5 ( 1 ) is both an argument and the value of the function; i.e., f 5 (v 1 ( 0 ) , v 1 ( 1 ) , … , v 5 ( 0 ) , v 5 ( 1 ) ) = v 5 ( 1 ) ∂ f 5 ∂ v 5 ( 1 ) = 1 All the other partial derivatives of f 5 are zero.
Index 5: f_4
Second order reverse mode starts with the last operation in the sequence. For the case in question, this is the operation with index 5. The zero and first order sweep representations of this operation are v 5 ( 0 ) = v 2 ( 0 ) + v 4 ( 0 ) v 5 ( 1 ) = v 2 ( 1 ) + v 4 ( 1 ) We define the function f 4 (v 1 ( 0 ) , … , v 4 ( 1 ) ) as equal to f 5 except that v 5 ( 0 ) and v 5 ( 1 ) are eliminated using this operation; i.e. f 4 = f 5 [v 1 ( 0 ) , … , v 4 ( 1 ) , v 5 ( 0 ) (v 2 ( 0 ) , v 4 ( 0 ) ) , v 5 ( 1 ) (v 2 ( 1 ) , v 4 ( 1 ) ) ] It follows that ∂ f 4 ∂ v 2 ( 1 ) = ∂ f 5 ∂ v 2 ( 1 ) + ∂ f 5 ∂ v 5 ( 1 ) * ∂ v 5 ( 1 ) ∂ v 2 ( 1 ) = 1 ∂ f 4 ∂ v 4 ( 1 ) = ∂ f 5 ∂ v 4 ( 1 ) + ∂ f 5 ∂ v 5 ( 1 ) * ∂ v 5 ∂ v 4 ( 1 ) = 1 All the other partial derivatives of f 4 are zero.
Index 4: f_3
The next operation has index 4, v 4 ( 0 ) = v 3 ( 0 ) / 2 v 4 ( 1 ) = v 3 ( 1 ) / 2 We define the function f 3 (v 1 ( 0 ) , … , v 3 ( 1 ) ) as equal to f 4 except that v 4 ( 0 ) and v 4 ( 1 ) are eliminated using this operation; i.e., f 3 = f 4 [v 1 ( 0 ) , … , v 3 ( 1 ) , v 4 ( 0 ) (v 3 ( 0 ) ) , v 4 ( 1 ) (v 3 ( 1 ) ) ] It follows that ∂ f 3 ∂ v 2 ( 1 ) = ∂ f 4 ∂ v 2 ( 1 ) = 1 ∂ f 3 ∂ v 3 ( 1 ) = ∂ f 4 ∂ v 3 ( 1 ) + ∂ f 4 ∂ v 4 ( 1 ) * ∂ v 4 ( 1 ) ∂ v 3 ( 1 ) = 0.5 All the other partial derivatives of f 3 are zero.
Index 3: f_2
The next operation has index 3, v 3 ( 0 ) = v 1 ( 0 ) * v 1 ( 0 ) v 3 ( 1 ) = 2 * v 1 ( 0 ) * v 1 ( 1 ) We define the function f 2 (v 1 ( 0 ) , … , v 2 ( 1 ) ) as equal to f 3 except that v 3 ( 0 ) and v 3 ( 1 ) are eliminated using this operation; i.e., f 2 = f 3 [v 1 ( 0 ) , … , v 2 ( 1 ) , v 3 ( 0 ) ( v 1 ( 0 ) ) , v 3 ( 1 ) ( v 1 ( 0 ) , v 1 ( 1 ) ) ] Note that, from the first order forward sweep , the value of v 1 ( 0 ) is equal to .5 and v 1 ( 1 ) is equal 1. It follows that ∂ f 2 ∂ v 1 ( 0 ) = ∂ f 3 ∂ v 1 ( 0 ) + ∂ f 3 ∂ v 3 ( 0 ) * ∂ v 3 ( 0 ) ∂ v 1 ( 0 ) + ∂ f 3 ∂ v 3 ( 1 ) * ∂ v 3 ( 1 ) ∂ v 1 ( 0 ) = 1 ∂ f 2 ∂ v 1 ( 1 ) = ∂ f 3 ∂ v 1 ( 1 ) + ∂ f 3 ∂ v 3 ( 1 ) * ∂ v 3 ( 1 ) ∂ v 1 ( 1 ) = 0.5 ∂ f 2 ∂ v 2 ( 0 ) = ∂ f 3 ∂ v 2 ( 0 ) = 0 ∂ f 2 ∂ v 2 ( 1 ) = ∂ f 3 ∂ v 2 ( 1 ) = 1
Index 2: f_1
The next operation has index 2, v 2 ( 0 ) = 1 + v 1 ( 0 ) v 2 ( 1 ) = v 1 ( 1 ) We define the function f 1 ( v 1 ( 0 ) , v 1 ( 1 ) ) as equal to f 2 except that v 2 ( 0 ) and v 2 ( 1 ) are eliminated using this operation; i.e., f 1 = f 2 [v 1 ( 0 ) , v 1 ( 1 ) , v 2 ( 0 ) ( v 1 ( 0 ) ) , v 2 ( 1 ) ( v 1 ( 1 ) ) ] It follows that ∂ f 1 ∂ v 1 ( 0 ) = ∂ f 2 ∂ v 1 ( 0 ) + ∂ f 2 ∂ v 2 ( 0 ) * ∂ v 2 ( 0 ) ∂ v 1 ( 0 ) = 1 ∂ f 1 ∂ v 1 ( 1 ) = ∂ f 2 ∂ v 1 ( 1 ) + ∂ f 2 ∂ v 2 ( 1 ) * ∂ v 2 ( 1 ) ∂ v 1 ( 1 ) = 1.5 Note that v 1 is equal to x , so the second derivative of the function defined by exp_2.hpp at x = .5 is given by ∂ 2 ∂ x 2 v 5 ( 0 ) = ∂ v 5 ( 1 ) ∂ x = ∂ v 5 ( 1 ) ∂ v 1 ( 0 ) = ∂ f 1 ∂ v 1 ( 0 ) = 1 There is a theorem about Algorithmic Differentiation that explains why the other partial of f 1 is equal to the first derivative of the function defined by exp_2.hpp at x = .5 .
Verification
The file exp_2_rev2.cpp contains a routine which verifies the values computed above. It returns true for success and false for failure. It only tests the partial derivatives of f j that might not be equal to the corresponding partials of f j + 1 ; i.e., the other partials of f j must be equal to the corresponding partials of f j + 1 .
Exercises

1. Which statement in the routine defined by exp_2_rev2.cpp uses the values that are calculated by the routine defined by exp_2_for0.cpp ? Which statements use values that are calculate by the routine defined in exp_2_for1.cpp ?
2. Consider the case where x = .1 and we first preform a zero order forward sweep, then a first order sweep, for the operation sequence used above. What are the results of a second order reverse sweep; i.e., what are the corresponding derivatives of f 5 , f 4 , … , f 1 .
3. Create a modified version of exp_2_rev2.cpp that verifies the values you obtained for the previous exercise. Also create and run a main program that reports the result of calling the modified version of exp_2_rev2.cpp .

Input File: introduction/exp_apx/exp_2.omh