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