autodiff_scalar.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) 2013 Christoph Hertzberg <chtz@informatik.uni-bremen.de>
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 <unsupported/Eigen/AutoDiff>
12 
13 /*
14  * In this file scalar derivations are tested for correctness.
15  * TODO add more tests!
16  */
17 
18 template<typename Scalar> void check_atan2()
19 {
20  typedef Matrix<Scalar, 1, 1> Deriv1;
21  typedef AutoDiffScalar<Deriv1> AD;
22 
23  AD x(internal::random<Scalar>(-3.0, 3.0), Deriv1::UnitX());
24 
25  using std::exp;
26  Scalar r = exp(internal::random<Scalar>(-10, 10));
27 
28  AD s = sin(x), c = cos(x);
29  AD res = atan2(r*s, r*c);
30 
31  VERIFY_IS_APPROX(res.value(), x.value());
32  VERIFY_IS_APPROX(res.derivatives(), x.derivatives());
33 
34  res = atan2(r*s+0, r*c+0);
35  VERIFY_IS_APPROX(res.value(), x.value());
36  VERIFY_IS_APPROX(res.derivatives(), x.derivatives());
37 }
38 
39 template<typename Scalar> void check_hyperbolic_functions()
40 {
41  using std::sinh;
42  using std::cosh;
43  using std::tanh;
44  typedef Matrix<Scalar, 1, 1> Deriv1;
45  typedef AutoDiffScalar<Deriv1> AD;
46  Deriv1 p = Deriv1::Random();
47  AD val(p.x(),Deriv1::UnitX());
48 
49  Scalar cosh_px = std::cosh(p.x());
50  AD res1 = tanh(val);
51  VERIFY_IS_APPROX(res1.value(), std::tanh(p.x()));
52  VERIFY_IS_APPROX(res1.derivatives().x(), Scalar(1.0) / (cosh_px * cosh_px));
53 
54  AD res2 = sinh(val);
55  VERIFY_IS_APPROX(res2.value(), std::sinh(p.x()));
56  VERIFY_IS_APPROX(res2.derivatives().x(), cosh_px);
57 
58  AD res3 = cosh(val);
59  VERIFY_IS_APPROX(res3.value(), cosh_px);
60  VERIFY_IS_APPROX(res3.derivatives().x(), std::sinh(p.x()));
61 
62  // Check constant values.
63  const Scalar sample_point = Scalar(1) / Scalar(3);
64  val = AD(sample_point,Deriv1::UnitX());
65  res1 = tanh(val);
66  VERIFY_IS_APPROX(res1.derivatives().x(), Scalar(0.896629559604914));
67 
68  res2 = sinh(val);
69  VERIFY_IS_APPROX(res2.derivatives().x(), Scalar(1.056071867829939));
70 
71  res3 = cosh(val);
72  VERIFY_IS_APPROX(res3.derivatives().x(), Scalar(0.339540557256150));
73 }
74 
75 template <typename Scalar>
77 {
78  typedef Eigen::Matrix<Scalar, 1, 1> Deriv;
80 
81  typedef std::numeric_limits<AD> A;
82  typedef std::numeric_limits<Scalar> B;
83 
84  // workaround "unused typedef" warning:
86 
87 #if EIGEN_HAS_CXX11
89 #endif
90 }
91 
92 EIGEN_DECLARE_TEST(autodiff_scalar)
93 {
94  for(int i = 0; i < g_repeat; i++) {
95  CALL_SUBTEST_1( check_atan2<float>() );
96  CALL_SUBTEST_2( check_atan2<double>() );
97  CALL_SUBTEST_3( check_hyperbolic_functions<float>() );
98  CALL_SUBTEST_4( check_hyperbolic_functions<double>() );
99  CALL_SUBTEST_5( check_limits_specialization<double>());
100  }
101 }
check_limits_specialization
void check_limits_specialization()
Definition: autodiff_scalar.cpp:76
B
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
Eigen::AutoDiffScalar
A scalar type replacement with automatic differentiation capability.
Definition: AutoDiffScalar.h:33
s
RealScalar s
Definition: level1_cplx_impl.h:126
ceres::sin
Jet< T, N > sin(const Jet< T, N > &f)
Definition: jet.h:439
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
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
check_atan2
void check_atan2()
Definition: autodiff_scalar.cpp:18
res
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
exp
const EIGEN_DEVICE_FUNC ExpReturnType exp() const
Definition: ArrayCwiseUnaryOps.h:97
ceres::cos
Jet< T, N > cos(const Jet< T, N > &f)
Definition: jet.h:426
AD
static double AD[8]
Definition: airy.c:83
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
cosh
const EIGEN_DEVICE_FUNC CoshReturnType cosh() const
Definition: ArrayCwiseUnaryOps.h:353
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
check_hyperbolic_functions
void check_hyperbolic_functions()
Definition: autodiff_scalar.cpp:39
atan2
AnnoyingScalar atan2(const AnnoyingScalar &y, const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:110
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
tanh
const EIGEN_DEVICE_FUNC TanhReturnType tanh() const
Definition: ArrayCwiseUnaryOps.h:325
main.h
p
float * p
Definition: Tutorial_Map_using.cpp:9
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
sinh
const EIGEN_DEVICE_FUNC SinhReturnType sinh() const
Definition: ArrayCwiseUnaryOps.h:339
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(autodiff_scalar)
Definition: autodiff_scalar.cpp:92
VERIFY
#define VERIFY(a)
Definition: main.h:380


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:01:52