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_DETAIL_2013_07_30_HPP_
|
Chris@102
|
12 #define _BOOST_POLYGAMMA_DETAIL_2013_07_30_HPP_
|
Chris@102
|
13
|
Chris@102
|
14 #include <cmath>
|
Chris@102
|
15 #include <limits>
|
Chris@102
|
16 #include <boost/cstdint.hpp>
|
Chris@102
|
17 #include <boost/math/policies/policy.hpp>
|
Chris@102
|
18 #include <boost/math/special_functions/bernoulli.hpp>
|
Chris@102
|
19 #include <boost/math/special_functions/trunc.hpp>
|
Chris@102
|
20 #include <boost/math/special_functions/zeta.hpp>
|
Chris@102
|
21 #include <boost/math/special_functions/digamma.hpp>
|
Chris@102
|
22 #include <boost/math/special_functions/sin_pi.hpp>
|
Chris@102
|
23 #include <boost/math/special_functions/cos_pi.hpp>
|
Chris@102
|
24 #include <boost/math/special_functions/pow.hpp>
|
Chris@102
|
25 #include <boost/mpl/if.hpp>
|
Chris@102
|
26 #include <boost/mpl/int.hpp>
|
Chris@102
|
27 #include <boost/static_assert.hpp>
|
Chris@102
|
28 #include <boost/type_traits/is_convertible.hpp>
|
Chris@102
|
29
|
Chris@102
|
30 namespace boost { namespace math { namespace detail{
|
Chris@102
|
31
|
Chris@102
|
32 template<class T, class Policy>
|
Chris@102
|
33 T polygamma_atinfinityplus(const int n, const T& x, const Policy& pol, const char* function) // for large values of x such as for x> 400
|
Chris@102
|
34 {
|
Chris@102
|
35 // See http://functions.wolfram.com/GammaBetaErf/PolyGamma2/06/02/0001/
|
Chris@102
|
36 BOOST_MATH_STD_USING
|
Chris@102
|
37 //
|
Chris@102
|
38 // sum == current value of accumulated sum.
|
Chris@102
|
39 // term == value of current term to be added to sum.
|
Chris@102
|
40 // part_term == value of current term excluding the Bernoulli number part
|
Chris@102
|
41 //
|
Chris@102
|
42 if(n + x == x)
|
Chris@102
|
43 {
|
Chris@102
|
44 // x is crazy large, just concentrate on the first part of the expression and use logs:
|
Chris@102
|
45 if(n == 1) return 1 / x;
|
Chris@102
|
46 T nlx = n * log(x);
|
Chris@102
|
47 if((nlx < tools::log_max_value<T>()) && (n < max_factorial<T>::value))
|
Chris@102
|
48 return ((n & 1) ? 1 : -1) * boost::math::factorial<T>(n - 1) * pow(x, -n);
|
Chris@102
|
49 else
|
Chris@102
|
50 return ((n & 1) ? 1 : -1) * exp(boost::math::lgamma(T(n), pol) - n * log(x));
|
Chris@102
|
51 }
|
Chris@102
|
52 T term, sum, part_term;
|
Chris@102
|
53 T x_squared = x * x;
|
Chris@102
|
54 //
|
Chris@102
|
55 // Start by setting part_term to:
|
Chris@102
|
56 //
|
Chris@102
|
57 // (n-1)! / x^(n+1)
|
Chris@102
|
58 //
|
Chris@102
|
59 // which is common to both the first term of the series (with k = 1)
|
Chris@102
|
60 // and to the leading part.
|
Chris@102
|
61 // We can then get to the leading term by:
|
Chris@102
|
62 //
|
Chris@102
|
63 // part_term * (n + 2 * x) / 2
|
Chris@102
|
64 //
|
Chris@102
|
65 // and to the first term in the series
|
Chris@102
|
66 // (excluding the Bernoulli number) by:
|
Chris@102
|
67 //
|
Chris@102
|
68 // part_term n * (n + 1) / (2x)
|
Chris@102
|
69 //
|
Chris@102
|
70 // If either the factorial would overflow,
|
Chris@102
|
71 // or the power term underflows, this just gets set to 0 and then we
|
Chris@102
|
72 // know that we have to use logs for the initial terms:
|
Chris@102
|
73 //
|
Chris@102
|
74 part_term = ((n > boost::math::max_factorial<T>::value) && (T(n) * n > tools::log_max_value<T>()))
|
Chris@102
|
75 ? T(0) : static_cast<T>(boost::math::factorial<T>(n - 1, pol) * pow(x, -n - 1));
|
Chris@102
|
76 if(part_term == 0)
|
Chris@102
|
77 {
|
Chris@102
|
78 // Either n is very large, or the power term underflows,
|
Chris@102
|
79 // set the initial values of part_term, term and sum via logs:
|
Chris@102
|
80 part_term = boost::math::lgamma(n, pol) - (n + 1) * log(x);
|
Chris@102
|
81 sum = exp(part_term + log(n + 2 * x) - boost::math::constants::ln_two<T>());
|
Chris@102
|
82 part_term += log(T(n) * (n + 1)) - boost::math::constants::ln_two<T>() - log(x);
|
Chris@102
|
83 part_term = exp(part_term);
|
Chris@102
|
84 }
|
Chris@102
|
85 else
|
Chris@102
|
86 {
|
Chris@102
|
87 sum = part_term * (n + 2 * x) / 2;
|
Chris@102
|
88 part_term *= (T(n) * (n + 1)) / 2;
|
Chris@102
|
89 part_term /= x;
|
Chris@102
|
90 }
|
Chris@102
|
91 //
|
Chris@102
|
92 // If the leading term is 0, so is the result:
|
Chris@102
|
93 //
|
Chris@102
|
94 if(sum == 0)
|
Chris@102
|
95 return sum;
|
Chris@102
|
96
|
Chris@102
|
97 for(unsigned k = 1;;)
|
Chris@102
|
98 {
|
Chris@102
|
99 term = part_term * boost::math::bernoulli_b2n<T>(k, pol);
|
Chris@102
|
100 sum += term;
|
Chris@102
|
101 //
|
Chris@102
|
102 // Normal termination condition:
|
Chris@102
|
103 //
|
Chris@102
|
104 if(fabs(term / sum) < tools::epsilon<T>())
|
Chris@102
|
105 break;
|
Chris@102
|
106 //
|
Chris@102
|
107 // Increment our counter, and move part_term on to the next value:
|
Chris@102
|
108 //
|
Chris@102
|
109 ++k;
|
Chris@102
|
110 part_term *= T(n + 2 * k - 2) * (n - 1 + 2 * k);
|
Chris@102
|
111 part_term /= (2 * k - 1) * 2 * k;
|
Chris@102
|
112 part_term /= x_squared;
|
Chris@102
|
113 //
|
Chris@102
|
114 // Emergency get out termination condition:
|
Chris@102
|
115 //
|
Chris@102
|
116 if(k > policies::get_max_series_iterations<Policy>())
|
Chris@102
|
117 {
|
Chris@102
|
118 return policies::raise_evaluation_error(function, "Series did not converge, closest value was %1%", sum, pol);
|
Chris@102
|
119 }
|
Chris@102
|
120 }
|
Chris@102
|
121
|
Chris@102
|
122 if((n - 1) & 1)
|
Chris@102
|
123 sum = -sum;
|
Chris@102
|
124
|
Chris@102
|
125 return sum;
|
Chris@102
|
126 }
|
Chris@102
|
127
|
Chris@102
|
128 template<class T, class Policy>
|
Chris@102
|
129 T polygamma_attransitionplus(const int n, const T& x, const Policy& pol, const char* function)
|
Chris@102
|
130 {
|
Chris@102
|
131 // See: http://functions.wolfram.com/GammaBetaErf/PolyGamma2/16/01/01/0017/
|
Chris@102
|
132
|
Chris@102
|
133 // Use N = (0.4 * digits) + (4 * n) for target value for x:
|
Chris@102
|
134 BOOST_MATH_STD_USING
|
Chris@102
|
135 const int d4d = static_cast<int>(0.4F * policies::digits_base10<T, Policy>());
|
Chris@102
|
136 const int N = d4d + (4 * n);
|
Chris@102
|
137 const int m = n;
|
Chris@102
|
138 const int iter = N - itrunc(x);
|
Chris@102
|
139
|
Chris@102
|
140 if(iter > (int)policies::get_max_series_iterations<Policy>())
|
Chris@102
|
141 return policies::raise_evaluation_error<T>(function, ("Exceeded maximum series evaluations evaluating at n = " + boost::lexical_cast<std::string>(n) + " and x = %1%").c_str(), x, pol);
|
Chris@102
|
142
|
Chris@102
|
143 const int minus_m_minus_one = -m - 1;
|
Chris@102
|
144
|
Chris@102
|
145 T z(x);
|
Chris@102
|
146 T sum0(0);
|
Chris@102
|
147 T z_plus_k_pow_minus_m_minus_one(0);
|
Chris@102
|
148
|
Chris@102
|
149 // Forward recursion to larger x, need to check for overflow first though:
|
Chris@102
|
150 if(log(z + iter) * minus_m_minus_one > -tools::log_max_value<T>())
|
Chris@102
|
151 {
|
Chris@102
|
152 for(int k = 1; k <= iter; ++k)
|
Chris@102
|
153 {
|
Chris@102
|
154 z_plus_k_pow_minus_m_minus_one = pow(z, minus_m_minus_one);
|
Chris@102
|
155 sum0 += z_plus_k_pow_minus_m_minus_one;
|
Chris@102
|
156 z += 1;
|
Chris@102
|
157 }
|
Chris@102
|
158 sum0 *= boost::math::factorial<T>(n);
|
Chris@102
|
159 }
|
Chris@102
|
160 else
|
Chris@102
|
161 {
|
Chris@102
|
162 for(int k = 1; k <= iter; ++k)
|
Chris@102
|
163 {
|
Chris@102
|
164 T log_term = log(z) * minus_m_minus_one + boost::math::lgamma(T(n + 1), pol);
|
Chris@102
|
165 sum0 += exp(log_term);
|
Chris@102
|
166 z += 1;
|
Chris@102
|
167 }
|
Chris@102
|
168 }
|
Chris@102
|
169 if((n - 1) & 1)
|
Chris@102
|
170 sum0 = -sum0;
|
Chris@102
|
171
|
Chris@102
|
172 return sum0 + polygamma_atinfinityplus(n, z, pol, function);
|
Chris@102
|
173 }
|
Chris@102
|
174
|
Chris@102
|
175 template <class T, class Policy>
|
Chris@102
|
176 T polygamma_nearzero(int n, T x, const Policy& pol, const char* function)
|
Chris@102
|
177 {
|
Chris@102
|
178 BOOST_MATH_STD_USING
|
Chris@102
|
179 //
|
Chris@102
|
180 // If we take this expansion for polygamma: http://functions.wolfram.com/06.15.06.0003.02
|
Chris@102
|
181 // and substitute in this expression for polygamma(n, 1): http://functions.wolfram.com/06.15.03.0009.01
|
Chris@102
|
182 // we get an alternating series for polygamma when x is small in terms of zeta functions of
|
Chris@102
|
183 // integer arguments (which are easy to evaluate, at least when the integer is even).
|
Chris@102
|
184 //
|
Chris@102
|
185 // In order to avoid spurious overflow, save the n! term for later, and rescale at the end:
|
Chris@102
|
186 //
|
Chris@102
|
187 T scale = boost::math::factorial<T>(n, pol);
|
Chris@102
|
188 //
|
Chris@102
|
189 // "factorial_part" contains everything except the zeta function
|
Chris@102
|
190 // evaluations in each term:
|
Chris@102
|
191 //
|
Chris@102
|
192 T factorial_part = 1;
|
Chris@102
|
193 //
|
Chris@102
|
194 // "prefix" is what we'll be adding the accumulated sum to, it will
|
Chris@102
|
195 // be n! / z^(n+1), but since we're scaling by n! it's just
|
Chris@102
|
196 // 1 / z^(n+1) for now:
|
Chris@102
|
197 //
|
Chris@102
|
198 T prefix = pow(x, n + 1);
|
Chris@102
|
199 if(prefix == 0)
|
Chris@102
|
200 return boost::math::policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@102
|
201 prefix = 1 / prefix;
|
Chris@102
|
202 //
|
Chris@102
|
203 // First term in the series is necessarily < zeta(2) < 2, so
|
Chris@102
|
204 // ignore the sum if it will have no effect on the result anyway:
|
Chris@102
|
205 //
|
Chris@102
|
206 if(prefix > 2 / policies::get_epsilon<T, Policy>())
|
Chris@102
|
207 return ((n & 1) ? 1 : -1) *
|
Chris@102
|
208 (tools::max_value<T>() / prefix < scale ? policies::raise_overflow_error<T>(function, 0, pol) : prefix * scale);
|
Chris@102
|
209 //
|
Chris@102
|
210 // As this is an alternating series we could accelerate it using
|
Chris@102
|
211 // "Convergence Acceleration of Alternating Series",
|
Chris@102
|
212 // Henri Cohen, Fernando Rodriguez Villegas, and Don Zagier, Experimental Mathematics, 1999.
|
Chris@102
|
213 // In practice however, it appears not to make any difference to the number of terms
|
Chris@102
|
214 // required except in some edge cases which are filtered out anyway before we get here.
|
Chris@102
|
215 //
|
Chris@102
|
216 T sum = prefix;
|
Chris@102
|
217 for(unsigned k = 0;;)
|
Chris@102
|
218 {
|
Chris@102
|
219 // Get the k'th term:
|
Chris@102
|
220 T term = factorial_part * boost::math::zeta(T(k + n + 1), pol);
|
Chris@102
|
221 sum += term;
|
Chris@102
|
222 // Termination condition:
|
Chris@102
|
223 if(fabs(term) < fabs(sum * boost::math::policies::get_epsilon<T, Policy>()))
|
Chris@102
|
224 break;
|
Chris@102
|
225 //
|
Chris@102
|
226 // Move on k and factorial_part:
|
Chris@102
|
227 //
|
Chris@102
|
228 ++k;
|
Chris@102
|
229 factorial_part *= (-x * (n + k)) / k;
|
Chris@102
|
230 //
|
Chris@102
|
231 // Last chance exit:
|
Chris@102
|
232 //
|
Chris@102
|
233 if(k > policies::get_max_series_iterations<Policy>())
|
Chris@102
|
234 return policies::raise_evaluation_error<T>(function, "Series did not converge, best value is %1%", sum, pol);
|
Chris@102
|
235 }
|
Chris@102
|
236 //
|
Chris@102
|
237 // We need to multiply by the scale, at each stage checking for oveflow:
|
Chris@102
|
238 //
|
Chris@102
|
239 if(boost::math::tools::max_value<T>() / scale < sum)
|
Chris@102
|
240 return boost::math::policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@102
|
241 sum *= scale;
|
Chris@102
|
242 return n & 1 ? sum : -sum;
|
Chris@102
|
243 }
|
Chris@102
|
244
|
Chris@102
|
245 //
|
Chris@102
|
246 // Helper function which figures out which slot our coefficient is in
|
Chris@102
|
247 // given an angle multiplier for the cosine term of power:
|
Chris@102
|
248 //
|
Chris@102
|
249 template <class Table>
|
Chris@102
|
250 typename Table::value_type::reference dereference_table(Table& table, unsigned row, unsigned power)
|
Chris@102
|
251 {
|
Chris@102
|
252 return table[row][power / 2];
|
Chris@102
|
253 }
|
Chris@102
|
254
|
Chris@102
|
255
|
Chris@102
|
256
|
Chris@102
|
257 template <class T, class Policy>
|
Chris@102
|
258 T poly_cot_pi(int n, T x, T xc, const Policy& pol, const char* function)
|
Chris@102
|
259 {
|
Chris@102
|
260 BOOST_MATH_STD_USING
|
Chris@102
|
261 // Return n'th derivative of cot(pi*x) at x, these are simply
|
Chris@102
|
262 // tabulated for up to n = 9, beyond that it is possible to
|
Chris@102
|
263 // calculate coefficients as follows:
|
Chris@102
|
264 //
|
Chris@102
|
265 // The general form of each derivative is:
|
Chris@102
|
266 //
|
Chris@102
|
267 // pi^n * SUM{k=0, n} C[k,n] * cos^k(pi * x) * csc^(n+1)(pi * x)
|
Chris@102
|
268 //
|
Chris@102
|
269 // With constant C[0,1] = -1 and all other C[k,n] = 0;
|
Chris@102
|
270 // Then for each k < n+1:
|
Chris@102
|
271 // C[k-1, n+1] -= k * C[k, n];
|
Chris@102
|
272 // C[k+1, n+1] += (k-n-1) * C[k, n];
|
Chris@102
|
273 //
|
Chris@102
|
274 // Note that there are many different ways of representing this derivative thanks to
|
Chris@102
|
275 // the many trigomonetric identies available. In particular, the sum of powers of
|
Chris@102
|
276 // cosines could be replaced by a sum of cosine multiple angles, and indeed if you
|
Chris@102
|
277 // plug the derivative into Mathematica this is the form it will give. The two
|
Chris@102
|
278 // forms are related via the Chebeshev polynomials of the first kind and
|
Chris@102
|
279 // T_n(cos(x)) = cos(n x). The polynomial form has the great advantage that
|
Chris@102
|
280 // all the cosine terms are zero at half integer arguments - right where this
|
Chris@102
|
281 // function has it's minumum - thus avoiding cancellation error in this region.
|
Chris@102
|
282 //
|
Chris@102
|
283 // And finally, since every other term in the polynomials is zero, we can save
|
Chris@102
|
284 // space by only storing the non-zero terms. This greatly complexifies
|
Chris@102
|
285 // subscripting the tables in the calculation, but halves the storage space
|
Chris@102
|
286 // (and complexity for that matter).
|
Chris@102
|
287 //
|
Chris@102
|
288 T s = fabs(x) < fabs(xc) ? boost::math::sin_pi(x, pol) : boost::math::sin_pi(xc, pol);
|
Chris@102
|
289 T c = boost::math::cos_pi(x, pol);
|
Chris@102
|
290 switch(n)
|
Chris@102
|
291 {
|
Chris@102
|
292 case 1:
|
Chris@102
|
293 return -constants::pi<T, Policy>() / (s * s);
|
Chris@102
|
294 case 2:
|
Chris@102
|
295 {
|
Chris@102
|
296 return 2 * constants::pi<T, Policy>() * constants::pi<T, Policy>() * c / boost::math::pow<3>(s, pol);
|
Chris@102
|
297 }
|
Chris@102
|
298 case 3:
|
Chris@102
|
299 {
|
Chris@102
|
300 int P[] = { -2, -4 };
|
Chris@102
|
301 return boost::math::pow<3>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<4>(s, pol);
|
Chris@102
|
302 }
|
Chris@102
|
303 case 4:
|
Chris@102
|
304 {
|
Chris@102
|
305 int P[] = { 16, 8 };
|
Chris@102
|
306 return boost::math::pow<4>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<5>(s, pol);
|
Chris@102
|
307 }
|
Chris@102
|
308 case 5:
|
Chris@102
|
309 {
|
Chris@102
|
310 int P[] = { -16, -88, -16 };
|
Chris@102
|
311 return boost::math::pow<5>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<6>(s, pol);
|
Chris@102
|
312 }
|
Chris@102
|
313 case 6:
|
Chris@102
|
314 {
|
Chris@102
|
315 int P[] = { 272, 416, 32 };
|
Chris@102
|
316 return boost::math::pow<6>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<7>(s, pol);
|
Chris@102
|
317 }
|
Chris@102
|
318 case 7:
|
Chris@102
|
319 {
|
Chris@102
|
320 int P[] = { -272, -2880, -1824, -64 };
|
Chris@102
|
321 return boost::math::pow<7>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<8>(s, pol);
|
Chris@102
|
322 }
|
Chris@102
|
323 case 8:
|
Chris@102
|
324 {
|
Chris@102
|
325 int P[] = { 7936, 24576, 7680, 128 };
|
Chris@102
|
326 return boost::math::pow<8>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<9>(s, pol);
|
Chris@102
|
327 }
|
Chris@102
|
328 case 9:
|
Chris@102
|
329 {
|
Chris@102
|
330 int P[] = { -7936, -137216, -185856, -31616, -256 };
|
Chris@102
|
331 return boost::math::pow<9>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<10>(s, pol);
|
Chris@102
|
332 }
|
Chris@102
|
333 case 10:
|
Chris@102
|
334 {
|
Chris@102
|
335 int P[] = { 353792, 1841152, 1304832, 128512, 512 };
|
Chris@102
|
336 return boost::math::pow<10>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<11>(s, pol);
|
Chris@102
|
337 }
|
Chris@102
|
338 case 11:
|
Chris@102
|
339 {
|
Chris@102
|
340 int P[] = { -353792, -9061376, -21253376, -8728576, -518656, -1024};
|
Chris@102
|
341 return boost::math::pow<11>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<12>(s, pol);
|
Chris@102
|
342 }
|
Chris@102
|
343 case 12:
|
Chris@102
|
344 {
|
Chris@102
|
345 int P[] = { 22368256, 175627264, 222398464, 56520704, 2084864, 2048 };
|
Chris@102
|
346 return boost::math::pow<12>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<13>(s, pol);
|
Chris@102
|
347 }
|
Chris@102
|
348 #ifndef BOOST_NO_LONG_LONG
|
Chris@102
|
349 case 13:
|
Chris@102
|
350 {
|
Chris@102
|
351 long long P[] = { -22368256LL, -795300864LL, -2868264960LL, -2174832640LL, -357888000LL, -8361984LL, -4096 };
|
Chris@102
|
352 return boost::math::pow<13>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<14>(s, pol);
|
Chris@102
|
353 }
|
Chris@102
|
354 case 14:
|
Chris@102
|
355 {
|
Chris@102
|
356 long long P[] = { 1903757312LL, 21016670208LL, 41731645440LL, 20261765120LL, 2230947840LL, 33497088LL, 8192 };
|
Chris@102
|
357 return boost::math::pow<14>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<15>(s, pol);
|
Chris@102
|
358 }
|
Chris@102
|
359 case 15:
|
Chris@102
|
360 {
|
Chris@102
|
361 long long P[] = { -1903757312LL, -89702612992LL, -460858269696LL, -559148810240LL, -182172651520LL, -13754155008LL, -134094848LL, -16384 };
|
Chris@102
|
362 return boost::math::pow<15>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<16>(s, pol);
|
Chris@102
|
363 }
|
Chris@102
|
364 case 16:
|
Chris@102
|
365 {
|
Chris@102
|
366 long long P[] = { 209865342976LL, 3099269660672LL, 8885192097792LL, 7048869314560LL, 1594922762240LL, 84134068224LL, 536608768LL, 32768 };
|
Chris@102
|
367 return boost::math::pow<16>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<17>(s, pol);
|
Chris@102
|
368 }
|
Chris@102
|
369 case 17:
|
Chris@102
|
370 {
|
Chris@102
|
371 long long P[] = { -209865342976LL, -12655654469632LL, -87815735738368LL, -155964390375424LL, -84842998005760LL, -13684856848384LL, -511780323328LL, -2146926592LL, -65536 };
|
Chris@102
|
372 return boost::math::pow<17>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<18>(s, pol);
|
Chris@102
|
373 }
|
Chris@102
|
374 case 18:
|
Chris@102
|
375 {
|
Chris@102
|
376 long long P[] = { 29088885112832LL, 553753414467584LL, 2165206642589696LL, 2550316668551168LL, 985278548541440LL, 115620218667008LL, 3100738912256LL, 8588754944LL, 131072 };
|
Chris@102
|
377 return boost::math::pow<18>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<19>(s, pol);
|
Chris@102
|
378 }
|
Chris@102
|
379 case 19:
|
Chris@102
|
380 {
|
Chris@102
|
381 long long P[] = { -29088885112832LL, -2184860175433728LL, -19686087844429824LL, -48165109676113920LL, -39471306959486976LL, -11124607890751488LL, -965271355195392LL, -18733264797696LL, -34357248000LL, -262144 };
|
Chris@102
|
382 return boost::math::pow<19>(constants::pi<T, Policy>(), pol) * tools::evaluate_even_polynomial(P, c) / boost::math::pow<20>(s, pol);
|
Chris@102
|
383 }
|
Chris@102
|
384 case 20:
|
Chris@102
|
385 {
|
Chris@102
|
386 long long P[] = { 4951498053124096LL, 118071834535526400LL, 603968063567560704LL, 990081991141490688LL, 584901762421358592LL, 122829335169859584LL, 7984436548730880LL, 112949304754176LL, 137433710592LL, 524288 };
|
Chris@102
|
387 return boost::math::pow<20>(constants::pi<T, Policy>(), pol) * c * tools::evaluate_even_polynomial(P, c) / boost::math::pow<21>(s, pol);
|
Chris@102
|
388 }
|
Chris@102
|
389 #endif
|
Chris@102
|
390 }
|
Chris@102
|
391
|
Chris@102
|
392 //
|
Chris@102
|
393 // We'll have to compute the coefficients up to n,
|
Chris@102
|
394 // complexity is O(n^2) which we don't worry about for now
|
Chris@102
|
395 // as the values are computed once and then cached.
|
Chris@102
|
396 // However, if the final evaluation would have too many
|
Chris@102
|
397 // terms just bail out right away:
|
Chris@102
|
398 //
|
Chris@102
|
399 if((unsigned)n / 2u > policies::get_max_series_iterations<Policy>())
|
Chris@102
|
400 return policies::raise_evaluation_error<T>(function, "The value of n is so large that we're unable to compute the result in reasonable time, best guess is %1%", 0, pol);
|
Chris@102
|
401 #ifdef BOOST_HAS_THREADS
|
Chris@102
|
402 static boost::detail::lightweight_mutex m;
|
Chris@102
|
403 boost::detail::lightweight_mutex::scoped_lock l(m);
|
Chris@102
|
404 #endif
|
Chris@102
|
405 static std::vector<std::vector<T> > table(1, std::vector<T>(1, T(-1)));
|
Chris@102
|
406
|
Chris@102
|
407 int index = n - 1;
|
Chris@102
|
408
|
Chris@102
|
409 if(index >= (int)table.size())
|
Chris@102
|
410 {
|
Chris@102
|
411 for(int i = (int)table.size() - 1; i < index; ++i)
|
Chris@102
|
412 {
|
Chris@102
|
413 int offset = i & 1; // 1 if the first cos power is 0, otherwise 0.
|
Chris@102
|
414 int sin_order = i + 2; // order of the sin term
|
Chris@102
|
415 int max_cos_order = sin_order - 1; // largest order of the polynomial of cos terms
|
Chris@102
|
416 int max_columns = (max_cos_order - offset) / 2; // How many entries there are in the current row.
|
Chris@102
|
417 int next_offset = offset ? 0 : 1;
|
Chris@102
|
418 int next_max_columns = (max_cos_order + 1 - next_offset) / 2; // How many entries there will be in the next row
|
Chris@102
|
419 table.push_back(std::vector<T>(next_max_columns + 1, T(0)));
|
Chris@102
|
420
|
Chris@102
|
421 for(int column = 0; column <= max_columns; ++column)
|
Chris@102
|
422 {
|
Chris@102
|
423 int cos_order = 2 * column + offset; // order of the cosine term in entry "column"
|
Chris@102
|
424 BOOST_ASSERT(column < (int)table[i].size());
|
Chris@102
|
425 BOOST_ASSERT((cos_order + 1) / 2 < (int)table[i + 1].size());
|
Chris@102
|
426 table[i + 1][(cos_order + 1) / 2] += ((cos_order - sin_order) * table[i][column]) / (sin_order - 1);
|
Chris@102
|
427 if(cos_order)
|
Chris@102
|
428 table[i + 1][(cos_order - 1) / 2] += (-cos_order * table[i][column]) / (sin_order - 1);
|
Chris@102
|
429 }
|
Chris@102
|
430 }
|
Chris@102
|
431
|
Chris@102
|
432 }
|
Chris@102
|
433 T sum = boost::math::tools::evaluate_even_polynomial(&table[index][0], c, table[index].size());
|
Chris@102
|
434 if(index & 1)
|
Chris@102
|
435 sum *= c; // First coeffient is order 1, and really an odd polynomial.
|
Chris@102
|
436 if(sum == 0)
|
Chris@102
|
437 return sum;
|
Chris@102
|
438 //
|
Chris@102
|
439 // The remaining terms are computed using logs since the powers and factorials
|
Chris@102
|
440 // get real large real quick:
|
Chris@102
|
441 //
|
Chris@102
|
442 T power_terms = n * log(boost::math::constants::pi<T>());
|
Chris@102
|
443 if(s == 0)
|
Chris@102
|
444 return sum * boost::math::policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@102
|
445 power_terms -= log(fabs(s)) * (n + 1);
|
Chris@102
|
446 power_terms += boost::math::lgamma(T(n));
|
Chris@102
|
447 power_terms += log(fabs(sum));
|
Chris@102
|
448
|
Chris@102
|
449 if(power_terms > boost::math::tools::log_max_value<T>())
|
Chris@102
|
450 return sum * boost::math::policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@102
|
451
|
Chris@102
|
452 return exp(power_terms) * ((s < 0) && ((n + 1) & 1) ? -1 : 1) * boost::math::sign(sum);
|
Chris@102
|
453 }
|
Chris@102
|
454
|
Chris@102
|
455 template <class T, class Policy>
|
Chris@102
|
456 struct polygamma_initializer
|
Chris@102
|
457 {
|
Chris@102
|
458 struct init
|
Chris@102
|
459 {
|
Chris@102
|
460 init()
|
Chris@102
|
461 {
|
Chris@102
|
462 // Forces initialization of our table of coefficients and mutex:
|
Chris@102
|
463 boost::math::polygamma(30, T(-2.5f), Policy());
|
Chris@102
|
464 }
|
Chris@102
|
465 void force_instantiate()const{}
|
Chris@102
|
466 };
|
Chris@102
|
467 static const init initializer;
|
Chris@102
|
468 static void force_instantiate()
|
Chris@102
|
469 {
|
Chris@102
|
470 initializer.force_instantiate();
|
Chris@102
|
471 }
|
Chris@102
|
472 };
|
Chris@102
|
473
|
Chris@102
|
474 template <class T, class Policy>
|
Chris@102
|
475 const typename polygamma_initializer<T, Policy>::init polygamma_initializer<T, Policy>::initializer;
|
Chris@102
|
476
|
Chris@102
|
477 template<class T, class Policy>
|
Chris@102
|
478 inline T polygamma_imp(const int n, T x, const Policy &pol)
|
Chris@102
|
479 {
|
Chris@102
|
480 BOOST_MATH_STD_USING
|
Chris@102
|
481 static const char* function = "boost::math::polygamma<%1%>(int, %1%)";
|
Chris@102
|
482 polygamma_initializer<T, Policy>::initializer.force_instantiate();
|
Chris@102
|
483 if(n < 0)
|
Chris@102
|
484 return policies::raise_domain_error<T>(function, "Order must be >= 0, but got %1%", static_cast<T>(n), pol);
|
Chris@102
|
485 if(x < 0)
|
Chris@102
|
486 {
|
Chris@102
|
487 if(floor(x) == x)
|
Chris@102
|
488 {
|
Chris@102
|
489 //
|
Chris@102
|
490 // Result is infinity if x is odd, and a pole error if x is even.
|
Chris@102
|
491 //
|
Chris@102
|
492 if(lltrunc(x) & 1)
|
Chris@102
|
493 return policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@102
|
494 else
|
Chris@102
|
495 return policies::raise_pole_error<T>(function, "Evaluation at negative integer %1%", x, pol);
|
Chris@102
|
496 }
|
Chris@102
|
497 T z = 1 - x;
|
Chris@102
|
498 T result = polygamma_imp(n, z, pol) + constants::pi<T, Policy>() * poly_cot_pi(n, z, x, pol, function);
|
Chris@102
|
499 return n & 1 ? T(-result) : result;
|
Chris@102
|
500 }
|
Chris@102
|
501 //
|
Chris@102
|
502 // Limit for use of small-x-series is chosen
|
Chris@102
|
503 // so that the series doesn't go too divergent
|
Chris@102
|
504 // in the first few terms. Ordinarily this
|
Chris@102
|
505 // would mean setting the limit to ~ 1 / n,
|
Chris@102
|
506 // but we can tolerate a small amount of divergence:
|
Chris@102
|
507 //
|
Chris@102
|
508 T small_x_limit = std::min(T(T(5) / n), T(0.25f));
|
Chris@102
|
509 if(x < small_x_limit)
|
Chris@102
|
510 {
|
Chris@102
|
511 return polygamma_nearzero(n, x, pol, function);
|
Chris@102
|
512 }
|
Chris@102
|
513 else if(x > 0.4F * policies::digits_base10<T, Policy>() + 4.0f * n)
|
Chris@102
|
514 {
|
Chris@102
|
515 return polygamma_atinfinityplus(n, x, pol, function);
|
Chris@102
|
516 }
|
Chris@102
|
517 else if(x == 1)
|
Chris@102
|
518 {
|
Chris@102
|
519 return (n & 1 ? 1 : -1) * boost::math::factorial<T>(n, pol) * boost::math::zeta(T(n + 1), pol);
|
Chris@102
|
520 }
|
Chris@102
|
521 else if(x == 0.5f)
|
Chris@102
|
522 {
|
Chris@102
|
523 T result = (n & 1 ? 1 : -1) * boost::math::factorial<T>(n, pol) * boost::math::zeta(T(n + 1), pol);
|
Chris@102
|
524 if(fabs(result) >= ldexp(tools::max_value<T>(), -n - 1))
|
Chris@102
|
525 return boost::math::sign(result) * policies::raise_overflow_error<T>(function, 0, pol);
|
Chris@102
|
526 result *= ldexp(T(1), n + 1) - 1;
|
Chris@102
|
527 return result;
|
Chris@102
|
528 }
|
Chris@102
|
529 else
|
Chris@102
|
530 {
|
Chris@102
|
531 return polygamma_attransitionplus(n, x, pol, function);
|
Chris@102
|
532 }
|
Chris@102
|
533 }
|
Chris@102
|
534
|
Chris@102
|
535 } } } // namespace boost::math::detail
|
Chris@102
|
536
|
Chris@102
|
537 #endif // _BOOST_POLYGAMMA_DETAIL_2013_07_30_HPP_
|
Chris@102
|
538
|