Evaluate the primal, Jacobian and Hessian of function objects. More...
#include <evaluationHelper.hpp>
Public Types | |
template<typename Func> | |
using | DefaultHandle = EvaluationHandleForward<Func, JacobianComputationType> |
template<typename Func> | |
using | DefaultHandle2nd = EvaluationHandleForward<Func, HessianComputationType> |
template<typename Func, size_t m, size_t n> | |
using | DefaultHandleFixed |
template<typename Func, size_t m, size_t n> | |
using | DefaultHandleFixed2nd |
template<typename VecIn, typename VecOut> | |
using | FunctorInterface = void (*)(VecIn const& x, VecOut& y) |
using | HessianComputationType = RealForwardGen<RealForwardVec<4>, Direction<RealForwardVec<4>, 4>> |
using | JacobianComputationType = RealForwardVec<4> |
Static Public Member Functions | |
template<typename Type, typename Func> | |
static inlineEvaluationHandle< Func, Type > | createHandle (Func &func, size_t m, size_t n) |
Helper function for the creation of an evaluation handle with the specified CoDiPack type and a variable vector size. | |
template<typename Func> | |
static inlineDefaultHandle< Func > | createHandleDefault (Func &func, size_t m, size_t n) |
Helper function for the creation of a default first order evaluation handle with a variable vector size. | |
template<typename Func> | |
static inlineDefaultHandle2nd< Func > | createHandleDefault2nd (Func &func, size_t m, size_t n) |
Helper function for the creation of a default second order evaluation handle with a variable vector size. | |
template<size_t m, size_t n, typename Func> | |
static inlineDefaultHandleFixed< Func, m, n > | createHandleDefaultFixed (Func &func) |
Helper function for the creation of a default first order evaluation handle with a compile time vector size. | |
template<size_t m, size_t n, typename Func> | |
static inlineDefaultHandleFixed2nd< Func, m, n > | createHandleDefaultFixed2nd (Func &func) |
Helper function for the creation of a default second order evaluation handle with a compile time vector size. | |
template<typename Type, size_t m, size_t n, typename Func> | |
static inlineEvaluationHandle< Func, Type, std::array< Type, n >, std::array< Type, m > > | createHandleFixed (Func &func) |
Helper function for the creation of an evaluation handle with the specified CoDiPack type and a compile time vector size. | |
template<typename Type, typename InputStore, typename OutputStore, typename Func> | |
static inlineEvaluationHandle< Func, Type, InputStore, OutputStore > | createHandleFull (Func &func, size_t m, size_t n) |
Helper function for the creation of an evaluation handle with the specified CoDiPack type and storage types. | |
template<typename T = double> | |
static inlineHessian< T > | createHessian (size_t m, size_t n) |
Create a Hessian with the given size. | |
template<size_t m, size_t n, typename T = double> | |
static inlineHessian< T, std::array< T, m *n *n > > | createHessianFixed () |
Create a Hessian with a compile time size. | |
template<typename T = double> | |
static inlineJacobian< T > | createJacobian (size_t m, size_t n) |
Create a Jacobian with the given size. | |
template<size_t m, size_t n, typename T = double> | |
static inlineJacobian< T, std::array< T, m *n > > | createJacobianFixed () |
Create a Jacobian with a compile time size. | |
template<typename Handle, typename VecX, typename Hes> | |
static inlinevoid | evalHandleHessian (Handle &handle, VecX const &x, Hes &hes) |
Compute the Hessian of the function object stored in the handle. | |
template<typename Handle, typename VecX, typename Jac> | |
static inlinevoid | evalHandleJacobian (Handle &handle, VecX const &x, Jac &jac) |
Compute the Jacobian of the function object stored in the handle. | |
template<typename Handle, typename VecX, typename Jac, typename Hes> | |
static inlinevoid | evalHandleJacobianAndHessian (Handle &handle, VecX const &x, Jac &jac, Hes &hes) |
Compute the Hessian of the evaluation procedure in the function object. | |
template<typename Handle, typename VecX, typename VecY> | |
static inlinevoid | evalHandlePrimal (Handle &handle, VecX const &x, VecY &y) |
Perform a primal evaluation of the function object stored in the handle. | |
template<typename Handle, typename VecX, typename VecY, typename Hes> | |
static inlinevoid | evalHandlePrimalAndHessian (Handle &handle, VecX const &x, VecY &y, Hes &hes) |
Compute the primal result and Hessian of the function object. | |
template<typename Handle, typename VecX, typename VecY, typename Jac> | |
static inlinevoid | evalHandlePrimalAndJacobian (Handle &handle, VecX const &x, VecY &y, Jac &jac) |
Compute the primal result and Jacobian of the function object stored in the handle. | |
template<typename Handle, typename VecX, typename VecY, typename Jac, typename Hes> | |
static inlinevoid | evalHandlePrimalAndJacobianAndHessian (Handle &handle, VecX const &x, VecY &y, Jac &jac, Hes &hes) |
Compute the primal result, Jacobian, and Hessian of the function object. | |
template<typename Func, typename VecX, typename Hes> | |
static inlinevoid | evalHessian (Func &func, VecX const &x, size_t const ySize, Hes &hes) |
Compute the Hessian of the function object. | |
template<typename Func, typename VecX, typename Jac> | |
static inlinevoid | evalJacobian (Func &func, VecX const &x, size_t const ySize, Jac &jac) |
Compute the Jacobian of the function object. | |
template<typename Func, typename VecX, typename Jac, typename Hes> | |
static inlinevoid | evalJacobianAndHessian (Func &func, VecX const &x, size_t ySize, Jac &jac, Hes &hes) |
Compute the Jacobian and Hessian of the function object. | |
template<typename Func, typename VecX, typename VecY> | |
static inlinevoid | evalPrimal (Func &func, VecX const &x, VecY &y) |
Perform a primal evaluation of the function object with the default first order type. | |
template<typename Func, typename VecX, typename VecY, typename Hes> | |
static inlinevoid | evalPrimalAndHessian (Func &func, VecX const &x, VecY &y, Hes &hes) |
Compute the primal result and Hessian of the function object. | |
template<typename Func, typename VecX, typename VecY, typename Jac> | |
static inlinevoid | evalPrimalAndJacobian (Func &func, VecX const &x, VecY &y, Jac &jac) |
Compute the primal result and Jacobian of the function object. | |
template<typename Func, typename VecX, typename VecY, typename Jac, typename Hes> | |
static inlinevoid | evalPrimalAndJacobianAndHessian (Func &func, VecX const &x, VecY &y, Jac &jac, Hes &hes) |
Compute the primal result, Jacobian, and Hessian of the function object. | |
Evaluate the primal, Jacobian and Hessian of function objects.
This helper provides the means to easily evaluate derivatives of arbitrary function objects. These function objects can be regular functions, lambda functions or structures where operator() is implemented.
The nomenclature and mathematical definitions for the function, the Jacobian, and the Hessian can be found in the Mathematical naming conventions documentation. Function arguments in this class follow the same naming scheme.
The algorithms will call the function objects with the vector of inputs and with the vector of outputs as arguments. The function object has to resemble the interface defined by FunctorInterface. An example function definition is:
x is the vector of input values and y is the vector of output values. ADType is the chosen CoDiPack type for the function. For most users, this definition will be enough. For more general examples please go to section Advanced function object definitions.
The CoDiPack type can be any ActiveType type from CoDiPack, for example all types that are defined in codi.hpp. The evaluation helper provides the default CoDiPack type definitions EvaluationHelper::JacobianComputationType and EvaluationHelper::HessianComputationType. These two use the forward mode of algorithmic differentiation and are more appropriate if
The most simple example of using the EvaluationHelper is:
Since we want to evaluate the Hessian, we use the Hessian type of the EvaluationHelper. The function is defined with this type and the standard vector classes. In the main function, we create the vector on which we want to call the function and then use the helper to create the storage for the Jacobian and Hessian. With
The evaluation helper class provides all combinations of evaluation choices, that is: evalPrimal(), evalJacobian(), evalHessian(), evalPrimalAndJacobian(), evalPrimalAndHessian(), evalJacobianAndHessian() and evalPrimalAndJacobianAndHessian(). Each of these functions uses the default CoDiPack types in the evaluation helper. In the cases where the primal is not stored, the user has to provide the number of output values manually.
If the EvaluationHelper is used to evaluate the same function several times, a higher performance can be achieved if a handle for the evaluation is created up front and then used several times. The above example with the handle creation would look like this:
The evaluation logic nearly stayed the same, but instead of providing the function to the evaluation routine, we create a handle up front and then use this handle in the evalHandle method. Each of the above mentioned eval routines has a corresponding evalHandle method.
Each of the create methods has similar create..Fixed method which uses the std::array type instead of the std::vector type for the data management. These methods can be used if the size is known at compile time. An example with these methods would be:
Until now, the default definition for the used CoDiPack types have been used. In order to use an arbitrary CoDiPack type, the createHandle(), createHandleFixed(), and createHandleFull() methods can be used. The first one uses std::vectors for the storage, the second method std::array, and in the third the user can provide the storage class as a template parameter. The use case for the createHandle() method would look like this:
The function object can also have a template argument for the evaluation type, e.g.:
There is also no need to specify std::vector as the array class, e.g.:
using codi::EvaluationHelper::DefaultHandle = EvaluationHandleForward<Func, JacobianComputationType> |
Type for the default handle for first order derivative computations with a variable vector size.
Func | See FunctorInterface. |
using codi::EvaluationHelper::DefaultHandle2nd = EvaluationHandleForward<Func, HessianComputationType> |
Type for the default handle for second order derivative computations with a variable vector size.
Func | See FunctorInterface. |
using codi::EvaluationHelper::DefaultHandleFixed |
Type for the default handle for first order derivative computations with a compile time vector size.
Func | See FunctorInterface |
m | The size of the output vector. |
n | The size of the input vector. |
using codi::EvaluationHelper::DefaultHandleFixed2nd |
Type for the default handle for second order derivative computations with a compile time vector size.
Func | See FunctorInterface |
m | The size of the output vector. |
n | The size of the input vector. |
using codi::EvaluationHelper::FunctorInterface = void (*)(VecIn const& x, VecOut& y) |
Function object syntax for all Func template arguments.
VecIn | User defined (default: std::vector). |
VecOut | User defined (default: std::vector). |
using codi::EvaluationHelper::HessianComputationType = RealForwardGen<RealForwardVec<4>, Direction<RealForwardVec<4>, 4>> |
The default type used for second order derivative computation. It is defined as forward vector over forward vector AD mode of size 4 and 4. */
The default type used for first order derivative computation. It is defined as forward vector AD mode of size 4.
|
inlinestatic |
Helper function for the creation of an evaluation handle with the specified CoDiPack type and a variable vector size.
The CoDiPack type can be an arbitrary one:
[in] | func | The function object for the evaluation (see FunctorInterface). |
[in] | m | The size of the output vector. |
[in] | n | The size of the input vector. |
CoDiType | An arbitrary CoDiPack type based on ActiveType. All definitions in codi.hpp are supported. For user developed tapes, EvaluationHandle has to be specialized. |
Func | See FunctorInterface. |
|
inlinestatic |
Helper function for the creation of a default first order evaluation handle with a variable vector size.
[in] | func | The function object for the evaluation (see FunctorInterface). |
[in] | m | The size of the output vector. |
[in] | n | The size of the input vector. |
Func | See FunctorInterface. |
|
inlinestatic |
Helper function for the creation of a default second order evaluation handle with a variable vector size.
[in] | func | The function object for the evaluation (see FunctorInterface). |
[in] | m | The size of the output vector. |
[in] | n | The size of the input vector. |
Func | See FunctorInterface. |
|
inlinestatic |
Helper function for the creation of a default first order evaluation handle with a compile time vector size.
[in] | func | The function object for the evaluation (see FunctorInterface). |
m | The size of the output vector. |
n | The size of the input vector. |
Func | See FunctorInterface. |
|
inlinestatic |
Helper function for the creation of a default second order evaluation handle with a compile time vector size.
[in] | func | The function object for the evaluation (see FunctorInterface). |
m | The size of the output vector. |
n | The size of the input vector. |
Func | See FunctorInterface. |
|
inlinestatic |
Helper function for the creation of an evaluation handle with the specified CoDiPack type and a compile time vector size.
The CoDiPack type can be an arbitrary one:
[in] | func | The function object for the evaluation (see FunctorInterface). |
CoDiType | An arbitrary CoDiPack type based on ActiveType. All definitions in codi.hpp are supported. For user developed tapes, EvaluationHandle has to be specialized. |
Func | See FunctorInterface. |
m | The size of the output vector. |
n | The size of the input vector. |
|
inlinestatic |
Helper function for the creation of an evaluation handle with the specified CoDiPack type and storage types.
The CoDiPack type can be an arbitrary one and the storage types must use it for their elements.
[in] | func | The function object for the evaluation (see FunctorInterface). |
[in] | m | The size of the output vector. |
[in] | n | The size of the input vector. |
CoDiType | An arbitrary CoDiPack type based on ActiveType. All definitions in codi.hpp are supported. For user developed tapes, EvaluationHandle has to be specialized. |
Func | See FunctorInterface |
InputStore | The storage type for vectors of input variables. |
OutputStore | The storage type for vectors of output variables. |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
Compute the Hessian of the function object stored in the handle.
[in] | handle | The handle with all data for the evaluation. |
[in] | x | The vector with the primal values where the function object is evaluated. |
[out] | hes | The Hessian in which the values are stored. |
Handle | The handle type for the data storage and the evaluation. |
VecX | The vector type for the input values. Element type is e.g. double. |
Hes | The storage type of the Hessian. Element type is e.g. double. |
|
inlinestatic |
Compute the Jacobian of the function object stored in the handle.
[in] | handle | The handle with all data for the evaluation. |
[in] | x | The vector with the primal values where the function object is evaluated. |
[out] | jac | The Jacobian in which the values are stored. |
Handle | The handle type for the data storage and the evaluation. |
VecX | The vector type for the input values. Element type is e.g. double. |
Jac | The storage type of the Jacobian. Element type is e.g. double. |
|
inlinestatic |
Compute the Hessian of the evaluation procedure in the function object.
In this method the Jacobian is also stored.
|
inlinestatic |
Perform a primal evaluation of the function object stored in the handle.
[in] | handle | The handle with all data for the evaluation. |
[in] | x | The vector with the primal values where the function object is evaluated. |
[out] | y | The vector for the result of the primal function evaluation. The vector must have the correct size allocated. |
Handle | The handle type for the data storage and the evaluation. |
VecX | The vector type for the input values. Element type is e.g. double. |
VecY | The vector type for the output values. Element type is e.g. double. |
|
inlinestatic |
Compute the primal result and Hessian of the function object.
[in] | handle | The handle with all data for the evaluation. |
[in] | x | The vector with the primal values where the function object is evaluated. |
[out] | y | The vector for the result of the primal function evaluation. The vector must have the correct size allocated. |
[out] | hes | The Hessian in which the values are stored. |
Handle | The handle type for the data storage and the evaluation. |
VecX | The vector type for the input values. Element type is e.g. double. |
VecY | The vector type for the output values. Element type is e.g. double. |
Hes | The storage type of the Hessian. Element type is e.g. double. |
|
inlinestatic |
Compute the primal result and Jacobian of the function object stored in the handle.
[in] | handle | The handle with all data for the evaluation. |
[in] | x | The vector with the primal values where the function object is evaluated. |
[out] | y | The vector for the result of the primal function evaluation. The vector must have the correct size allocated. |
[out] | jac | The Jacobian in which the values are stored. |
Handle | The handle type for the data storage and the evaluation. |
VecX | The vector type for the input values. Element type is e.g. double. |
VecY | The vector type for the output values. Element type is e.g. double. |
Jac | The storage type of the Jacobian. Element type is e.g. double. |
|
inlinestatic |
Compute the primal result, Jacobian, and Hessian of the function object.
[in] | handle | The handle with all data for the evaluation. |
[in] | x | The vector with the primal values where the function object is evaluated. |
[out] | y | The vector for the result of the primal function evaluation. The vector must have the correct size allocated. |
[out] | jac | The Jacobian in which the values are stored. |
[out] | hes | The Hessian in which the values are stored. |
Handle | The handle type for the data storage and the evaluation. |
VecX | The vector type for the input values. Element type is e.g. double. |
VecY | The vector type for the output values. Element type is e.g. double. |
Jac | The storage type of the Jacobian. Element type is e.g. double. |
Hes | The storage type of the Hessian. Element type is e.g. double. |
|
inlinestatic |
Compute the Hessian of the function object.
[in] | func | The function object for the evaluation (see FunctorInterface). |
[in] | x | The vector with the primal values where the function object is evaluated. |
[in] | ySize | The size of the output vector. |
[out] | hes | The Hessian in which the values are stored. |
Func | See FunctorInterface. |
VecX | The vector type for the input values. Element type is e.g. double. |
Hes | The storage type of the Hessian. Element type is e.g. double. |
|
inlinestatic |
Compute the Jacobian of the function object.
[in] | func | The function object for the evaluation (see FunctorInterface). |
[in] | x | The vector with the primal values where the function object is evaluated. |
[in] | ySize | The size of the output vector. |
[out] | jac | The Jacobian in which the values are stored. |
Func | See FunctorInterface. |
VecX | The vector type for the input values. Element type is e.g. double. |
Jac | The storage type of the Jacobian. Element type is e.g. double. |
|
inlinestatic |
Compute the Jacobian and Hessian of the function object.
[in] | func | The function object for the evaluation (see FunctorInterface). |
[in] | x | The vector with the primal values where the function object is evaluated. |
[in] | ySize | The size of the output vector. |
[out] | jac | The Jacobian in which the values are stored. |
[out] | hes | The Hessian in which the values are stored. |
Func | See FunctorInterface |
VecX | The vector type for the input values. Element type is e.g. double. |
Jac | The storage type of the Jacobian. Element type is e.g. double. |
Hes | The storage type of the Hessian. Element type is e.g. double. |
|
inlinestatic |
Perform a primal evaluation of the function object with the default first order type.
[in] | func | The function object for the evaluation (see FunctorInterface). |
[in] | x | The vector with the primal values where the function object is evaluated. |
[out] | y | The vector for the result of the primal function evaluation. The vector must have the correct size allocated. |
Func | See FunctorInterface. |
VecX | The vector type for the input values. Element type is e.g. double. |
VecY | The vector type for the output values. Element type is e.g. double. |
|
inlinestatic |
Compute the primal result and Hessian of the function object.
[in] | func | The function object for the evaluation (see FunctorInterface). |
[in] | x | The vector with the primal values where the function object is evaluated. |
[out] | y | The vector for the result of the primal function evaluation. The vector must have the correct size allocated. |
[out] | hes | The Hessian in which the values are stored. |
Func | See FunctorInterface. |
VecX | The vector type for the input values. Element type is e.g. double. |
VecY | The vector type for the output values. Element type is e.g. double. |
Hes | The storage type of the Hessian. Element type is e.g. double. |
|
inlinestatic |
Compute the primal result and Jacobian of the function object.
[in] | func | The function object for the evaluation (see FunctorInterface). |
[in] | x | The vector with the primal values where the function object is evaluated. |
[out] | y | The vector for the result of the primal function evaluation. The vector must have the correct size allocated. |
[out] | jac | The Jacobian in which the values are stored. |
Func | See FunctorInterface. |
VecX | The vector type for the input values. Element type is e.g. double. |
VecY | The vector type for the output values. Element type is e.g. double. |
Jac | The storage type of the Jacobian. Element type is e.g. double. |
|
inlinestatic |
Compute the primal result, Jacobian, and Hessian of the function object.
[in] | func | The function object for the evaluation (see FunctorInterface). |
[in] | x | The vector with the primal values where the function object is evaluated. |
[out] | y | The vector for the result of the primal function evaluation. The vector must have the correct size allocated. |
[out] | jac | The Jacobian in which the values are stored. |
[out] | hes | The Hessian in which the values are stored. |
Func | See FunctorInterface. |
VecX | The vector type for the input values. Element type is e.g. double. |
VecY | The vector type for the output values. Element type is e.g. double. |
Jac | The storage type of the Jacobian. Element type is e.g. double. |
Hes | The storage type of the Hessian. Element type is e.g. double. |