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 }
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
SCALAR Scalar
Definition: bench_gemm.cpp:46
#define CALL_SUBTEST_4(FUNC)
Jet< T, N > cos(const Jet< T, N > &f)
Definition: jet.h:426
A scalar type replacement with automatic differentiation capability.
#define CALL_SUBTEST_3(FUNC)
void check_hyperbolic_functions()
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
EIGEN_DEVICE_FUNC const TanhReturnType tanh() const
Jet< T, N > sin(const Jet< T, N > &f)
Definition: jet.h:439
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
EIGEN_DEVICE_FUNC const SinhReturnType sinh() const
EIGEN_DEVICE_FUNC const ExpReturnType exp() const
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
#define VERIFY_IS_APPROX(a, b)
EIGEN_DECLARE_TEST(autodiff_scalar)
#define CALL_SUBTEST_1(FUNC)
static int g_repeat
Definition: main.h:169
RealScalar s
void check_limits_specialization()
AnnoyingScalar atan2(const AnnoyingScalar &y, const AnnoyingScalar &x)
void check_atan2()
#define CALL_SUBTEST_5(FUNC)
#define VERIFY(a)
Definition: main.h:380
float * p
#define CALL_SUBTEST_2(FUNC)
The matrix class, also used for vectors and row-vectors.
EIGEN_DEVICE_FUNC const CoshReturnType cosh() const
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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:33:56