Goal: Learn how to define higher order types and how to compute higher order derivatives.
Prerequisite: Tutorial 2 - Reverse mode AD
Function: Simple real valued function for higher order derivatives
Full code:
Higher order derivative types in CoDiPack can be defined by nesting the provided CoDiPack types. The simplest on is the nesting of forward types:
The types are created by using the general types where the user can specify the type of the value and gradient. The nesting is done in the example up to the 6-th order. This nesting is called forward-over-forward since the forward AD mode is applied on the forward AD mode.
The reverse types can also be used to create higher oder types. The general recomendation is to create first a nesing with forward mode types and then apply once the reverse type:
Here, a 6-th order type is constructed by applying five times the forward mode and then once the reverse mode. This nesting is called forward-over-reverse.
With these two techniques arbitray higher order types can be constructed. There are also the two other posibilites to use a reverse-over-forward nesting or a reverse-over-reverse nesting. The first one is mathematical identical to the forward-over-reverse approach but it would create a very large reverse tape since all higher order computations are recorded on the tape. The second approach is theoretical possible but the seccond application of the reversal transforms the reverse code into a forward derivative code. Therefore it is more appropritate to use a forward mode type in the first place.
In order to know, which directions need to be set to get e.g. second order derivatives, the forward AD mode equation Forward AD Equation needs to be modified. (We use the notations from Uwe Naumans book "The Art of Differentiating Computer Programs".) Each application of the forward mode introduces a superscript with the number of the application. For the first application this is
The second application gets now the superscript
From these equations we learn, that all first order tangent directions
This can be extended to arbitrary derivative orders. All first order derivatives need to be seeded in order to compute the highest order derivative. For forward-over-reverse higher order types this is also true, but here n-1 direction need to be set during the recording and 1 direction during the reversal.
This tutorial uses the DerivativeAccess helper for the management of the derivative directions. It provides convinience functions that allow to set all derivatives on a specific order. For a compile time interface see the example Example 4 - Higher order derivatives compile time access. Example Example 5 - Higher order derivatives direct access shows how the gradients can be accessd without the helper.