forward_adolc.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) 2008 Gael Guennebaud <g.gael@free.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 #include <Eigen/Dense>
12 
13 #define NUMBER_DIRECTIONS 16
14 #include <unsupported/Eigen/AdolcForward>
15 
16 template<typename Vector>
18 {
19  typedef typename Vector::Scalar Scalar;
20  return (p-Vector(Scalar(-1),Scalar(1.))).norm() + (p.array().sqrt().abs() * p.array().sin()).sum() + p.dot(p);
21 }
22 
23 template<typename _Scalar, int NX=Dynamic, int NY=Dynamic>
24 struct TestFunc1
25 {
26  typedef _Scalar Scalar;
27  enum {
30  };
34 
35  int m_inputs, m_values;
36 
38  TestFunc1(int inputs_, int values_) : m_inputs(inputs_), m_values(values_) {}
39 
40  int inputs() const { return m_inputs; }
41  int values() const { return m_values; }
42 
43  template<typename T>
45  {
47 
48  v[0] = 2 * x[0] * x[0] + x[0] * x[1];
49  v[1] = 3 * x[1] * x[0] + 0.5 * x[1] * x[1];
50  if(inputs()>2)
51  {
52  v[0] += 0.5 * x[2];
53  v[1] += x[2];
54  }
55  if(values()>2)
56  {
57  v[2] = 3 * x[1] * x[0] * x[0];
58  }
59  if (inputs()>2 && values()>2)
60  v[2] *= x[2];
61  }
62 
63  void operator() (const InputType& x, ValueType* v, JacobianType* _j) const
64  {
65  (*this)(x, v);
66 
67  if(_j)
68  {
69  JacobianType& j = *_j;
70 
71  j(0,0) = 4 * x[0] + x[1];
72  j(1,0) = 3 * x[1];
73 
74  j(0,1) = x[0];
75  j(1,1) = 3 * x[0] + 2 * 0.5 * x[1];
76 
77  if (inputs()>2)
78  {
79  j(0,2) = 0.5;
80  j(1,2) = 1;
81  }
82  if(values()>2)
83  {
84  j(2,0) = 3 * x[1] * 2 * x[0];
85  j(2,1) = 3 * x[0] * x[0];
86  }
87  if (inputs()>2 && values()>2)
88  {
89  j(2,0) *= x[2];
90  j(2,1) *= x[2];
91 
92  j(2,2) = 3 * x[1] * x[0] * x[0];
93  j(2,2) = 3 * x[1] * x[0] * x[0];
94  }
95  }
96  }
97 };
98 
99 template<typename Func> void adolc_forward_jacobian(const Func& f)
100 {
101  typename Func::InputType x = Func::InputType::Random(f.inputs());
102  typename Func::ValueType y(f.values()), yref(f.values());
103  typename Func::JacobianType j(f.values(),f.inputs()), jref(f.values(),f.inputs());
104 
105  jref.setZero();
106  yref.setZero();
107  f(x,&yref,&jref);
108 // std::cerr << y.transpose() << "\n\n";;
109 // std::cerr << j << "\n\n";;
110 
111  j.setZero();
112  y.setZero();
113  AdolcForwardJacobian<Func> autoj(f);
114  autoj(x, &y, &j);
115 // std::cerr << y.transpose() << "\n\n";;
116 // std::cerr << j << "\n\n";;
117 
118  VERIFY_IS_APPROX(y, yref);
119  VERIFY_IS_APPROX(j, jref);
120 }
121 
122 EIGEN_DECLARE_TEST(forward_adolc)
123 {
124  adtl::setNumDir(NUMBER_DIRECTIONS);
125 
126  for(int i = 0; i < g_repeat; i++) {
132  }
133 
134  {
135  // simple instantiation tests
137  foo(x);
139  A.selfadjointView<Lower>().eigenvalues();
140  }
141 }
TestFunc1::values
int values() const
Definition: forward_adolc.cpp:41
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
TestFunc1::inputs
int inputs() const
Definition: forward_adolc.cpp:40
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(forward_adolc)
Definition: forward_adolc.cpp:122
foo
EIGEN_DONT_INLINE Vector::Scalar foo(const Vector &p)
Definition: forward_adolc.cpp:17
TestFunc1::ValueType
Matrix< Scalar, ValuesAtCompileTime, 1 > ValueType
Definition: forward_adolc.cpp:32
TestFunc1::ValuesAtCompileTime
@ ValuesAtCompileTime
Definition: autodiff.cpp:38
NUMBER_DIRECTIONS
#define NUMBER_DIRECTIONS
Definition: forward_adolc.cpp:13
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
TestFunc1
Definition: autodiff.cpp:33
TestFunc1::InputsAtCompileTime
@ InputsAtCompileTime
Definition: autodiff.cpp:37
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
TestFunc1::m_inputs
int m_inputs
Definition: autodiff.cpp:44
TestFunc1::InputType
Matrix< Scalar, InputsAtCompileTime, 1 > InputType
Definition: forward_adolc.cpp:31
TestFunc1::JacobianType
Matrix< Scalar, ValuesAtCompileTime, InputsAtCompileTime > JacobianType
Definition: forward_adolc.cpp:33
Eigen::Lower
@ Lower
Definition: Constants.h:209
y
Scalar * y
Definition: level1_cplx_impl.h:124
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
TestFunc1::TestFunc1
TestFunc1()
Definition: forward_adolc.cpp:37
main.h
TestFunc1::operator()
void operator()(const Matrix< T, InputsAtCompileTime, 1 > &x, Matrix< T, ValuesAtCompileTime, 1 > *_v) const
Definition: autodiff.cpp:53
p
float * p
Definition: Tutorial_Map_using.cpp:9
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
adolc_forward_jacobian
void adolc_forward_jacobian(const Func &f)
Definition: forward_adolc.cpp:99
TestFunc1::m_values
int m_values
Definition: autodiff.cpp:44
TestFunc1::Scalar
_Scalar Scalar
Definition: forward_adolc.cpp:26
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
ceres::Vector
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
Definition: gtsam/3rdparty/ceres/eigen.h:38
TestFunc1::TestFunc1
TestFunc1(int inputs_, int values_)
Definition: forward_adolc.cpp:38
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399
EIGEN_DONT_INLINE
#define EIGEN_DONT_INLINE
Definition: Macros.h:940


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:18