MathFunctionsImpl.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2014 Pedro Gonnet (pedro.gonnet@gmail.com)
  5. // Copyright (C) 2016 Gael Guennebaud <gael.guennebaud@inria.fr>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #ifndef EIGEN_MATHFUNCTIONSIMPL_H
  11. #define EIGEN_MATHFUNCTIONSIMPL_H
  12. namespace Eigen {
  13. namespace internal {
  14. /** \internal \returns the hyperbolic tan of \a a (coeff-wise)
  15. Doesn't do anything fancy, just a 13/6-degree rational interpolant which
  16. is accurate up to a couple of ulp in the range [-9, 9], outside of which
  17. the tanh(x) = +/-1.
  18. This implementation works on both scalars and packets.
  19. */
  20. template<typename T>
  21. T generic_fast_tanh_float(const T& a_x)
  22. {
  23. // Clamp the inputs to the range [-9, 9] since anything outside
  24. // this range is +/-1.0f in single-precision.
  25. const T plus_9 = pset1<T>(9.f);
  26. const T minus_9 = pset1<T>(-9.f);
  27. // NOTE GCC prior to 6.3 might improperly optimize this max/min
  28. // step such that if a_x is nan, x will be either 9 or -9,
  29. // and tanh will return 1 or -1 instead of nan.
  30. // This is supposed to be fixed in gcc6.3,
  31. // see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=72867
  32. const T x = pmax(minus_9,pmin(plus_9,a_x));
  33. // The monomial coefficients of the numerator polynomial (odd).
  34. const T alpha_1 = pset1<T>(4.89352455891786e-03f);
  35. const T alpha_3 = pset1<T>(6.37261928875436e-04f);
  36. const T alpha_5 = pset1<T>(1.48572235717979e-05f);
  37. const T alpha_7 = pset1<T>(5.12229709037114e-08f);
  38. const T alpha_9 = pset1<T>(-8.60467152213735e-11f);
  39. const T alpha_11 = pset1<T>(2.00018790482477e-13f);
  40. const T alpha_13 = pset1<T>(-2.76076847742355e-16f);
  41. // The monomial coefficients of the denominator polynomial (even).
  42. const T beta_0 = pset1<T>(4.89352518554385e-03f);
  43. const T beta_2 = pset1<T>(2.26843463243900e-03f);
  44. const T beta_4 = pset1<T>(1.18534705686654e-04f);
  45. const T beta_6 = pset1<T>(1.19825839466702e-06f);
  46. // Since the polynomials are odd/even, we need x^2.
  47. const T x2 = pmul(x, x);
  48. // Evaluate the numerator polynomial p.
  49. T p = pmadd(x2, alpha_13, alpha_11);
  50. p = pmadd(x2, p, alpha_9);
  51. p = pmadd(x2, p, alpha_7);
  52. p = pmadd(x2, p, alpha_5);
  53. p = pmadd(x2, p, alpha_3);
  54. p = pmadd(x2, p, alpha_1);
  55. p = pmul(x, p);
  56. // Evaluate the denominator polynomial p.
  57. T q = pmadd(x2, beta_6, beta_4);
  58. q = pmadd(x2, q, beta_2);
  59. q = pmadd(x2, q, beta_0);
  60. // Divide the numerator by the denominator.
  61. return pdiv(p, q);
  62. }
  63. template<typename RealScalar>
  64. EIGEN_STRONG_INLINE
  65. RealScalar positive_real_hypot(const RealScalar& x, const RealScalar& y)
  66. {
  67. EIGEN_USING_STD_MATH(sqrt);
  68. RealScalar p, qp;
  69. p = numext::maxi(x,y);
  70. if(p==RealScalar(0)) return RealScalar(0);
  71. qp = numext::mini(y,x) / p;
  72. return p * sqrt(RealScalar(1) + qp*qp);
  73. }
  74. template<typename Scalar>
  75. struct hypot_impl
  76. {
  77. typedef typename NumTraits<Scalar>::Real RealScalar;
  78. static inline RealScalar run(const Scalar& x, const Scalar& y)
  79. {
  80. EIGEN_USING_STD_MATH(abs);
  81. return positive_real_hypot<RealScalar>(abs(x), abs(y));
  82. }
  83. };
  84. } // end namespace internal
  85. } // end namespace Eigen
  86. #endif // EIGEN_MATHFUNCTIONSIMPL_H