NumericalDiff.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 Thomas Capricelli <orzel@freehackers.org>
5 
6 #include <stdio.h>
7 
8 #include "main.h"
9 #include <unsupported/Eigen/NumericalDiff>
10 
11 // Generic functor
12 template<typename _Scalar, int NX=Dynamic, int NY=Dynamic>
13 struct Functor
14 {
15  typedef _Scalar Scalar;
16  enum {
19  };
23 
25 
27  Functor(int inputs_, int values_) : m_inputs(inputs_), m_values(values_) {}
28 
29  int inputs() const { return m_inputs; }
30  int values() const { return m_values; }
31 
32 };
33 
34 struct my_functor : Functor<double>
35 {
36  my_functor(void): Functor<double>(3,15) {}
37  int operator()(const VectorXd &x, VectorXd &fvec) const
38  {
39  double tmp1, tmp2, tmp3;
40  double y[15] = {1.4e-1, 1.8e-1, 2.2e-1, 2.5e-1, 2.9e-1, 3.2e-1, 3.5e-1,
41  3.9e-1, 3.7e-1, 5.8e-1, 7.3e-1, 9.6e-1, 1.34, 2.1, 4.39};
42 
43  for (int i = 0; i < values(); i++)
44  {
45  tmp1 = i+1;
46  tmp2 = 16 - i - 1;
47  tmp3 = (i>=8)? tmp2 : tmp1;
48  fvec[i] = y[i] - (x[0] + tmp1/(x[1]*tmp2 + x[2]*tmp3));
49  }
50  return 0;
51  }
52 
53  int actual_df(const VectorXd &x, MatrixXd &fjac) const
54  {
55  double tmp1, tmp2, tmp3, tmp4;
56  for (int i = 0; i < values(); i++)
57  {
58  tmp1 = i+1;
59  tmp2 = 16 - i - 1;
60  tmp3 = (i>=8)? tmp2 : tmp1;
61  tmp4 = (x[1]*tmp2 + x[2]*tmp3); tmp4 = tmp4*tmp4;
62  fjac(i,0) = -1;
63  fjac(i,1) = tmp1*tmp2/tmp4;
64  fjac(i,2) = tmp1*tmp3/tmp4;
65  }
66  return 0;
67  }
68 };
69 
71 {
72  VectorXd x(3);
73  MatrixXd jac(15,3);
74  MatrixXd actual_jac(15,3);
75  my_functor functor;
76 
77  x << 0.082, 1.13, 2.35;
78 
79  // real one
80  functor.actual_df(x, actual_jac);
81 // std::cout << actual_jac << std::endl << std::endl;
82 
83  // using NumericalDiff
84  NumericalDiff<my_functor> numDiff(functor);
85  numDiff.df(x, jac);
86 // std::cout << jac << std::endl;
87 
88  VERIFY_IS_APPROX(jac, actual_jac);
89 }
90 
92 {
93  VectorXd x(3);
94  MatrixXd jac(15,3);
95  MatrixXd actual_jac(15,3);
96  my_functor functor;
97 
98  x << 0.082, 1.13, 2.35;
99 
100  // real one
101  functor.actual_df(x, actual_jac);
102 
103  // using NumericalDiff
104  NumericalDiff<my_functor,Central> numDiff(functor);
105  numDiff.df(x, jac);
106 
107  VERIFY_IS_APPROX(jac, actual_jac);
108 }
109 
111 {
114 }
Functor::JacobianType
Matrix< Scalar, ValuesAtCompileTime, InputsAtCompileTime > JacobianType
Definition: NumericalDiff.cpp:22
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(NumericalDiff)
Definition: NumericalDiff.cpp:110
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
my_functor::actual_df
int actual_df(const VectorXd &x, MatrixXd &fjac) const
Definition: NumericalDiff.cpp:53
test_forward
void test_forward()
Definition: NumericalDiff.cpp:70
Functor::InputsAtCompileTime
@ InputsAtCompileTime
Definition: NonLinearOptimization.cpp:121
Functor::Functor
Functor()
Definition: NumericalDiff.cpp:26
Functor::Scalar
_Scalar Scalar
Definition: NumericalDiff.cpp:15
Functor
Definition: NonLinearOptimization.cpp:117
Functor::m_values
int m_values
Definition: NumericalDiff.cpp:24
Eigen::NumericalDiff::df
int df(const InputType &_x, JacobianType &jac) const
Definition: NumericalDiff.h:64
Eigen::NumericalDiff
Definition: NumericalDiff.h:36
Functor::inputs
int inputs() const
Definition: NumericalDiff.cpp:29
Functor::values
int values() const
Definition: NumericalDiff.cpp:30
Functor::InputType
Matrix< Scalar, InputsAtCompileTime, 1 > InputType
Definition: NumericalDiff.cpp:20
y
Scalar * y
Definition: level1_cplx_impl.h:124
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
my_functor::operator()
int operator()(const VectorXd &x, VectorXd &fvec) const
Definition: NumericalDiff.cpp:37
my_functor
Definition: NumericalDiff.cpp:34
Functor::ValueType
Matrix< Scalar, ValuesAtCompileTime, 1 > ValueType
Definition: NumericalDiff.cpp:21
main.h
Functor::m_inputs
const int m_inputs
Definition: NonLinearOptimization.cpp:128
my_functor::my_functor
my_functor(void)
Definition: NumericalDiff.cpp:36
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
test_central
void test_central()
Definition: NumericalDiff.cpp:91
Functor::m_values
const int m_values
Definition: NonLinearOptimization.cpp:128
Functor::ValuesAtCompileTime
@ ValuesAtCompileTime
Definition: NonLinearOptimization.cpp:122
Functor::Functor
Functor(int inputs_, int values_)
Definition: NumericalDiff.cpp:27
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:12:24