• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
check_atan2()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 
check_hyperbolic_functions()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>
check_limits_specialization()76 void check_limits_specialization()
77 {
78   typedef Eigen::Matrix<Scalar, 1, 1> Deriv;
79   typedef Eigen::AutoDiffScalar<Deriv> AD;
80 
81   typedef std::numeric_limits<AD> A;
82   typedef std::numeric_limits<Scalar> B;
83 
84 #if EIGEN_HAS_CXX11
85   VERIFY(bool(std::is_base_of<B, A>::value));
86 #endif
87 }
88 
test_autodiff_scalar()89 void test_autodiff_scalar()
90 {
91   for(int i = 0; i < g_repeat; i++) {
92     CALL_SUBTEST_1( check_atan2<float>() );
93     CALL_SUBTEST_2( check_atan2<double>() );
94     CALL_SUBTEST_3( check_hyperbolic_functions<float>() );
95     CALL_SUBTEST_4( check_hyperbolic_functions<double>() );
96     CALL_SUBTEST_5( check_limits_specialization<double>());
97   }
98 }
99