polygamma.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2013 Nikhar Agrawal
  3. // Copyright 2013 Christopher Kormanyos
  4. // Copyright 2014 John Maddock
  5. // Copyright 2013 Paul Bristow
  6. // Distributed under the Boost
  7. // Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef _BOOST_POLYGAMMA_2013_07_30_HPP_
  10. #define _BOOST_POLYGAMMA_2013_07_30_HPP_
  11. #include <boost/math/special_functions/factorials.hpp>
  12. #include <boost/math/special_functions/detail/polygamma.hpp>
  13. #include <boost/math/special_functions/trigamma.hpp>
  14. namespace boost { namespace math {
  15. template<class T, class Policy>
  16. inline typename tools::promote_args<T>::type polygamma(const int n, T x, const Policy& pol)
  17. {
  18. //
  19. // Filter off special cases right at the start:
  20. //
  21. if(n == 0)
  22. return boost::math::digamma(x, pol);
  23. if(n == 1)
  24. return boost::math::trigamma(x, pol);
  25. //
  26. // We've found some standard library functions to misbehave if any FPU exception flags
  27. // are set prior to their call, this code will clear those flags, then reset them
  28. // on exit:
  29. //
  30. BOOST_FPU_EXCEPTION_GUARD
  31. //
  32. // The type of the result - the common type of T and U after
  33. // any integer types have been promoted to double:
  34. //
  35. typedef typename tools::promote_args<T>::type result_type;
  36. //
  37. // The type used for the calculation. This may be a wider type than
  38. // the result in order to ensure full precision:
  39. //
  40. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  41. //
  42. // The type of the policy to forward to the actual implementation.
  43. // We disable promotion of float and double as that's [possibly]
  44. // happened already in the line above. Also reset to the default
  45. // any policies we don't use (reduces code bloat if we're called
  46. // multiple times with differing policies we don't actually use).
  47. // Also normalise the type, again to reduce code bloat in case we're
  48. // called multiple times with functionally identical policies that happen
  49. // to be different types.
  50. //
  51. typedef typename policies::normalise<
  52. Policy,
  53. policies::promote_float<false>,
  54. policies::promote_double<false>,
  55. policies::discrete_quantile<>,
  56. policies::assert_undefined<> >::type forwarding_policy;
  57. //
  58. // Whew. Now we can make the actual call to the implementation.
  59. // Arguments are explicitly cast to the evaluation type, and the result
  60. // passed through checked_narrowing_cast which handles things like overflow
  61. // according to the policy passed:
  62. //
  63. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  64. detail::polygamma_imp(n, static_cast<value_type>(x), forwarding_policy()),
  65. "boost::math::polygamma<%1%>(int, %1%)");
  66. }
  67. template<class T>
  68. inline typename tools::promote_args<T>::type polygamma(const int n, T x)
  69. {
  70. return boost::math::polygamma(n, x, policies::policy<>());
  71. }
  72. } } // namespace boost::math
  73. #endif // _BOOST_BERNOULLI_2013_05_30_HPP_