Eigen::NumericalDiff< Functor_, mode > Class Template Reference
+ Inheritance diagram for Eigen::NumericalDiff< Functor_, mode >:

Public Types

enum  {
  InputsAtCompileTime ,
  ValuesAtCompileTime
}
 
typedef Functor_ Functor
 
typedef Functor::InputType InputType
 
typedef Functor::JacobianType JacobianType
 
typedef Functor::Scalar Scalar
 
typedef Functor::ValueType ValueType
 

Public Member Functions

int df (const InputType &_x, JacobianType &jac) const
 
 NumericalDiff (const Functor &f, Scalar _epsfcn=0.)
 
template<typename T0 >
 NumericalDiff (const T0 &a0)
 
template<typename T0 , typename T1 >
 NumericalDiff (const T0 &a0, const T1 &a1)
 
template<typename T0 , typename T1 , typename T2 >
 NumericalDiff (const T0 &a0, const T1 &a1, const T2 &a2)
 
 NumericalDiff (Scalar _epsfcn=0.)
 

Private Member Functions

NumericalDiffoperator= (const NumericalDiff &)
 

Private Attributes

Scalar epsfcn
 

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.

Definition at line 38 of file NumericalDiff.h.

Member Typedef Documentation

◆ Functor

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

Definition at line 41 of file NumericalDiff.h.

◆ InputType

template<typename Functor_ , NumericalDiffMode mode = Forward>
typedef Functor::InputType Eigen::NumericalDiff< Functor_, mode >::InputType

Definition at line 43 of file NumericalDiff.h.

◆ JacobianType

template<typename Functor_ , NumericalDiffMode mode = Forward>
typedef Functor::JacobianType Eigen::NumericalDiff< Functor_, mode >::JacobianType

Definition at line 45 of file NumericalDiff.h.

◆ Scalar

template<typename Functor_ , NumericalDiffMode mode = Forward>
typedef Functor::Scalar Eigen::NumericalDiff< Functor_, mode >::Scalar

Definition at line 42 of file NumericalDiff.h.

◆ ValueType

template<typename Functor_ , NumericalDiffMode mode = Forward>
typedef Functor::ValueType Eigen::NumericalDiff< Functor_, mode >::ValueType

Definition at line 44 of file NumericalDiff.h.

Member Enumeration Documentation

◆ anonymous enum

template<typename Functor_ , NumericalDiffMode mode = Forward>
anonymous enum
Enumerator
InputsAtCompileTime 
ValuesAtCompileTime 

Definition at line 58 of file NumericalDiff.h.

58  {
59  InputsAtCompileTime = Functor::InputsAtCompileTime,
60  ValuesAtCompileTime = Functor::ValuesAtCompileTime
61  };

Constructor & Destructor Documentation

◆ NumericalDiff() [1/5]

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

Definition at line 47 of file NumericalDiff.h.

47 : Functor(), epsfcn(_epsfcn) {}

◆ NumericalDiff() [2/5]

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

Definition at line 48 of file NumericalDiff.h.

48 : Functor(f), epsfcn(_epsfcn) {}

◆ NumericalDiff() [3/5]

template<typename Functor_ , NumericalDiffMode mode = Forward>
template<typename T0 >
Eigen::NumericalDiff< Functor_, mode >::NumericalDiff ( const T0 &  a0)
inline

Definition at line 52 of file NumericalDiff.h.

52 : Functor(a0), epsfcn(0) {}

◆ NumericalDiff() [4/5]

template<typename Functor_ , NumericalDiffMode mode = Forward>
template<typename T0 , typename T1 >
Eigen::NumericalDiff< Functor_, mode >::NumericalDiff ( const T0 &  a0,
const T1 &  a1 
)
inline

Definition at line 54 of file NumericalDiff.h.

54 : Functor(a0, a1), epsfcn(0) {}

◆ NumericalDiff() [5/5]

template<typename Functor_ , NumericalDiffMode mode = Forward>
template<typename T0 , typename T1 , typename T2 >
Eigen::NumericalDiff< Functor_, mode >::NumericalDiff ( const T0 &  a0,
const T1 &  a1,
const T2 &  a2 
)
inline

Definition at line 56 of file NumericalDiff.h.

56 : Functor(a0, a1, a2), epsfcn(0) {}

Member Function Documentation

◆ df()

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

return the number of evaluation of functor

Definition at line 66 of file NumericalDiff.h.

67  {
68  using std::sqrt;
69  using std::abs;
70  /* Local variables */
71  Scalar h;
72  int nfev=0;
73  const typename InputType::Index n = _x.size();
74  const Scalar eps = sqrt(((std::max)(epsfcn,NumTraits<Scalar>::epsilon() )));
75  ValueType val1, val2;
76  InputType x = _x;
77  // TODO : we should do this only if the size is not already known
78  val1.resize(Functor::values());
79  val2.resize(Functor::values());
80 
81  // initialization
82  switch(mode) {
83  case Forward:
84  // compute f(x)
85  Functor::operator()(x, val1); nfev++;
86  break;
87  case Central:
88  // do nothing
89  break;
90  default:
91  eigen_assert(false);
92  };
93 
94  // Function Body
95  for (int j = 0; j < n; ++j) {
96  h = eps * abs(x[j]);
97  if (h == 0.) {
98  h = eps;
99  }
100  switch(mode) {
101  case Forward:
102  x[j] += h;
103  Functor::operator()(x, val2);
104  nfev++;
105  x[j] = _x[j];
106  jac.col(j) = (val2-val1)/h;
107  break;
108  case Central:
109  x[j] += h;
110  Functor::operator()(x, val2); nfev++;
111  x[j] -= 2*h;
112  Functor::operator()(x, val1); nfev++;
113  x[j] = _x[j];
114  jac.col(j) = (val2-val1)/(2*h);
115  break;
116  default:
117  eigen_assert(false);
118  };
119  }
120  return nfev;
121  }
int n
#define eigen_assert(x)
Functor::InputType InputType
Definition: NumericalDiff.h:43
Functor::ValueType ValueType
Definition: NumericalDiff.h:44
Functor::Scalar Scalar
Definition: NumericalDiff.h:42
Eigen::AutoDiffScalar< EIGEN_EXPR_BINARYOP_SCALAR_RETURN_TYPE(Eigen::internal::remove_all_t< DerType >, typename Eigen::internal::traits< Eigen::internal::remove_all_t< DerType >>::Scalar, product) > sqrt(const Eigen::AutoDiffScalar< DerType > &x)
CleanedUpDerType< DerType >::type() max(const AutoDiffScalar< DerType > &x, const T &y)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
adouble abs(const adouble &x)
Definition: AdolcForward:74
std::ptrdiff_t j

◆ operator=()

template<typename Functor_ , NumericalDiffMode mode = Forward>
NumericalDiff& Eigen::NumericalDiff< Functor_, mode >::operator= ( const NumericalDiff< Functor_, mode > &  )
private

Member Data Documentation

◆ epsfcn

template<typename Functor_ , NumericalDiffMode mode = Forward>
Scalar Eigen::NumericalDiff< Functor_, mode >::epsfcn
private

Definition at line 123 of file NumericalDiff.h.


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