TMB Documentation  v1.9.2
romberg Namespace Reference

Univariate and multivariate numerical integration. More...

## Functions

template<class Type , class F >
Type integrate (F f, Type a, Type b, int n=7, int p=2)
1D numerical integration using Romberg's method. More...

template<class Type , class F >
Type integrate (F f, vector< Type > a, vector< Type > b, int n=7, int p=2)
Multi-dimensional numerical integration using Romberg's method. Dimension need not be known at compile time. More...

## Detailed Description

Univariate and multivariate numerical integration.

• Based on CppAD's Romberg integration routines. Adapted so easy to use with TMB, e.g. allow multivariate dimension to be unknown at compile time.

## § integrate() [1/2]

template<class Type , class F >
 Type romberg::integrate ( F f, Type a, Type b, int n = 7, int p = 2 )

1D numerical integration using Romberg's method.

Parameters
 f Univariate functor a Lower scalar integration limit b Upper scalar integration limit n Subdivisions ( $$2^{n-1}+1$$ function evaluations).
Note
The method is not adaptive and the user may have to specify a larger n.

Example:

#include <TMB.hpp>
template<class Type>
struct univariate {
Type theta; // Parameter in integrand
univariate(Type theta_) // Constructor of integrand
: theta (theta_) {} // Initializer list
Type operator()(Type x){ // Evaluate integrand
return exp( -theta * (x * x) );
}
};
template<class Type>
Type objective_function<Type>::operator() () {
PARAMETER(theta);
univariate<Type> f(theta);
Type res = romberg::integrate(f, a, b);
return res;
}
Examples:

Definition at line 52 of file romberg.hpp.

## § integrate() [2/2]

template<class Type , class F >
 Type romberg::integrate ( F f, vector< Type > a, vector< Type > b, int n = 7, int p = 2 )

Multi-dimensional numerical integration using Romberg's method. Dimension need not be known at compile time.

Parameters
 f Multivariate functor a Lower vector integration limit b Upper vector integration limit n Subdivisions per dimension ( $$(2^{n-1}+1)^d$$ function evaluations).
Note
The method is not adaptive and the user may have to specify a larger n.

Example:

#include <TMB.hpp>
template<class Type>
struct multivariate {
Type theta; // Parameter in integrand
multivariate(Type theta_) // Constructor of integrand
: theta (theta_) {} // Initializer list
Type operator() // Evaluate integrand
return exp( -theta * (x * x).sum() );
}
};
template<class Type>
Type objective_function<Type>::operator() () {
PARAMETER(theta);
multivariate<Type> f(theta);
Type res = romberg::integrate(f, a, b);
return res;
}

Definition at line 154 of file romberg.hpp.