exp_2: First Order Reverse Mode
Keywords: exp_2: first 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: First Order Reverse Mode
Purpose
First order reverse mode uses the operation sequence , and zero order forward sweep values, to compute the first order derivative of one dependent variable with respect to all the independent variables. The computations are done in reverse of the order of the computations in the original algorithm.
Mathematical Form
Suppose that we use the algorithm exp_2.hpp to compute f ( x ) = 1 + x + x 2 / 2 The corresponding derivative function is ∂ x f ( x ) = 1 + x
f_5
For our example, we chose to compute the derivative of the value returned by exp_2.hpp which is equal to the symbol v 5 in the exp_2 operation sequence . We begin with the function f 5 where v 5 is both an argument and the value of the function; i.e., f 5 ( v 1 , v 2 , v 3 , v 4 , v 5 ) = v 5 ∂ f 5 ∂ v 5 = 1 All the other partial derivatives of f 5 are zero.
Index 5: f_4
Reverse mode starts with the last operation in the sequence. For the case in question, this is the operation with index 5, v 5 = v 2 + v 4 We define the function f 4 ( v 1 , v 2 , v 3 , v 4 ) as equal to f 5 except that v 5 is eliminated using this operation; i.e. f 4 = f 5 [ v 1 , v 2 , v 3 , v 4 , v 5 ( v 2 , v 4 ) ] It follows that ∂ f 4 ∂ v 2 = ∂ f 5 ∂ v 2 + ∂ f 5 ∂ v 5 * ∂ v 5 ∂ v 2 = 1 ∂ f 4 ∂ v 4 = ∂ f 5 ∂ v 4 + ∂ f 5 ∂ v 5 * ∂ v 5 ∂ v 4 = 1 All the other partial derivatives of f 4 are zero.
Index 4: f_3
The next operation has index 4, v 4 = v 3 / 2 We define the function f 3 ( v 1 , v 2 , v 3 ) as equal to f 4 except that v 4 is eliminated using this operation; i.e., f 3 = f 4 [ v 1 , v 2 , v 3 , v 4 ( v 3 ) ] It follows that ∂ f 3 ∂ v 1 = ∂ f 4 ∂ v 1 = 0 ∂ f 3 ∂ v 2 = ∂ f 4 ∂ v 2 = 1 ∂ f 3 ∂ v 3 = ∂ f 4 ∂ v 3 + ∂ f 4 ∂ v 4 * ∂ v 4 ∂ v 3 = 0.5
Index 3: f_2
The next operation has index 3, v 3 = v 1 * v 1 We define the function f 2 ( v 1 , v 2 ) as equal to f 3 except that v 3 is eliminated using this operation; i.e., f 2 = f 3 [ v 1 , v 2 , v 3 ( v 1 ) ] Note that the value of v 1 is equal to x which is .5 for this evaluation. It follows that ∂ f 2 ∂ v 1 = ∂ f 3 ∂ v 1 + ∂ f 3 ∂ v 3 * ∂ v 3 ∂ v 1 = 0.5 ∂ f 2 ∂ v 2 = ∂ f 3 ∂ v 2 = 1
Index 2: f_1
The next operation has index 2, v 2 = 1 + v 1 We define the function f 1 ( v 1 ) as equal to f 2 except that v 2 is eliminated using this operation; i.e., f 1 = f 2 [ v 1 , v 2 ( v 1 ) ] It follows that ∂ f 1 ∂ v 1 = ∂ f 2 ∂ v 1 + ∂ f 2 ∂ v 2 * ∂ v 2 ∂ v 1 = 1.5 Note that v 1 is equal to x , so the derivative of this is the derivative of the function defined by exp_2.hpp at x = .5 .
Verification
The file exp_2_rev1.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_rev1.cpp uses the values that are calculated by the routine defined by exp_2_for0.cpp ?
2. Consider the case where x = .1 and we first preform a zero order forward sweep for the operation sequence used above. What are the results of a first 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_rev1.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_rev1.cpp .

Input File: introduction/exp_apx/exp_2.omh