TMB Documentation  v1.9.10
Functions
autodiff Namespace Reference

Automatic differentiation (gradient, hessian, jacobian) More...

Functions

template<class Functor , class Type >
vector< Type > gradient (Functor F, vector< Type > x)
 Calculate gradient of vector function with scalar values. More...
 
template<class Functor , class Type >
matrix< Type > hessian (Functor f, vector< Type > x)
 Calculate hessian of vector function with scalar values. More...
 
template<class Functor , class Type >
matrix< Type > jacobian (Functor f, vector< Type > x)
 Calculate jacobian of vector function with vector values. More...
 

Detailed Description

Automatic differentiation (gradient, hessian, jacobian)

Function Documentation

§ gradient()

template<class Functor , class Type >
vector<Type> autodiff::gradient ( Functor  F,
vector< Type >  x 
)

Calculate gradient of vector function with scalar values.

Parameters
FFunctor with scalar values
xVector evaluation point
Returns
Gradient vector
Note
The evaluation method of the functor must be templated

Example:

#include <TMB.hpp>
struct func {
template <class T>
T operator()(vector<T> x){ // Evaluate function
return exp( -x.sum() );
}
};
template<class Type>
Type objective_function<Type>::operator() () {
func f;
// Calculate gradient
REPORT(g);
// Exit
return 0;
}
Examples:
laplace.cpp.

Definition at line 67 of file autodiff.hpp.

§ hessian()

template<class Functor , class Type >
matrix<Type> autodiff::hessian ( Functor  f,
vector< Type >  x 
)

Calculate hessian of vector function with scalar values.

Parameters
fFunctor with scalar values
xVector evaluation point
Returns
Hessian matrix
Note
The evaluation method of the functor must be templated

Example:

#include <TMB.hpp>
struct func {
template <class T>
T operator()(vector<T> x){ // Evaluate function
return exp( -x.sum() );
}
};
template<class Type>
Type objective_function<Type>::operator() () {
func f;
// Calculate hessian
REPORT(h);
// Exit
return 0;
}
Examples:
laplace.cpp.

Definition at line 134 of file autodiff.hpp.

§ jacobian()

template<class Functor , class Type >
matrix<Type> autodiff::jacobian ( Functor  f,
vector< Type >  x 
)

Calculate jacobian of vector function with vector values.

Parameters
fFunctor with vector values
xVector evaluation point
Returns
Jacobian matrix
Note
The evaluation method of the functor must be templated

Example:

#include <TMB.hpp>
struct func {
template <class T>
vector<T> operator()(vector<T> x){ // Evaluate function
vector<T> y(2);
y(0) = x.sum();
y(1) = x.prod();
return y;
}
};
template<class Type>
Type objective_function<Type>::operator() () {
func f;
// Calculate jacobian
REPORT(j);
// Exit
return 0;
}

Definition at line 203 of file autodiff.hpp.

Referenced by density::MVNORM_t< scalartype >::operator()(), density::N01< scalartype_ >::operator()(), density::AR1_t< distribution >::operator()(), density::ARk_t< scalartype_ >::operator()(), density::contAR2_t< scalartype_ >::operator()(), density::SCALE_t< distribution >::operator()(), and density::VECSCALE_t< distribution >::operator()().

License: GPL v2