Goal:
Prerequisite: Tutorial 1 - Forward mode AD, Tutorial 2 - Reverse mode AD
Function: Simple vector valued function
Full code:
With the forward and reverse AD modes only directional derivatives or reverse directional derivatives can be computed. For a computation of the full Jacobian matrix the tangent direction or adjoint direction need to be set to the unit vectors and the corresponding AD mode has to be evaluated for each unit vector.
In general, the Jacobian computation for both modes, forward and reverse, uses all things that have been explained in the basic tutorials.
For a Jacobian evaluation in the forward mode an iteration over the input dimension is required (Step 1). Then a regular forwad mode evaluation follows (Step 2 and Step 3). The new element is Step 4. Here, the tangent seeding from Step 1 is cleared so that only unit vectors are set as tangent direction.
The Jacobian evaluation in the reverse mode differs a little bit form the procedure in Tutorial 2 - Reverse mode AD. The tape is recorded as usual, but only once (Step 1). Afterwards, a regular seeding of the output values is performed and the tape is evaluated (Step 3 and Step 4). The additional function call is required in Step 5. Here, the adjoint vector of CoDiPack is cleared, such that no adjoint values from the previous tape evaluation disturb the result. The function clearAdjoints only clears the adjoint in contrast to a tape reset which also clears the tape data. The optional parameter on the tape reset function allows to skip the clear of the adjoints.
CoDiPack clears the adjoints of all intermediate variables automatically, only the ones from input variables are left in place. (Adjoints of output variables are also cleared.) Therefore a complete reset of the adjoint vector is not necessary. After the adjoint of an output value is accessed, the adjoint value can be cleared manually with x[j].gradient() = 0.0
in Step 4. Then Step 5 is no longer necessary. If not all input variables are cleared this way, subsequent results will be wrong.