autodiff_scalar.cpp File Reference
#include "main.h"
#include <unsupported/Eigen/AutoDiff>

Functions

template<typename Scalar >
void check_atan2 ()
 
template<typename Scalar >
void check_hyperbolic_functions ()
 
template<typename Scalar >
void check_limits_specialization ()
 
 EIGEN_DECLARE_TEST (autodiff_scalar)
 

Function Documentation

◆ check_atan2()

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 }
AnnoyingScalar cos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:136
AnnoyingScalar atan2(const AnnoyingScalar &y, const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:139
AnnoyingScalar sin(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:137
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
SCALAR Scalar
Definition: bench_gemm.cpp:45
A scalar type replacement with automatic differentiation capability.
Definition: AutoDiffScalar.h:99
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
RealScalar s
Definition: level1_cplx_impl.h:130
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 exp(const bfloat16 &a)
Definition: BFloat16.h:615
r
Definition: UniformPSDSelfTest.py:20
int c
Definition: calibrate.py:100
list x
Definition: plotDoE.py:28

References atan2(), calibrate::c, cos(), Eigen::bfloat16_impl::exp(), UniformPSDSelfTest::r, res, s, sin(), VERIFY_IS_APPROX, and plotDoE::x.

◆ check_hyperbolic_functions()

template<typename Scalar >
void check_hyperbolic_functions ( )
40  {
41  using std::cosh;
42  using std::sinh;
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 }
float * p
Definition: Tutorial_Map_using.cpp:9
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 tanh(const bfloat16 &a)
Definition: BFloat16.h:639
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 cosh(const bfloat16 &a)
Definition: BFloat16.h:638
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 sinh(const bfloat16 &a)
Definition: BFloat16.h:637
val
Definition: calibrate.py:119

References Eigen::bfloat16_impl::cosh(), p, Eigen::bfloat16_impl::sinh(), Eigen::bfloat16_impl::tanh(), calibrate::val, and VERIFY_IS_APPROX.

◆ check_limits_specialization()

template<typename Scalar >
void check_limits_specialization ( )
76  {
77  typedef Eigen::Matrix<Scalar, 1, 1> Deriv;
79 
80  typedef std::numeric_limits<AD> A;
81  typedef std::numeric_limits<Scalar> B;
82 
83  // workaround "unused typedef" warning:
85 
87 }
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:48
#define VERIFY(a)
Definition: main.h:362
squared absolute value
Definition: GlobalFunctions.h:87

References Eigen::value, and VERIFY.

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( autodiff_scalar  )
89  {
90  for (int i = 0; i < g_repeat; i++) {
91  CALL_SUBTEST_1(check_atan2<float>());
92  CALL_SUBTEST_2(check_atan2<double>());
93  CALL_SUBTEST_3(check_hyperbolic_functions<float>());
94  CALL_SUBTEST_4(check_hyperbolic_functions<double>());
95  CALL_SUBTEST_5(check_limits_specialization<double>());
96  }
97 }
int i
Definition: BiCGSTAB_step_by_step.cpp:9
static int g_repeat
Definition: main.h:191
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22

References CALL_SUBTEST_1, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, CALL_SUBTEST_5, Eigen::g_repeat, and i.