Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Eigen::NumericalDiff< Functor_, mode > Class Template Reference

#include <contrib/Eigen/src/NumericalDiff/NumericalDiff.h>

Detailed Description

template<typename Functor_, NumericalDiffMode mode = Forward>
class Eigen::NumericalDiff< Functor_, mode >

This class allows you to add a method df() to your functor, which will use numerical differentiation to compute an approximate of the derivative for the functor. Of course, if you have an analytical form for the derivative, you should rather implement df() by yourself.

More information on http://en.wikipedia.org/wiki/Numerical_differentiation

Currently only "Forward" and "Central" scheme are implemented.

The step along coordinate j is

\[ h_j = \begin{cases} \max(|x_j|\,\mathrm{rel\_tol},\ \mathrm{abs\_tol}) & \text{if this is nonzero},\\ \mathrm{rel\_tol} & \text{otherwise.} \end{cases} \]

The forward difference is \((f(x + h_j e_j) - f(x))/h_j\) and the central difference is \((f(x + h_j e_j) - f(x - h_j e_j))/(2 h_j)\), each with the divisor replaced by the representable step actually applied. By default \(\mathrm{abs\_tol} = \mathrm{rel\_tol}\) (set by the constructors), so \(h_j \ge \mathrm{rel\_tol}\): this suits residuals with an offset of order 1 even when some \(|x_j|\) is tiny, where a step proportional to \(|x_j|\) would be lost in the rounding error of \(f\). For a function whose natural scale is \(|x_j|\) itself (parameters of tiny magnitude throughout), call setAbsTol(0): then \(h_j = \mathrm{rel\_tol}\,|x_j|\) as in MINPACK's fdjac2, and the second case applies only when this product is zero ( \(x_j = 0\), or \(|x_j|\,\mathrm{rel\_tol}\) underflows, so that \(x_j + h\) would equal \(x_j\)).

Public Member Functions

Scalar absTol () const
 
int df (const InputType &_x, JacobianType &jac) const
 
 NumericalDiff (const Functor &f, Scalar epsfcn=0.)
 
 NumericalDiff (Scalar epsfcn=0.)
 
Scalar relTol () const
 
void setAbsTol (Scalar _abs_tol)
 
void setRelTol (Scalar _rel_tol)
 

Constructor & Destructor Documentation

◆ NumericalDiff() [1/2]

template<typename Functor_, NumericalDiffMode mode = Forward>
Eigen::NumericalDiff< Functor_, mode >::NumericalDiff ( Scalar epsfcn = 0.)
inline

Constructs a default-initialized functor with rel_tol the square root (forward) or cube root (central) of max(epsfcn, NumTraits<Scalar>::epsilon()) and abs_tol = rel_tol, see the class documentation.

◆ NumericalDiff() [2/2]

template<typename Functor_, NumericalDiffMode mode = Forward>
Eigen::NumericalDiff< Functor_, mode >::NumericalDiff ( const Functor & f,
Scalar epsfcn = 0. )
inline

Same as above, copying the functor f.

Member Function Documentation

◆ absTol()

template<typename Functor_, NumericalDiffMode mode = Forward>
Scalar Eigen::NumericalDiff< Functor_, mode >::absTol ( ) const
inline
Returns
the lower bound abs_tol of the step.

◆ df()

template<typename Functor_, NumericalDiffMode mode = Forward>
int Eigen::NumericalDiff< Functor_, mode >::df ( const InputType & _x,
JacobianType & jac ) const
inline

Computes the Jacobian of the functor at _x into jac and returns the number of functor evaluations.

The step along coordinate j is h = max(|x[j]| * rel_tol, abs_tol), or h = rel_tol when that is zero (possible only for abs_tol == 0), with rel_tol and abs_tol selected by the user. The rel_tol selected by the user must be greater than or equal to epsilon, and abs_tol must be nonnegative, where epsilon is the machine precision NumTraits<Scalar>::epsilon(); the difference quotient divides by the representable step fl(x[j] + h) - x[j] actually applied.

In forward mode, the default rel_tol is sqrt(NumTraits<Scalar>::epsilon()) and the default abs_tol is abs_tol = rel_tol. In central mode, the default rel_tol is cbrt(NumTraits<Scalar>::epsilon()) and the default abs_tol = rel_tol. Call setAbsTol(0) for a purely relative step, see the class documentation.

◆ relTol()

template<typename Functor_, NumericalDiffMode mode = Forward>
Scalar Eigen::NumericalDiff< Functor_, mode >::relTol ( ) const
inline
Returns
the relative step rel_tol.

◆ setAbsTol()

template<typename Functor_, NumericalDiffMode mode = Forward>
void Eigen::NumericalDiff< Functor_, mode >::setAbsTol ( Scalar _abs_tol)
inline

Sets the lower bound abs_tol >= 0 of the step: \(h_j = \max(|x_j|\,\mathrm{rel\_tol}, \mathrm{abs\_tol})\). The default abs_tol = rel_tol suits residuals with an offset of order 1 even when some \(|x_j|\) is tiny. With 0 the step is purely relative, \(h_j = \mathrm{rel\_tol}\,|x_j|\) (rel_tol at \(x_j = 0\)), which suits a function whose natural scale is \(|x_j|\) itself.

◆ setRelTol()

template<typename Functor_, NumericalDiffMode mode = Forward>
void Eigen::NumericalDiff< Functor_, mode >::setRelTol ( Scalar _rel_tol)
inline

Sets the relative step rel_tol, which must be at least NumTraits<Scalar>::epsilon().


The documentation for this class was generated from the following file: