NumericalDiff.h
Go to the documentation of this file.
1 // -*- coding: utf-8
2 // vim: set fileencoding=utf-8
3 
4 // This file is part of Eigen, a lightweight C++ template library
5 // for linear algebra.
6 //
7 // Copyright (C) 2009 Thomas Capricelli <orzel@freehackers.org>
8 //
9 // This Source Code Form is subject to the terms of the Mozilla
10 // Public License v. 2.0. If a copy of the MPL was not distributed
11 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 
13 #ifndef EIGEN_NUMERICAL_DIFF_H
14 #define EIGEN_NUMERICAL_DIFF_H
15 
16 namespace Eigen {
17 
21 };
22 
23 
35 template<typename _Functor, NumericalDiffMode mode=Forward>
36 class NumericalDiff : public _Functor
37 {
38 public:
39  typedef _Functor Functor;
40  typedef typename Functor::Scalar Scalar;
41  typedef typename Functor::InputType InputType;
42  typedef typename Functor::ValueType ValueType;
44 
45  NumericalDiff(Scalar _epsfcn=0.) : Functor(), epsfcn(_epsfcn) {}
46  NumericalDiff(const Functor& f, Scalar _epsfcn=0.) : Functor(f), epsfcn(_epsfcn) {}
47 
48  // forward constructors
49  template<typename T0>
50  NumericalDiff(const T0& a0) : Functor(a0), epsfcn(0) {}
51  template<typename T0, typename T1>
52  NumericalDiff(const T0& a0, const T1& a1) : Functor(a0, a1), epsfcn(0) {}
53  template<typename T0, typename T1, typename T2>
54  NumericalDiff(const T0& a0, const T1& a1, const T2& a2) : Functor(a0, a1, a2), epsfcn(0) {}
55 
56  enum {
59  };
60 
64  int df(const InputType& _x, JacobianType &jac) const
65  {
66  using std::sqrt;
67  using std::abs;
68  /* Local variables */
69  Scalar h;
70  int nfev=0;
71  const typename InputType::Index n = _x.size();
73  ValueType val1, val2;
74  InputType x = _x;
75  // TODO : we should do this only if the size is not already known
76  val1.resize(Functor::values());
77  val2.resize(Functor::values());
78 
79  // initialization
80  switch(mode) {
81  case Forward:
82  // compute f(x)
83  Functor::operator()(x, val1); nfev++;
84  break;
85  case Central:
86  // do nothing
87  break;
88  default:
89  eigen_assert(false);
90  };
91 
92  // Function Body
93  for (int j = 0; j < n; ++j) {
94  h = eps * abs(x[j]);
95  if (h == 0.) {
96  h = eps;
97  }
98  switch(mode) {
99  case Forward:
100  x[j] += h;
101  Functor::operator()(x, val2);
102  nfev++;
103  x[j] = _x[j];
104  jac.col(j) = (val2-val1)/h;
105  break;
106  case Central:
107  x[j] += h;
108  Functor::operator()(x, val2); nfev++;
109  x[j] -= 2*h;
110  Functor::operator()(x, val1); nfev++;
111  x[j] = _x[j];
112  jac.col(j) = (val2-val1)/(2*h);
113  break;
114  default:
115  eigen_assert(false);
116  };
117  }
118  return nfev;
119  }
120 private:
122 
124 };
125 
126 } // end namespace Eigen
127 
128 //vim: ai ts=4 sts=4 et sw=4
129 #endif // EIGEN_NUMERICAL_DIFF_H
130 
Eigen::Forward
@ Forward
Definition: NumericalDiff.h:19
Eigen::Central
@ Central
Definition: NumericalDiff.h:20
Eigen::NumericalDiff::NumericalDiff
NumericalDiff(const T0 &a0)
Definition: NumericalDiff.h:50
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Functor::JacobianType
Matrix< Scalar, ValuesAtCompileTime, InputsAtCompileTime > JacobianType
Definition: NonLinearOptimization.cpp:126
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
Eigen::NumericalDiff::ValuesAtCompileTime
@ ValuesAtCompileTime
Definition: NumericalDiff.h:58
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
Eigen::NumericalDiffMode
NumericalDiffMode
Definition: NumericalDiff.h:18
h
const double h
Definition: testSimpleHelicopter.cpp:19
Functor::InputsAtCompileTime
@ InputsAtCompileTime
Definition: NonLinearOptimization.cpp:121
Eigen::NumericalDiff::epsfcn
Scalar epsfcn
Definition: NumericalDiff.h:121
Eigen::NumericalDiff::NumericalDiff
NumericalDiff(Scalar _epsfcn=0.)
Definition: NumericalDiff.h:45
n
int n
Definition: BiCGSTAB_simple.cpp:1
align_3::a1
Point2 a1
Definition: testPose2.cpp:769
Functor::Scalar
_Scalar Scalar
Definition: NonLinearOptimization.cpp:119
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
operator()
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
Definition: IndexedViewMethods.h:73
Eigen::NumericalDiff::InputType
Functor::InputType InputType
Definition: NumericalDiff.h:41
Eigen::NumericalDiff::df
int df(const InputType &_x, JacobianType &jac) const
Definition: NumericalDiff.h:64
Eigen::NumericalDiff
Definition: NumericalDiff.h:36
T2
static const Pose3 T2(Rot3::Rodrigues(0.3, 0.2, 0.1), P2)
Eigen::NumericalDiff::NumericalDiff
NumericalDiff(const T0 &a0, const T1 &a1)
Definition: NumericalDiff.h:52
Eigen::NumericalDiff::Functor
_Functor Functor
Definition: NumericalDiff.h:39
Functor::values
int values() const
Definition: NonLinearOptimization.cpp:134
Functor::InputType
Matrix< Scalar, InputsAtCompileTime, 1 > InputType
Definition: NonLinearOptimization.cpp:124
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
Eigen::NumericalDiff::InputsAtCompileTime
@ InputsAtCompileTime
Definition: NumericalDiff.h:57
Eigen::NumericalDiff::NumericalDiff
NumericalDiff(const Functor &f, Scalar _epsfcn=0.)
Definition: NumericalDiff.h:46
Functor::ValueType
Matrix< Scalar, ValuesAtCompileTime, 1 > ValueType
Definition: NonLinearOptimization.cpp:125
Eigen::NumericalDiff::NumericalDiff
NumericalDiff(const T0 &a0, const T1 &a1, const T2 &a2)
Definition: NumericalDiff.h:54
align_3::a2
Point2 a2
Definition: testPose2.cpp:770
Eigen::NumericalDiff::Scalar
Functor::Scalar Scalar
Definition: NumericalDiff.h:40
mode
static const DiscreteKey mode(modeKey, 2)
abs
#define abs(x)
Definition: datatypes.h:17
T1
static const Similarity3 T1(R, Point3(3.5, -8.2, 4.2), 1)
Eigen::NumericalDiff::ValueType
Functor::ValueType ValueType
Definition: NumericalDiff.h:42
max
#define max(a, b)
Definition: datatypes.h:20
Eigen::NumericalDiff::operator=
NumericalDiff & operator=(const NumericalDiff &)
Eigen::NumericalDiff::JacobianType
Functor::JacobianType JacobianType
Definition: NumericalDiff.h:43
Functor::ValuesAtCompileTime
@ ValuesAtCompileTime
Definition: NonLinearOptimization.cpp:122
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
ceres::sqrt
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Wed May 15 2024 15:20:23