autodiff_scalar.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include "main.h"
  10. #include <unsupported/Eigen/AutoDiff>
  11. /*
  12. * In this file scalar derivations are tested for correctness.
  13. * TODO add more tests!
  14. */
  15. template<typename Scalar> void check_atan2()
  16. {
  17. typedef Matrix<Scalar, 1, 1> Deriv1;
  18. typedef AutoDiffScalar<Deriv1> AD;
  19. AD x(internal::random<Scalar>(-3.0, 3.0), Deriv1::UnitX());
  20. using std::exp;
  21. Scalar r = exp(internal::random<Scalar>(-10, 10));
  22. AD s = sin(x), c = cos(x);
  23. AD res = atan2(r*s, r*c);
  24. VERIFY_IS_APPROX(res.value(), x.value());
  25. VERIFY_IS_APPROX(res.derivatives(), x.derivatives());
  26. res = atan2(r*s+0, r*c+0);
  27. VERIFY_IS_APPROX(res.value(), x.value());
  28. VERIFY_IS_APPROX(res.derivatives(), x.derivatives());
  29. }
  30. template<typename Scalar> void check_hyperbolic_functions()
  31. {
  32. using std::sinh;
  33. using std::cosh;
  34. using std::tanh;
  35. typedef Matrix<Scalar, 1, 1> Deriv1;
  36. typedef AutoDiffScalar<Deriv1> AD;
  37. Deriv1 p = Deriv1::Random();
  38. AD val(p.x(),Deriv1::UnitX());
  39. Scalar cosh_px = std::cosh(p.x());
  40. AD res1 = tanh(val);
  41. VERIFY_IS_APPROX(res1.value(), std::tanh(p.x()));
  42. VERIFY_IS_APPROX(res1.derivatives().x(), Scalar(1.0) / (cosh_px * cosh_px));
  43. AD res2 = sinh(val);
  44. VERIFY_IS_APPROX(res2.value(), std::sinh(p.x()));
  45. VERIFY_IS_APPROX(res2.derivatives().x(), cosh_px);
  46. AD res3 = cosh(val);
  47. VERIFY_IS_APPROX(res3.value(), cosh_px);
  48. VERIFY_IS_APPROX(res3.derivatives().x(), std::sinh(p.x()));
  49. // Check constant values.
  50. const Scalar sample_point = Scalar(1) / Scalar(3);
  51. val = AD(sample_point,Deriv1::UnitX());
  52. res1 = tanh(val);
  53. VERIFY_IS_APPROX(res1.derivatives().x(), Scalar(0.896629559604914));
  54. res2 = sinh(val);
  55. VERIFY_IS_APPROX(res2.derivatives().x(), Scalar(1.056071867829939));
  56. res3 = cosh(val);
  57. VERIFY_IS_APPROX(res3.derivatives().x(), Scalar(0.339540557256150));
  58. }
  59. template <typename Scalar>
  60. void check_limits_specialization()
  61. {
  62. typedef Eigen::Matrix<Scalar, 1, 1> Deriv;
  63. typedef Eigen::AutoDiffScalar<Deriv> AD;
  64. typedef std::numeric_limits<AD> A;
  65. typedef std::numeric_limits<Scalar> B;
  66. // workaround "unused typedef" warning:
  67. VERIFY(!bool(internal::is_same<B, A>::value));
  68. #if EIGEN_HAS_CXX11
  69. VERIFY(bool(std::is_base_of<B, A>::value));
  70. #endif
  71. }
  72. EIGEN_DECLARE_TEST(autodiff_scalar)
  73. {
  74. for(int i = 0; i < g_repeat; i++) {
  75. CALL_SUBTEST_1( check_atan2<float>() );
  76. CALL_SUBTEST_2( check_atan2<double>() );
  77. CALL_SUBTEST_3( check_hyperbolic_functions<float>() );
  78. CALL_SUBTEST_4( check_hyperbolic_functions<double>() );
  79. CALL_SUBTEST_5( check_limits_specialization<double>());
  80. }
  81. }