annotate DEPENDENCIES/generic/include/boost/math/special_functions/gamma.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1
Chris@101 2 // Copyright John Maddock 2006-7, 2013-14.
Chris@101 3 // Copyright Paul A. Bristow 2007, 2013-14.
Chris@101 4 // Copyright Nikhar Agrawal 2013-14
Chris@101 5 // Copyright Christopher Kormanyos 2013-14
Chris@16 6
Chris@16 7 // Use, modification and distribution are subject to the
Chris@16 8 // Boost Software License, Version 1.0. (See accompanying file
Chris@16 9 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 10
Chris@16 11 #ifndef BOOST_MATH_SF_GAMMA_HPP
Chris@16 12 #define BOOST_MATH_SF_GAMMA_HPP
Chris@16 13
Chris@16 14 #ifdef _MSC_VER
Chris@16 15 #pragma once
Chris@16 16 #endif
Chris@16 17
Chris@16 18 #include <boost/config.hpp>
Chris@16 19 #include <boost/math/tools/series.hpp>
Chris@16 20 #include <boost/math/tools/fraction.hpp>
Chris@16 21 #include <boost/math/tools/precision.hpp>
Chris@16 22 #include <boost/math/tools/promotion.hpp>
Chris@16 23 #include <boost/math/policies/error_handling.hpp>
Chris@16 24 #include <boost/math/constants/constants.hpp>
Chris@16 25 #include <boost/math/special_functions/math_fwd.hpp>
Chris@16 26 #include <boost/math/special_functions/log1p.hpp>
Chris@16 27 #include <boost/math/special_functions/trunc.hpp>
Chris@16 28 #include <boost/math/special_functions/powm1.hpp>
Chris@16 29 #include <boost/math/special_functions/sqrt1pm1.hpp>
Chris@16 30 #include <boost/math/special_functions/lanczos.hpp>
Chris@16 31 #include <boost/math/special_functions/fpclassify.hpp>
Chris@16 32 #include <boost/math/special_functions/detail/igamma_large.hpp>
Chris@16 33 #include <boost/math/special_functions/detail/unchecked_factorial.hpp>
Chris@16 34 #include <boost/math/special_functions/detail/lgamma_small.hpp>
Chris@101 35 #include <boost/math/special_functions/bernoulli.hpp>
Chris@16 36 #include <boost/type_traits/is_convertible.hpp>
Chris@16 37 #include <boost/assert.hpp>
Chris@16 38 #include <boost/mpl/greater.hpp>
Chris@16 39 #include <boost/mpl/equal_to.hpp>
Chris@16 40 #include <boost/mpl/greater.hpp>
Chris@16 41
Chris@16 42 #include <boost/config/no_tr1/cmath.hpp>
Chris@16 43 #include <algorithm>
Chris@16 44
Chris@16 45 #ifdef BOOST_MSVC
Chris@16 46 # pragma warning(push)
Chris@16 47 # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
Chris@16 48 # pragma warning(disable: 4127) // conditional expression is constant.
Chris@16 49 # pragma warning(disable: 4100) // unreferenced formal parameter.
Chris@16 50 // Several variables made comments,
Chris@16 51 // but some difficulty as whether referenced on not may depend on macro values.
Chris@16 52 // So to be safe, 4100 warnings suppressed.
Chris@16 53 // TODO - revisit this?
Chris@16 54 #endif
Chris@16 55
Chris@16 56 namespace boost{ namespace math{
Chris@16 57
Chris@16 58 namespace detail{
Chris@16 59
Chris@16 60 template <class T>
Chris@16 61 inline bool is_odd(T v, const boost::true_type&)
Chris@16 62 {
Chris@16 63 int i = static_cast<int>(v);
Chris@16 64 return i&1;
Chris@16 65 }
Chris@16 66 template <class T>
Chris@16 67 inline bool is_odd(T v, const boost::false_type&)
Chris@16 68 {
Chris@16 69 // Oh dear can't cast T to int!
Chris@16 70 BOOST_MATH_STD_USING
Chris@16 71 T modulus = v - 2 * floor(v/2);
Chris@16 72 return static_cast<bool>(modulus != 0);
Chris@16 73 }
Chris@16 74 template <class T>
Chris@16 75 inline bool is_odd(T v)
Chris@16 76 {
Chris@16 77 return is_odd(v, ::boost::is_convertible<T, int>());
Chris@16 78 }
Chris@16 79
Chris@16 80 template <class T>
Chris@16 81 T sinpx(T z)
Chris@16 82 {
Chris@16 83 // Ad hoc function calculates x * sin(pi * x),
Chris@16 84 // taking extra care near when x is near a whole number.
Chris@16 85 BOOST_MATH_STD_USING
Chris@16 86 int sign = 1;
Chris@16 87 if(z < 0)
Chris@16 88 {
Chris@16 89 z = -z;
Chris@16 90 }
Chris@16 91 T fl = floor(z);
Chris@16 92 T dist;
Chris@16 93 if(is_odd(fl))
Chris@16 94 {
Chris@16 95 fl += 1;
Chris@16 96 dist = fl - z;
Chris@16 97 sign = -sign;
Chris@16 98 }
Chris@16 99 else
Chris@16 100 {
Chris@16 101 dist = z - fl;
Chris@16 102 }
Chris@16 103 BOOST_ASSERT(fl >= 0);
Chris@16 104 if(dist > 0.5)
Chris@16 105 dist = 1 - dist;
Chris@16 106 T result = sin(dist*boost::math::constants::pi<T>());
Chris@16 107 return sign*z*result;
Chris@16 108 } // template <class T> T sinpx(T z)
Chris@16 109 //
Chris@16 110 // tgamma(z), with Lanczos support:
Chris@16 111 //
Chris@16 112 template <class T, class Policy, class Lanczos>
Chris@16 113 T gamma_imp(T z, const Policy& pol, const Lanczos& l)
Chris@16 114 {
Chris@16 115 BOOST_MATH_STD_USING
Chris@16 116
Chris@16 117 T result = 1;
Chris@16 118
Chris@16 119 #ifdef BOOST_MATH_INSTRUMENT
Chris@16 120 static bool b = false;
Chris@16 121 if(!b)
Chris@16 122 {
Chris@16 123 std::cout << "tgamma_imp called with " << typeid(z).name() << " " << typeid(l).name() << std::endl;
Chris@16 124 b = true;
Chris@16 125 }
Chris@16 126 #endif
Chris@16 127 static const char* function = "boost::math::tgamma<%1%>(%1%)";
Chris@16 128
Chris@16 129 if(z <= 0)
Chris@16 130 {
Chris@16 131 if(floor(z) == z)
Chris@16 132 return policies::raise_pole_error<T>(function, "Evaluation of tgamma at a negative integer %1%.", z, pol);
Chris@16 133 if(z <= -20)
Chris@16 134 {
Chris@16 135 result = gamma_imp(T(-z), pol, l) * sinpx(z);
Chris@16 136 BOOST_MATH_INSTRUMENT_VARIABLE(result);
Chris@16 137 if((fabs(result) < 1) && (tools::max_value<T>() * fabs(result) < boost::math::constants::pi<T>()))
Chris@101 138 return -boost::math::sign(result) * policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
Chris@16 139 result = -boost::math::constants::pi<T>() / result;
Chris@16 140 if(result == 0)
Chris@16 141 return policies::raise_underflow_error<T>(function, "Result of tgamma is too small to represent.", pol);
Chris@16 142 if((boost::math::fpclassify)(result) == (int)FP_SUBNORMAL)
Chris@16 143 return policies::raise_denorm_error<T>(function, "Result of tgamma is denormalized.", result, pol);
Chris@16 144 BOOST_MATH_INSTRUMENT_VARIABLE(result);
Chris@16 145 return result;
Chris@16 146 }
Chris@16 147
Chris@16 148 // shift z to > 1:
Chris@16 149 while(z < 0)
Chris@16 150 {
Chris@16 151 result /= z;
Chris@16 152 z += 1;
Chris@16 153 }
Chris@16 154 }
Chris@16 155 BOOST_MATH_INSTRUMENT_VARIABLE(result);
Chris@16 156 if((floor(z) == z) && (z < max_factorial<T>::value))
Chris@16 157 {
Chris@16 158 result *= unchecked_factorial<T>(itrunc(z, pol) - 1);
Chris@16 159 BOOST_MATH_INSTRUMENT_VARIABLE(result);
Chris@16 160 }
Chris@101 161 else if (z < tools::root_epsilon<T>())
Chris@101 162 {
Chris@101 163 if (z < 1 / tools::max_value<T>())
Chris@101 164 result = policies::raise_overflow_error<T>(function, 0, pol);
Chris@101 165 result *= 1 / z - constants::euler<T>();
Chris@101 166 }
Chris@16 167 else
Chris@16 168 {
Chris@16 169 result *= Lanczos::lanczos_sum(z);
Chris@16 170 T zgh = (z + static_cast<T>(Lanczos::g()) - boost::math::constants::half<T>());
Chris@16 171 T lzgh = log(zgh);
Chris@16 172 BOOST_MATH_INSTRUMENT_VARIABLE(result);
Chris@16 173 BOOST_MATH_INSTRUMENT_VARIABLE(tools::log_max_value<T>());
Chris@16 174 if(z * lzgh > tools::log_max_value<T>())
Chris@16 175 {
Chris@16 176 // we're going to overflow unless this is done with care:
Chris@16 177 BOOST_MATH_INSTRUMENT_VARIABLE(zgh);
Chris@16 178 if(lzgh * z / 2 > tools::log_max_value<T>())
Chris@101 179 return boost::math::sign(result) * policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
Chris@16 180 T hp = pow(zgh, (z / 2) - T(0.25));
Chris@16 181 BOOST_MATH_INSTRUMENT_VARIABLE(hp);
Chris@16 182 result *= hp / exp(zgh);
Chris@16 183 BOOST_MATH_INSTRUMENT_VARIABLE(result);
Chris@16 184 if(tools::max_value<T>() / hp < result)
Chris@101 185 return boost::math::sign(result) * policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
Chris@16 186 result *= hp;
Chris@16 187 BOOST_MATH_INSTRUMENT_VARIABLE(result);
Chris@16 188 }
Chris@16 189 else
Chris@16 190 {
Chris@16 191 BOOST_MATH_INSTRUMENT_VARIABLE(zgh);
Chris@16 192 BOOST_MATH_INSTRUMENT_VARIABLE(pow(zgh, z - boost::math::constants::half<T>()));
Chris@16 193 BOOST_MATH_INSTRUMENT_VARIABLE(exp(zgh));
Chris@16 194 result *= pow(zgh, z - boost::math::constants::half<T>()) / exp(zgh);
Chris@16 195 BOOST_MATH_INSTRUMENT_VARIABLE(result);
Chris@16 196 }
Chris@16 197 }
Chris@16 198 return result;
Chris@16 199 }
Chris@16 200 //
Chris@16 201 // lgamma(z) with Lanczos support:
Chris@16 202 //
Chris@16 203 template <class T, class Policy, class Lanczos>
Chris@16 204 T lgamma_imp(T z, const Policy& pol, const Lanczos& l, int* sign = 0)
Chris@16 205 {
Chris@16 206 #ifdef BOOST_MATH_INSTRUMENT
Chris@16 207 static bool b = false;
Chris@16 208 if(!b)
Chris@16 209 {
Chris@16 210 std::cout << "lgamma_imp called with " << typeid(z).name() << " " << typeid(l).name() << std::endl;
Chris@16 211 b = true;
Chris@16 212 }
Chris@16 213 #endif
Chris@16 214
Chris@16 215 BOOST_MATH_STD_USING
Chris@16 216
Chris@16 217 static const char* function = "boost::math::lgamma<%1%>(%1%)";
Chris@16 218
Chris@16 219 T result = 0;
Chris@16 220 int sresult = 1;
Chris@101 221 if(z <= -tools::root_epsilon<T>())
Chris@16 222 {
Chris@16 223 // reflection formula:
Chris@16 224 if(floor(z) == z)
Chris@16 225 return policies::raise_pole_error<T>(function, "Evaluation of lgamma at a negative integer %1%.", z, pol);
Chris@16 226
Chris@16 227 T t = sinpx(z);
Chris@16 228 z = -z;
Chris@16 229 if(t < 0)
Chris@16 230 {
Chris@16 231 t = -t;
Chris@16 232 }
Chris@16 233 else
Chris@16 234 {
Chris@16 235 sresult = -sresult;
Chris@16 236 }
Chris@16 237 result = log(boost::math::constants::pi<T>()) - lgamma_imp(z, pol, l) - log(t);
Chris@16 238 }
Chris@101 239 else if (z < tools::root_epsilon<T>())
Chris@101 240 {
Chris@101 241 if (0 == z)
Chris@101 242 return policies::raise_pole_error<T>(function, "Evaluation of lgamma at %1%.", z, pol);
Chris@101 243 if (fabs(z) < 1 / tools::max_value<T>())
Chris@101 244 result = -log(fabs(z));
Chris@101 245 else
Chris@101 246 result = log(fabs(1 / z - constants::euler<T>()));
Chris@101 247 if (z < 0)
Chris@101 248 sresult = -1;
Chris@101 249 }
Chris@16 250 else if(z < 15)
Chris@16 251 {
Chris@16 252 typedef typename policies::precision<T, Policy>::type precision_type;
Chris@16 253 typedef typename mpl::if_<
Chris@16 254 mpl::and_<
Chris@16 255 mpl::less_equal<precision_type, mpl::int_<64> >,
Chris@16 256 mpl::greater<precision_type, mpl::int_<0> >
Chris@16 257 >,
Chris@16 258 mpl::int_<64>,
Chris@16 259 typename mpl::if_<
Chris@16 260 mpl::and_<
Chris@16 261 mpl::less_equal<precision_type, mpl::int_<113> >,
Chris@16 262 mpl::greater<precision_type, mpl::int_<0> >
Chris@16 263 >,
Chris@16 264 mpl::int_<113>, mpl::int_<0> >::type
Chris@16 265 >::type tag_type;
Chris@16 266 result = lgamma_small_imp<T>(z, T(z - 1), T(z - 2), tag_type(), pol, l);
Chris@16 267 }
Chris@16 268 else if((z >= 3) && (z < 100) && (std::numeric_limits<T>::max_exponent >= 1024))
Chris@16 269 {
Chris@16 270 // taking the log of tgamma reduces the error, no danger of overflow here:
Chris@16 271 result = log(gamma_imp(z, pol, l));
Chris@16 272 }
Chris@16 273 else
Chris@16 274 {
Chris@16 275 // regular evaluation:
Chris@16 276 T zgh = static_cast<T>(z + Lanczos::g() - boost::math::constants::half<T>());
Chris@16 277 result = log(zgh) - 1;
Chris@16 278 result *= z - 0.5f;
Chris@16 279 result += log(Lanczos::lanczos_sum_expG_scaled(z));
Chris@16 280 }
Chris@16 281
Chris@16 282 if(sign)
Chris@16 283 *sign = sresult;
Chris@16 284 return result;
Chris@16 285 }
Chris@16 286
Chris@16 287 //
Chris@16 288 // Incomplete gamma functions follow:
Chris@16 289 //
Chris@16 290 template <class T>
Chris@16 291 struct upper_incomplete_gamma_fract
Chris@16 292 {
Chris@16 293 private:
Chris@16 294 T z, a;
Chris@16 295 int k;
Chris@16 296 public:
Chris@16 297 typedef std::pair<T,T> result_type;
Chris@16 298
Chris@16 299 upper_incomplete_gamma_fract(T a1, T z1)
Chris@16 300 : z(z1-a1+1), a(a1), k(0)
Chris@16 301 {
Chris@16 302 }
Chris@16 303
Chris@16 304 result_type operator()()
Chris@16 305 {
Chris@16 306 ++k;
Chris@16 307 z += 2;
Chris@16 308 return result_type(k * (a - k), z);
Chris@16 309 }
Chris@16 310 };
Chris@16 311
Chris@16 312 template <class T>
Chris@16 313 inline T upper_gamma_fraction(T a, T z, T eps)
Chris@16 314 {
Chris@16 315 // Multiply result by z^a * e^-z to get the full
Chris@16 316 // upper incomplete integral. Divide by tgamma(z)
Chris@16 317 // to normalise.
Chris@16 318 upper_incomplete_gamma_fract<T> f(a, z);
Chris@16 319 return 1 / (z - a + 1 + boost::math::tools::continued_fraction_a(f, eps));
Chris@16 320 }
Chris@16 321
Chris@16 322 template <class T>
Chris@16 323 struct lower_incomplete_gamma_series
Chris@16 324 {
Chris@16 325 private:
Chris@16 326 T a, z, result;
Chris@16 327 public:
Chris@16 328 typedef T result_type;
Chris@16 329 lower_incomplete_gamma_series(T a1, T z1) : a(a1), z(z1), result(1){}
Chris@16 330
Chris@16 331 T operator()()
Chris@16 332 {
Chris@16 333 T r = result;
Chris@16 334 a += 1;
Chris@16 335 result *= z/a;
Chris@16 336 return r;
Chris@16 337 }
Chris@16 338 };
Chris@16 339
Chris@16 340 template <class T, class Policy>
Chris@16 341 inline T lower_gamma_series(T a, T z, const Policy& pol, T init_value = 0)
Chris@16 342 {
Chris@16 343 // Multiply result by ((z^a) * (e^-z) / a) to get the full
Chris@16 344 // lower incomplete integral. Then divide by tgamma(a)
Chris@16 345 // to get the normalised value.
Chris@16 346 lower_incomplete_gamma_series<T> s(a, z);
Chris@16 347 boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
Chris@16 348 T factor = policies::get_epsilon<T, Policy>();
Chris@16 349 T result = boost::math::tools::sum_series(s, factor, max_iter, init_value);
Chris@16 350 policies::check_series_iterations<T>("boost::math::detail::lower_gamma_series<%1%>(%1%)", max_iter, pol);
Chris@16 351 return result;
Chris@16 352 }
Chris@16 353
Chris@16 354 //
Chris@101 355 // Fully generic tgamma and lgamma use Stirling's approximation
Chris@101 356 // with Bernoulli numbers.
Chris@16 357 //
Chris@101 358 template<class T>
Chris@101 359 std::size_t highest_bernoulli_index()
Chris@101 360 {
Chris@101 361 const float digits10_of_type = (std::numeric_limits<T>::is_specialized
Chris@101 362 ? static_cast<float>(std::numeric_limits<T>::digits10)
Chris@101 363 : static_cast<float>(boost::math::tools::digits<T>() * 0.301F));
Chris@101 364
Chris@101 365 // Find the high index n for Bn to produce the desired precision in Stirling's calculation.
Chris@101 366 return static_cast<std::size_t>(18.0F + (0.6F * digits10_of_type));
Chris@101 367 }
Chris@101 368
Chris@101 369 template<class T>
Chris@101 370 T minimum_argument_for_bernoulli_recursion()
Chris@101 371 {
Chris@101 372 const float digits10_of_type = (std::numeric_limits<T>::is_specialized
Chris@101 373 ? static_cast<float>(std::numeric_limits<T>::digits10)
Chris@101 374 : static_cast<float>(boost::math::tools::digits<T>() * 0.301F));
Chris@101 375
Chris@101 376 return T(digits10_of_type * 1.7F);
Chris@101 377 }
Chris@101 378
Chris@101 379 // Forward declaration of the lgamma_imp template specialization.
Chris@16 380 template <class T, class Policy>
Chris@101 381 T lgamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos&, int* sign = 0);
Chris@101 382
Chris@101 383 template <class T, class Policy>
Chris@101 384 T gamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos&)
Chris@16 385 {
Chris@101 386 BOOST_MATH_STD_USING
Chris@101 387
Chris@16 388 static const char* function = "boost::math::tgamma<%1%>(%1%)";
Chris@101 389
Chris@101 390 // Check if the argument of tgamma is identically zero.
Chris@101 391 const bool is_at_zero = (z == 0);
Chris@101 392
Chris@101 393 if(is_at_zero)
Chris@101 394 return policies::raise_domain_error<T>(function, "Evaluation of tgamma at zero %1%.", z, pol);
Chris@101 395
Chris@101 396 const bool b_neg = (z < 0);
Chris@101 397
Chris@101 398 const bool floor_of_z_is_equal_to_z = (floor(z) == z);
Chris@101 399
Chris@101 400 // Special case handling of small factorials:
Chris@101 401 if((!b_neg) && floor_of_z_is_equal_to_z && (z < boost::math::max_factorial<T>::value))
Chris@16 402 {
Chris@101 403 return boost::math::unchecked_factorial<T>(itrunc(z) - 1);
Chris@16 404 }
Chris@101 405
Chris@101 406 // Make a local, unsigned copy of the input argument.
Chris@101 407 T zz((!b_neg) ? z : -z);
Chris@101 408
Chris@101 409 // Special case for ultra-small z:
Chris@101 410 if(zz < tools::cbrt_epsilon<T>())
Chris@16 411 {
Chris@101 412 const T a0(1);
Chris@101 413 const T a1(boost::math::constants::euler<T>());
Chris@101 414 const T six_euler_squared((boost::math::constants::euler<T>() * boost::math::constants::euler<T>()) * 6);
Chris@101 415 const T a2((six_euler_squared - boost::math::constants::pi_sqr<T>()) / 12);
Chris@101 416
Chris@101 417 const T inverse_tgamma_series = z * ((a2 * z + a1) * z + a0);
Chris@101 418
Chris@101 419 return 1 / inverse_tgamma_series;
Chris@16 420 }
Chris@101 421
Chris@101 422 // Scale the argument up for the calculation of lgamma,
Chris@101 423 // and use downward recursion later for the final result.
Chris@101 424 const T min_arg_for_recursion = minimum_argument_for_bernoulli_recursion<T>();
Chris@101 425
Chris@101 426 int n_recur;
Chris@101 427
Chris@101 428 if(zz < min_arg_for_recursion)
Chris@16 429 {
Chris@101 430 n_recur = boost::math::itrunc(min_arg_for_recursion - zz) + 1;
Chris@101 431
Chris@101 432 zz += n_recur;
Chris@16 433 }
Chris@16 434 else
Chris@16 435 {
Chris@101 436 n_recur = 0;
Chris@101 437 }
Chris@101 438
Chris@101 439 const T log_gamma_value = lgamma_imp(zz, pol, lanczos::undefined_lanczos());
Chris@101 440
Chris@101 441 if(log_gamma_value > tools::log_max_value<T>())
Chris@101 442 return policies::raise_overflow_error<T>(function, 0, pol);
Chris@101 443
Chris@101 444 T gamma_value = exp(log_gamma_value);
Chris@101 445
Chris@101 446 // Rescale the result using downward recursion if necessary.
Chris@101 447 if(n_recur)
Chris@101 448 {
Chris@101 449 // The order of divides is important, if we keep subtracting 1 from zz
Chris@101 450 // we DO NOT get back to z (cancellation error). Further if z < epsilon
Chris@101 451 // we would end up dividing by zero. Also in order to prevent spurious
Chris@101 452 // overflow with the first division, we must save dividing by |z| till last,
Chris@101 453 // so the optimal order of divides is z+1, z+2, z+3...z+n_recur-1,z.
Chris@101 454 zz = fabs(z) + 1;
Chris@101 455 for(int k = 1; k < n_recur; ++k)
Chris@101 456 {
Chris@101 457 gamma_value /= zz;
Chris@101 458 zz += 1;
Chris@101 459 }
Chris@101 460 gamma_value /= fabs(z);
Chris@101 461 }
Chris@101 462
Chris@101 463 // Return the result, accounting for possible negative arguments.
Chris@101 464 if(b_neg)
Chris@101 465 {
Chris@101 466 // Provide special error analysis for:
Chris@101 467 // * arguments in the neighborhood of a negative integer
Chris@101 468 // * arguments exactly equal to a negative integer.
Chris@101 469
Chris@101 470 // Check if the argument of tgamma is exactly equal to a negative integer.
Chris@101 471 if(floor_of_z_is_equal_to_z)
Chris@101 472 return policies::raise_pole_error<T>(function, "Evaluation of tgamma at a negative integer %1%.", z, pol);
Chris@101 473
Chris@101 474 gamma_value *= sinpx(z);
Chris@101 475
Chris@101 476 BOOST_MATH_INSTRUMENT_VARIABLE(gamma_value);
Chris@101 477
Chris@101 478 const bool result_is_too_large_to_represent = ( (abs(gamma_value) < 1)
Chris@101 479 && ((tools::max_value<T>() * abs(gamma_value)) < boost::math::constants::pi<T>()));
Chris@101 480
Chris@101 481 if(result_is_too_large_to_represent)
Chris@16 482 return policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
Chris@101 483
Chris@101 484 gamma_value = -boost::math::constants::pi<T>() / gamma_value;
Chris@101 485 BOOST_MATH_INSTRUMENT_VARIABLE(gamma_value);
Chris@101 486
Chris@101 487 if(gamma_value == 0)
Chris@101 488 return policies::raise_underflow_error<T>(function, "Result of tgamma is too small to represent.", pol);
Chris@101 489
Chris@101 490 if((boost::math::fpclassify)(gamma_value) == static_cast<int>(FP_SUBNORMAL))
Chris@101 491 return policies::raise_denorm_error<T>(function, "Result of tgamma is denormalized.", gamma_value, pol);
Chris@16 492 }
Chris@101 493
Chris@101 494 return gamma_value;
Chris@16 495 }
Chris@16 496
Chris@16 497 template <class T, class Policy>
Chris@101 498 T lgamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos&, int* sign)
Chris@16 499 {
Chris@16 500 BOOST_MATH_STD_USING
Chris@16 501
Chris@16 502 static const char* function = "boost::math::lgamma<%1%>(%1%)";
Chris@101 503
Chris@101 504 // Check if the argument of lgamma is identically zero.
Chris@101 505 const bool is_at_zero = (z == 0);
Chris@101 506
Chris@101 507 if(is_at_zero)
Chris@101 508 return policies::raise_domain_error<T>(function, "Evaluation of lgamma at zero %1%.", z, pol);
Chris@101 509
Chris@101 510 const bool b_neg = (z < 0);
Chris@101 511
Chris@101 512 const bool floor_of_z_is_equal_to_z = (floor(z) == z);
Chris@101 513
Chris@101 514 // Special case handling of small factorials:
Chris@101 515 if((!b_neg) && floor_of_z_is_equal_to_z && (z < boost::math::max_factorial<T>::value))
Chris@16 516 {
Chris@101 517 return log(boost::math::unchecked_factorial<T>(itrunc(z) - 1));
Chris@101 518 }
Chris@101 519
Chris@101 520 // Make a local, unsigned copy of the input argument.
Chris@101 521 T zz((!b_neg) ? z : -z);
Chris@101 522
Chris@101 523 const T min_arg_for_recursion = minimum_argument_for_bernoulli_recursion<T>();
Chris@101 524
Chris@101 525 T log_gamma_value;
Chris@101 526
Chris@101 527 if (zz < min_arg_for_recursion)
Chris@101 528 {
Chris@101 529 // Here we simply take the logarithm of tgamma(). This is somewhat
Chris@101 530 // inefficient, but simple. The rationale is that the argument here
Chris@101 531 // is relatively small and overflow is not expected to be likely.
Chris@101 532 if (z > -tools::root_epsilon<T>())
Chris@101 533 {
Chris@101 534 // Reflection formula may fail if z is very close to zero, let the series
Chris@101 535 // expansion for tgamma close to zero do the work:
Chris@101 536 log_gamma_value = log(abs(gamma_imp(z, pol, lanczos::undefined_lanczos())));
Chris@101 537 if (sign)
Chris@101 538 {
Chris@101 539 *sign = z < 0 ? -1 : 1;
Chris@101 540 }
Chris@101 541 return log_gamma_value;
Chris@101 542 }
Chris@101 543 else
Chris@101 544 {
Chris@101 545 // No issue with spurious overflow in reflection formula,
Chris@101 546 // just fall through to regular code:
Chris@101 547 log_gamma_value = log(abs(gamma_imp(zz, pol, lanczos::undefined_lanczos())));
Chris@101 548 }
Chris@101 549 }
Chris@101 550 else
Chris@101 551 {
Chris@101 552 // Perform the Bernoulli series expansion of Stirling's approximation.
Chris@101 553
Chris@101 554 const std::size_t number_of_bernoullis_b2n = highest_bernoulli_index<T>();
Chris@101 555
Chris@101 556 T one_over_x_pow_two_n_minus_one = 1 / zz;
Chris@101 557 const T one_over_x2 = one_over_x_pow_two_n_minus_one * one_over_x_pow_two_n_minus_one;
Chris@101 558 T sum = (boost::math::bernoulli_b2n<T>(1) / 2) * one_over_x_pow_two_n_minus_one;
Chris@101 559 const T target_epsilon_to_break_loop = (sum * boost::math::tools::epsilon<T>()) * T(1.0E-10F);
Chris@101 560
Chris@101 561 for(std::size_t n = 2U; n < number_of_bernoullis_b2n; ++n)
Chris@101 562 {
Chris@101 563 one_over_x_pow_two_n_minus_one *= one_over_x2;
Chris@101 564
Chris@101 565 const std::size_t n2 = static_cast<std::size_t>(n * 2U);
Chris@101 566
Chris@101 567 const T term = (boost::math::bernoulli_b2n<T>(static_cast<int>(n)) * one_over_x_pow_two_n_minus_one) / (n2 * (n2 - 1U));
Chris@101 568
Chris@101 569 if((n >= 8U) && (abs(term) < target_epsilon_to_break_loop))
Chris@101 570 {
Chris@101 571 // We have reached the desired precision in Stirling's expansion.
Chris@101 572 // Adding additional terms to the sum of this divergent asymptotic
Chris@101 573 // expansion will not improve the result.
Chris@101 574
Chris@101 575 // Break from the loop.
Chris@101 576 break;
Chris@101 577 }
Chris@101 578
Chris@101 579 sum += term;
Chris@101 580 }
Chris@101 581
Chris@101 582 // Complete Stirling's approximation.
Chris@101 583 const T half_ln_two_pi = log(boost::math::constants::two_pi<T>()) / 2;
Chris@101 584
Chris@101 585 log_gamma_value = ((((zz - boost::math::constants::half<T>()) * log(zz)) - zz) + half_ln_two_pi) + sum;
Chris@101 586 }
Chris@101 587
Chris@101 588 int sign_of_result = 1;
Chris@101 589
Chris@101 590 if(b_neg)
Chris@101 591 {
Chris@101 592 // Provide special error analysis if the argument is exactly
Chris@101 593 // equal to a negative integer.
Chris@101 594
Chris@101 595 // Check if the argument of lgamma is exactly equal to a negative integer.
Chris@101 596 if(floor_of_z_is_equal_to_z)
Chris@101 597 return policies::raise_pole_error<T>(function, "Evaluation of lgamma at a negative integer %1%.", z, pol);
Chris@101 598
Chris@101 599 T t = sinpx(z);
Chris@101 600
Chris@16 601 if(t < 0)
Chris@16 602 {
Chris@16 603 t = -t;
Chris@16 604 }
Chris@16 605 else
Chris@16 606 {
Chris@101 607 sign_of_result = -sign_of_result;
Chris@16 608 }
Chris@101 609
Chris@101 610 log_gamma_value = - log_gamma_value
Chris@101 611 + log(boost::math::constants::pi<T>())
Chris@101 612 - log(t);
Chris@16 613 }
Chris@101 614
Chris@101 615 if(sign != static_cast<int*>(0U)) { *sign = sign_of_result; }
Chris@101 616
Chris@101 617 return log_gamma_value;
Chris@16 618 }
Chris@101 619
Chris@16 620 //
Chris@16 621 // This helper calculates tgamma(dz+1)-1 without cancellation errors,
Chris@16 622 // used by the upper incomplete gamma with z < 1:
Chris@16 623 //
Chris@16 624 template <class T, class Policy, class Lanczos>
Chris@16 625 T tgammap1m1_imp(T dz, Policy const& pol, const Lanczos& l)
Chris@16 626 {
Chris@16 627 BOOST_MATH_STD_USING
Chris@16 628
Chris@16 629 typedef typename policies::precision<T,Policy>::type precision_type;
Chris@16 630
Chris@16 631 typedef typename mpl::if_<
Chris@16 632 mpl::or_<
Chris@16 633 mpl::less_equal<precision_type, mpl::int_<0> >,
Chris@16 634 mpl::greater<precision_type, mpl::int_<113> >
Chris@16 635 >,
Chris@16 636 typename mpl::if_<
Chris@16 637 is_same<Lanczos, lanczos::lanczos24m113>,
Chris@16 638 mpl::int_<113>,
Chris@16 639 mpl::int_<0>
Chris@16 640 >::type,
Chris@16 641 typename mpl::if_<
Chris@16 642 mpl::less_equal<precision_type, mpl::int_<64> >,
Chris@16 643 mpl::int_<64>, mpl::int_<113> >::type
Chris@16 644 >::type tag_type;
Chris@16 645
Chris@16 646 T result;
Chris@16 647 if(dz < 0)
Chris@16 648 {
Chris@16 649 if(dz < -0.5)
Chris@16 650 {
Chris@16 651 // Best method is simply to subtract 1 from tgamma:
Chris@16 652 result = boost::math::tgamma(1+dz, pol) - 1;
Chris@16 653 BOOST_MATH_INSTRUMENT_CODE(result);
Chris@16 654 }
Chris@16 655 else
Chris@16 656 {
Chris@16 657 // Use expm1 on lgamma:
Chris@16 658 result = boost::math::expm1(-boost::math::log1p(dz, pol)
Chris@16 659 + lgamma_small_imp<T>(dz+2, dz + 1, dz, tag_type(), pol, l));
Chris@16 660 BOOST_MATH_INSTRUMENT_CODE(result);
Chris@16 661 }
Chris@16 662 }
Chris@16 663 else
Chris@16 664 {
Chris@16 665 if(dz < 2)
Chris@16 666 {
Chris@16 667 // Use expm1 on lgamma:
Chris@16 668 result = boost::math::expm1(lgamma_small_imp<T>(dz+1, dz, dz-1, tag_type(), pol, l), pol);
Chris@16 669 BOOST_MATH_INSTRUMENT_CODE(result);
Chris@16 670 }
Chris@16 671 else
Chris@16 672 {
Chris@16 673 // Best method is simply to subtract 1 from tgamma:
Chris@16 674 result = boost::math::tgamma(1+dz, pol) - 1;
Chris@16 675 BOOST_MATH_INSTRUMENT_CODE(result);
Chris@16 676 }
Chris@16 677 }
Chris@16 678
Chris@16 679 return result;
Chris@16 680 }
Chris@16 681
Chris@16 682 template <class T, class Policy>
Chris@16 683 inline T tgammap1m1_imp(T dz, Policy const& pol,
Chris@16 684 const ::boost::math::lanczos::undefined_lanczos& l)
Chris@16 685 {
Chris@16 686 BOOST_MATH_STD_USING // ADL of std names
Chris@16 687 //
Chris@16 688 // There should be a better solution than this, but the
Chris@16 689 // algebra isn't easy for the general case....
Chris@16 690 // Start by subracting 1 from tgamma:
Chris@16 691 //
Chris@16 692 T result = gamma_imp(T(1 + dz), pol, l) - 1;
Chris@16 693 BOOST_MATH_INSTRUMENT_CODE(result);
Chris@16 694 //
Chris@16 695 // Test the level of cancellation error observed: we loose one bit
Chris@16 696 // for each power of 2 the result is less than 1. If we would get
Chris@16 697 // more bits from our most precise lgamma rational approximation,
Chris@16 698 // then use that instead:
Chris@16 699 //
Chris@16 700 BOOST_MATH_INSTRUMENT_CODE((dz > -0.5));
Chris@16 701 BOOST_MATH_INSTRUMENT_CODE((dz < 2));
Chris@16 702 BOOST_MATH_INSTRUMENT_CODE((ldexp(1.0, boost::math::policies::digits<T, Policy>()) * fabs(result) < 1e34));
Chris@16 703 if((dz > -0.5) && (dz < 2) && (ldexp(1.0, boost::math::policies::digits<T, Policy>()) * fabs(result) < 1e34))
Chris@16 704 {
Chris@16 705 result = tgammap1m1_imp(dz, pol, boost::math::lanczos::lanczos24m113());
Chris@16 706 BOOST_MATH_INSTRUMENT_CODE(result);
Chris@16 707 }
Chris@16 708 return result;
Chris@16 709 }
Chris@16 710
Chris@16 711 //
Chris@16 712 // Series representation for upper fraction when z is small:
Chris@16 713 //
Chris@16 714 template <class T>
Chris@16 715 struct small_gamma2_series
Chris@16 716 {
Chris@16 717 typedef T result_type;
Chris@16 718
Chris@16 719 small_gamma2_series(T a_, T x_) : result(-x_), x(-x_), apn(a_+1), n(1){}
Chris@16 720
Chris@16 721 T operator()()
Chris@16 722 {
Chris@16 723 T r = result / (apn);
Chris@16 724 result *= x;
Chris@16 725 result /= ++n;
Chris@16 726 apn += 1;
Chris@16 727 return r;
Chris@16 728 }
Chris@16 729
Chris@16 730 private:
Chris@16 731 T result, x, apn;
Chris@16 732 int n;
Chris@16 733 };
Chris@16 734 //
Chris@16 735 // calculate power term prefix (z^a)(e^-z) used in the non-normalised
Chris@16 736 // incomplete gammas:
Chris@16 737 //
Chris@16 738 template <class T, class Policy>
Chris@16 739 T full_igamma_prefix(T a, T z, const Policy& pol)
Chris@16 740 {
Chris@16 741 BOOST_MATH_STD_USING
Chris@16 742
Chris@16 743 T prefix;
Chris@16 744 T alz = a * log(z);
Chris@16 745
Chris@16 746 if(z >= 1)
Chris@16 747 {
Chris@16 748 if((alz < tools::log_max_value<T>()) && (-z > tools::log_min_value<T>()))
Chris@16 749 {
Chris@16 750 prefix = pow(z, a) * exp(-z);
Chris@16 751 }
Chris@16 752 else if(a >= 1)
Chris@16 753 {
Chris@16 754 prefix = pow(z / exp(z/a), a);
Chris@16 755 }
Chris@16 756 else
Chris@16 757 {
Chris@16 758 prefix = exp(alz - z);
Chris@16 759 }
Chris@16 760 }
Chris@16 761 else
Chris@16 762 {
Chris@16 763 if(alz > tools::log_min_value<T>())
Chris@16 764 {
Chris@16 765 prefix = pow(z, a) * exp(-z);
Chris@16 766 }
Chris@16 767 else if(z/a < tools::log_max_value<T>())
Chris@16 768 {
Chris@16 769 prefix = pow(z / exp(z/a), a);
Chris@16 770 }
Chris@16 771 else
Chris@16 772 {
Chris@16 773 prefix = exp(alz - z);
Chris@16 774 }
Chris@16 775 }
Chris@16 776 //
Chris@16 777 // This error handling isn't very good: it happens after the fact
Chris@16 778 // rather than before it...
Chris@16 779 //
Chris@16 780 if((boost::math::fpclassify)(prefix) == (int)FP_INFINITE)
Chris@101 781 return policies::raise_overflow_error<T>("boost::math::detail::full_igamma_prefix<%1%>(%1%, %1%)", "Result of incomplete gamma function is too large to represent.", pol);
Chris@16 782
Chris@16 783 return prefix;
Chris@16 784 }
Chris@16 785 //
Chris@16 786 // Compute (z^a)(e^-z)/tgamma(a)
Chris@16 787 // most if the error occurs in this function:
Chris@16 788 //
Chris@16 789 template <class T, class Policy, class Lanczos>
Chris@16 790 T regularised_gamma_prefix(T a, T z, const Policy& pol, const Lanczos& l)
Chris@16 791 {
Chris@16 792 BOOST_MATH_STD_USING
Chris@16 793 T agh = a + static_cast<T>(Lanczos::g()) - T(0.5);
Chris@16 794 T prefix;
Chris@16 795 T d = ((z - a) - static_cast<T>(Lanczos::g()) + T(0.5)) / agh;
Chris@16 796
Chris@16 797 if(a < 1)
Chris@16 798 {
Chris@16 799 //
Chris@16 800 // We have to treat a < 1 as a special case because our Lanczos
Chris@16 801 // approximations are optimised against the factorials with a > 1,
Chris@16 802 // and for high precision types especially (128-bit reals for example)
Chris@16 803 // very small values of a can give rather eroneous results for gamma
Chris@16 804 // unless we do this:
Chris@16 805 //
Chris@16 806 // TODO: is this still required? Lanczos approx should be better now?
Chris@16 807 //
Chris@16 808 if(z <= tools::log_min_value<T>())
Chris@16 809 {
Chris@16 810 // Oh dear, have to use logs, should be free of cancellation errors though:
Chris@16 811 return exp(a * log(z) - z - lgamma_imp(a, pol, l));
Chris@16 812 }
Chris@16 813 else
Chris@16 814 {
Chris@16 815 // direct calculation, no danger of overflow as gamma(a) < 1/a
Chris@16 816 // for small a.
Chris@16 817 return pow(z, a) * exp(-z) / gamma_imp(a, pol, l);
Chris@16 818 }
Chris@16 819 }
Chris@16 820 else if((fabs(d*d*a) <= 100) && (a > 150))
Chris@16 821 {
Chris@16 822 // special case for large a and a ~ z.
Chris@16 823 prefix = a * boost::math::log1pmx(d, pol) + z * static_cast<T>(0.5 - Lanczos::g()) / agh;
Chris@16 824 prefix = exp(prefix);
Chris@16 825 }
Chris@16 826 else
Chris@16 827 {
Chris@16 828 //
Chris@16 829 // general case.
Chris@16 830 // direct computation is most accurate, but use various fallbacks
Chris@16 831 // for different parts of the problem domain:
Chris@16 832 //
Chris@16 833 T alz = a * log(z / agh);
Chris@16 834 T amz = a - z;
Chris@16 835 if(((std::min)(alz, amz) <= tools::log_min_value<T>()) || ((std::max)(alz, amz) >= tools::log_max_value<T>()))
Chris@16 836 {
Chris@16 837 T amza = amz / a;
Chris@16 838 if(((std::min)(alz, amz)/2 > tools::log_min_value<T>()) && ((std::max)(alz, amz)/2 < tools::log_max_value<T>()))
Chris@16 839 {
Chris@16 840 // compute square root of the result and then square it:
Chris@16 841 T sq = pow(z / agh, a / 2) * exp(amz / 2);
Chris@16 842 prefix = sq * sq;
Chris@16 843 }
Chris@16 844 else if(((std::min)(alz, amz)/4 > tools::log_min_value<T>()) && ((std::max)(alz, amz)/4 < tools::log_max_value<T>()) && (z > a))
Chris@16 845 {
Chris@16 846 // compute the 4th root of the result then square it twice:
Chris@16 847 T sq = pow(z / agh, a / 4) * exp(amz / 4);
Chris@16 848 prefix = sq * sq;
Chris@16 849 prefix *= prefix;
Chris@16 850 }
Chris@16 851 else if((amza > tools::log_min_value<T>()) && (amza < tools::log_max_value<T>()))
Chris@16 852 {
Chris@16 853 prefix = pow((z * exp(amza)) / agh, a);
Chris@16 854 }
Chris@16 855 else
Chris@16 856 {
Chris@16 857 prefix = exp(alz + amz);
Chris@16 858 }
Chris@16 859 }
Chris@16 860 else
Chris@16 861 {
Chris@16 862 prefix = pow(z / agh, a) * exp(amz);
Chris@16 863 }
Chris@16 864 }
Chris@16 865 prefix *= sqrt(agh / boost::math::constants::e<T>()) / Lanczos::lanczos_sum_expG_scaled(a);
Chris@16 866 return prefix;
Chris@16 867 }
Chris@16 868 //
Chris@16 869 // And again, without Lanczos support:
Chris@16 870 //
Chris@16 871 template <class T, class Policy>
Chris@16 872 T regularised_gamma_prefix(T a, T z, const Policy& pol, const lanczos::undefined_lanczos&)
Chris@16 873 {
Chris@16 874 BOOST_MATH_STD_USING
Chris@16 875
Chris@16 876 T limit = (std::max)(T(10), a);
Chris@16 877 T sum = detail::lower_gamma_series(a, limit, pol) / a;
Chris@16 878 sum += detail::upper_gamma_fraction(a, limit, ::boost::math::policies::get_epsilon<T, Policy>());
Chris@16 879
Chris@16 880 if(a < 10)
Chris@16 881 {
Chris@16 882 // special case for small a:
Chris@16 883 T prefix = pow(z / 10, a);
Chris@16 884 prefix *= exp(10-z);
Chris@16 885 if(0 == prefix)
Chris@16 886 {
Chris@16 887 prefix = pow((z * exp((10-z)/a)) / 10, a);
Chris@16 888 }
Chris@16 889 prefix /= sum;
Chris@16 890 return prefix;
Chris@16 891 }
Chris@16 892
Chris@16 893 T zoa = z / a;
Chris@16 894 T amz = a - z;
Chris@16 895 T alzoa = a * log(zoa);
Chris@16 896 T prefix;
Chris@16 897 if(((std::min)(alzoa, amz) <= tools::log_min_value<T>()) || ((std::max)(alzoa, amz) >= tools::log_max_value<T>()))
Chris@16 898 {
Chris@16 899 T amza = amz / a;
Chris@16 900 if((amza <= tools::log_min_value<T>()) || (amza >= tools::log_max_value<T>()))
Chris@16 901 {
Chris@16 902 prefix = exp(alzoa + amz);
Chris@16 903 }
Chris@16 904 else
Chris@16 905 {
Chris@16 906 prefix = pow(zoa * exp(amza), a);
Chris@16 907 }
Chris@16 908 }
Chris@16 909 else
Chris@16 910 {
Chris@16 911 prefix = pow(zoa, a) * exp(amz);
Chris@16 912 }
Chris@16 913 prefix /= sum;
Chris@16 914 return prefix;
Chris@16 915 }
Chris@16 916 //
Chris@16 917 // Upper gamma fraction for very small a:
Chris@16 918 //
Chris@16 919 template <class T, class Policy>
Chris@16 920 inline T tgamma_small_upper_part(T a, T x, const Policy& pol, T* pgam = 0, bool invert = false, T* pderivative = 0)
Chris@16 921 {
Chris@16 922 BOOST_MATH_STD_USING // ADL of std functions.
Chris@16 923 //
Chris@16 924 // Compute the full upper fraction (Q) when a is very small:
Chris@16 925 //
Chris@16 926 T result;
Chris@16 927 result = boost::math::tgamma1pm1(a, pol);
Chris@16 928 if(pgam)
Chris@16 929 *pgam = (result + 1) / a;
Chris@16 930 T p = boost::math::powm1(x, a, pol);
Chris@16 931 result -= p;
Chris@16 932 result /= a;
Chris@16 933 detail::small_gamma2_series<T> s(a, x);
Chris@16 934 boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>() - 10;
Chris@16 935 p += 1;
Chris@16 936 if(pderivative)
Chris@16 937 *pderivative = p / (*pgam * exp(x));
Chris@16 938 T init_value = invert ? *pgam : 0;
Chris@16 939 result = -p * tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, (init_value - result) / p);
Chris@16 940 policies::check_series_iterations<T>("boost::math::tgamma_small_upper_part<%1%>(%1%, %1%)", max_iter, pol);
Chris@16 941 if(invert)
Chris@16 942 result = -result;
Chris@16 943 return result;
Chris@16 944 }
Chris@16 945 //
Chris@16 946 // Upper gamma fraction for integer a:
Chris@16 947 //
Chris@16 948 template <class T, class Policy>
Chris@16 949 inline T finite_gamma_q(T a, T x, Policy const& pol, T* pderivative = 0)
Chris@16 950 {
Chris@16 951 //
Chris@16 952 // Calculates normalised Q when a is an integer:
Chris@16 953 //
Chris@16 954 BOOST_MATH_STD_USING
Chris@16 955 T e = exp(-x);
Chris@16 956 T sum = e;
Chris@16 957 if(sum != 0)
Chris@16 958 {
Chris@16 959 T term = sum;
Chris@16 960 for(unsigned n = 1; n < a; ++n)
Chris@16 961 {
Chris@16 962 term /= n;
Chris@16 963 term *= x;
Chris@16 964 sum += term;
Chris@16 965 }
Chris@16 966 }
Chris@16 967 if(pderivative)
Chris@16 968 {
Chris@16 969 *pderivative = e * pow(x, a) / boost::math::unchecked_factorial<T>(itrunc(T(a - 1), pol));
Chris@16 970 }
Chris@16 971 return sum;
Chris@16 972 }
Chris@16 973 //
Chris@16 974 // Upper gamma fraction for half integer a:
Chris@16 975 //
Chris@16 976 template <class T, class Policy>
Chris@16 977 T finite_half_gamma_q(T a, T x, T* p_derivative, const Policy& pol)
Chris@16 978 {
Chris@16 979 //
Chris@16 980 // Calculates normalised Q when a is a half-integer:
Chris@16 981 //
Chris@16 982 BOOST_MATH_STD_USING
Chris@16 983 T e = boost::math::erfc(sqrt(x), pol);
Chris@16 984 if((e != 0) && (a > 1))
Chris@16 985 {
Chris@16 986 T term = exp(-x) / sqrt(constants::pi<T>() * x);
Chris@16 987 term *= x;
Chris@16 988 static const T half = T(1) / 2;
Chris@16 989 term /= half;
Chris@16 990 T sum = term;
Chris@16 991 for(unsigned n = 2; n < a; ++n)
Chris@16 992 {
Chris@16 993 term /= n - half;
Chris@16 994 term *= x;
Chris@16 995 sum += term;
Chris@16 996 }
Chris@16 997 e += sum;
Chris@16 998 if(p_derivative)
Chris@16 999 {
Chris@16 1000 *p_derivative = 0;
Chris@16 1001 }
Chris@16 1002 }
Chris@16 1003 else if(p_derivative)
Chris@16 1004 {
Chris@16 1005 // We'll be dividing by x later, so calculate derivative * x:
Chris@16 1006 *p_derivative = sqrt(x) * exp(-x) / constants::root_pi<T>();
Chris@16 1007 }
Chris@16 1008 return e;
Chris@16 1009 }
Chris@16 1010 //
Chris@16 1011 // Main incomplete gamma entry point, handles all four incomplete gamma's:
Chris@16 1012 //
Chris@16 1013 template <class T, class Policy>
Chris@16 1014 T gamma_incomplete_imp(T a, T x, bool normalised, bool invert,
Chris@16 1015 const Policy& pol, T* p_derivative)
Chris@16 1016 {
Chris@16 1017 static const char* function = "boost::math::gamma_p<%1%>(%1%, %1%)";
Chris@16 1018 if(a <= 0)
Chris@101 1019 return policies::raise_domain_error<T>(function, "Argument a to the incomplete gamma function must be greater than zero (got a=%1%).", a, pol);
Chris@16 1020 if(x < 0)
Chris@101 1021 return policies::raise_domain_error<T>(function, "Argument x to the incomplete gamma function must be >= 0 (got x=%1%).", x, pol);
Chris@16 1022
Chris@16 1023 BOOST_MATH_STD_USING
Chris@16 1024
Chris@16 1025 typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
Chris@16 1026
Chris@16 1027 T result = 0; // Just to avoid warning C4701: potentially uninitialized local variable 'result' used
Chris@16 1028
Chris@101 1029 if(a >= max_factorial<T>::value && !normalised)
Chris@101 1030 {
Chris@101 1031 //
Chris@101 1032 // When we're computing the non-normalized incomplete gamma
Chris@101 1033 // and a is large the result is rather hard to compute unless
Chris@101 1034 // we use logs. There are really two options - if x is a long
Chris@101 1035 // way from a in value then we can reliably use methods 2 and 4
Chris@101 1036 // below in logarithmic form and go straight to the result.
Chris@101 1037 // Otherwise we let the regularized gamma take the strain
Chris@101 1038 // (the result is unlikely to unerflow in the central region anyway)
Chris@101 1039 // and combine with lgamma in the hopes that we get a finite result.
Chris@101 1040 //
Chris@101 1041 if(invert && (a * 4 < x))
Chris@101 1042 {
Chris@101 1043 // This is method 4 below, done in logs:
Chris@101 1044 result = a * log(x) - x;
Chris@101 1045 if(p_derivative)
Chris@101 1046 *p_derivative = exp(result);
Chris@101 1047 result += log(upper_gamma_fraction(a, x, policies::get_epsilon<T, Policy>()));
Chris@101 1048 }
Chris@101 1049 else if(!invert && (a > 4 * x))
Chris@101 1050 {
Chris@101 1051 // This is method 2 below, done in logs:
Chris@101 1052 result = a * log(x) - x;
Chris@101 1053 if(p_derivative)
Chris@101 1054 *p_derivative = exp(result);
Chris@101 1055 T init_value = 0;
Chris@101 1056 result += log(detail::lower_gamma_series(a, x, pol, init_value) / a);
Chris@101 1057 }
Chris@101 1058 else
Chris@101 1059 {
Chris@101 1060 result = gamma_incomplete_imp(a, x, true, invert, pol, p_derivative);
Chris@101 1061 if(result == 0)
Chris@101 1062 {
Chris@101 1063 if(invert)
Chris@101 1064 {
Chris@101 1065 // Try http://functions.wolfram.com/06.06.06.0039.01
Chris@101 1066 result = 1 + 1 / (12 * a) + 1 / (288 * a * a);
Chris@101 1067 result = log(result) - a + (a - 0.5f) * log(a) + log(boost::math::constants::root_two_pi<T>());
Chris@101 1068 if(p_derivative)
Chris@101 1069 *p_derivative = exp(a * log(x) - x);
Chris@101 1070 }
Chris@101 1071 else
Chris@101 1072 {
Chris@101 1073 // This is method 2 below, done in logs, we're really outside the
Chris@101 1074 // range of this method, but since the result is almost certainly
Chris@101 1075 // infinite, we should probably be OK:
Chris@101 1076 result = a * log(x) - x;
Chris@101 1077 if(p_derivative)
Chris@101 1078 *p_derivative = exp(result);
Chris@101 1079 T init_value = 0;
Chris@101 1080 result += log(detail::lower_gamma_series(a, x, pol, init_value) / a);
Chris@101 1081 }
Chris@101 1082 }
Chris@101 1083 else
Chris@101 1084 {
Chris@101 1085 result = log(result) + boost::math::lgamma(a, pol);
Chris@101 1086 }
Chris@101 1087 }
Chris@101 1088 if(result > tools::log_max_value<T>())
Chris@101 1089 return policies::raise_overflow_error<T>(function, 0, pol);
Chris@101 1090 return exp(result);
Chris@101 1091 }
Chris@101 1092
Chris@16 1093 BOOST_ASSERT((p_derivative == 0) || (normalised == true));
Chris@16 1094
Chris@16 1095 bool is_int, is_half_int;
Chris@16 1096 bool is_small_a = (a < 30) && (a <= x + 1) && (x < tools::log_max_value<T>());
Chris@16 1097 if(is_small_a)
Chris@16 1098 {
Chris@16 1099 T fa = floor(a);
Chris@16 1100 is_int = (fa == a);
Chris@16 1101 is_half_int = is_int ? false : (fabs(fa - a) == 0.5f);
Chris@16 1102 }
Chris@16 1103 else
Chris@16 1104 {
Chris@16 1105 is_int = is_half_int = false;
Chris@16 1106 }
Chris@16 1107
Chris@16 1108 int eval_method;
Chris@16 1109
Chris@16 1110 if(is_int && (x > 0.6))
Chris@16 1111 {
Chris@16 1112 // calculate Q via finite sum:
Chris@16 1113 invert = !invert;
Chris@16 1114 eval_method = 0;
Chris@16 1115 }
Chris@16 1116 else if(is_half_int && (x > 0.2))
Chris@16 1117 {
Chris@16 1118 // calculate Q via finite sum for half integer a:
Chris@16 1119 invert = !invert;
Chris@16 1120 eval_method = 1;
Chris@16 1121 }
Chris@101 1122 else if((x < tools::root_epsilon<T>()) && (a > 1))
Chris@101 1123 {
Chris@101 1124 eval_method = 6;
Chris@101 1125 }
Chris@16 1126 else if(x < 0.5)
Chris@16 1127 {
Chris@16 1128 //
Chris@16 1129 // Changeover criterion chosen to give a changeover at Q ~ 0.33
Chris@16 1130 //
Chris@16 1131 if(-0.4 / log(x) < a)
Chris@16 1132 {
Chris@16 1133 eval_method = 2;
Chris@16 1134 }
Chris@16 1135 else
Chris@16 1136 {
Chris@16 1137 eval_method = 3;
Chris@16 1138 }
Chris@16 1139 }
Chris@16 1140 else if(x < 1.1)
Chris@16 1141 {
Chris@16 1142 //
Chris@16 1143 // Changover here occurs when P ~ 0.75 or Q ~ 0.25:
Chris@16 1144 //
Chris@16 1145 if(x * 0.75f < a)
Chris@16 1146 {
Chris@16 1147 eval_method = 2;
Chris@16 1148 }
Chris@16 1149 else
Chris@16 1150 {
Chris@16 1151 eval_method = 3;
Chris@16 1152 }
Chris@16 1153 }
Chris@16 1154 else
Chris@16 1155 {
Chris@16 1156 //
Chris@16 1157 // Begin by testing whether we're in the "bad" zone
Chris@16 1158 // where the result will be near 0.5 and the usual
Chris@16 1159 // series and continued fractions are slow to converge:
Chris@16 1160 //
Chris@16 1161 bool use_temme = false;
Chris@16 1162 if(normalised && std::numeric_limits<T>::is_specialized && (a > 20))
Chris@16 1163 {
Chris@16 1164 T sigma = fabs((x-a)/a);
Chris@16 1165 if((a > 200) && (policies::digits<T, Policy>() <= 113))
Chris@16 1166 {
Chris@16 1167 //
Chris@16 1168 // This limit is chosen so that we use Temme's expansion
Chris@16 1169 // only if the result would be larger than about 10^-6.
Chris@16 1170 // Below that the regular series and continued fractions
Chris@16 1171 // converge OK, and if we use Temme's method we get increasing
Chris@16 1172 // errors from the dominant erfc term as it's (inexact) argument
Chris@16 1173 // increases in magnitude.
Chris@16 1174 //
Chris@16 1175 if(20 / a > sigma * sigma)
Chris@16 1176 use_temme = true;
Chris@16 1177 }
Chris@16 1178 else if(policies::digits<T, Policy>() <= 64)
Chris@16 1179 {
Chris@16 1180 // Note in this zone we can't use Temme's expansion for
Chris@16 1181 // types longer than an 80-bit real:
Chris@16 1182 // it would require too many terms in the polynomials.
Chris@16 1183 if(sigma < 0.4)
Chris@16 1184 use_temme = true;
Chris@16 1185 }
Chris@16 1186 }
Chris@16 1187 if(use_temme)
Chris@16 1188 {
Chris@16 1189 eval_method = 5;
Chris@16 1190 }
Chris@16 1191 else
Chris@16 1192 {
Chris@16 1193 //
Chris@16 1194 // Regular case where the result will not be too close to 0.5.
Chris@16 1195 //
Chris@16 1196 // Changeover here occurs at P ~ Q ~ 0.5
Chris@16 1197 // Note that series computation of P is about x2 faster than continued fraction
Chris@16 1198 // calculation of Q, so try and use the CF only when really necessary, especially
Chris@16 1199 // for small x.
Chris@16 1200 //
Chris@16 1201 if(x - (1 / (3 * x)) < a)
Chris@16 1202 {
Chris@16 1203 eval_method = 2;
Chris@16 1204 }
Chris@16 1205 else
Chris@16 1206 {
Chris@16 1207 eval_method = 4;
Chris@16 1208 invert = !invert;
Chris@16 1209 }
Chris@16 1210 }
Chris@16 1211 }
Chris@16 1212
Chris@16 1213 switch(eval_method)
Chris@16 1214 {
Chris@16 1215 case 0:
Chris@16 1216 {
Chris@16 1217 result = finite_gamma_q(a, x, pol, p_derivative);
Chris@16 1218 if(normalised == false)
Chris@16 1219 result *= boost::math::tgamma(a, pol);
Chris@16 1220 break;
Chris@16 1221 }
Chris@16 1222 case 1:
Chris@16 1223 {
Chris@16 1224 result = finite_half_gamma_q(a, x, p_derivative, pol);
Chris@16 1225 if(normalised == false)
Chris@16 1226 result *= boost::math::tgamma(a, pol);
Chris@16 1227 if(p_derivative && (*p_derivative == 0))
Chris@16 1228 *p_derivative = regularised_gamma_prefix(a, x, pol, lanczos_type());
Chris@16 1229 break;
Chris@16 1230 }
Chris@16 1231 case 2:
Chris@16 1232 {
Chris@16 1233 // Compute P:
Chris@16 1234 result = normalised ? regularised_gamma_prefix(a, x, pol, lanczos_type()) : full_igamma_prefix(a, x, pol);
Chris@16 1235 if(p_derivative)
Chris@16 1236 *p_derivative = result;
Chris@16 1237 if(result != 0)
Chris@16 1238 {
Chris@101 1239 //
Chris@101 1240 // If we're going to be inverting the result then we can
Chris@101 1241 // reduce the number of series evaluations by quite
Chris@101 1242 // a few iterations if we set an initial value for the
Chris@101 1243 // series sum based on what we'll end up subtracting it from
Chris@101 1244 // at the end.
Chris@101 1245 // Have to be careful though that this optimization doesn't
Chris@101 1246 // lead to spurious numberic overflow. Note that the
Chris@101 1247 // scary/expensive overflow checks below are more often
Chris@101 1248 // than not bypassed in practice for "sensible" input
Chris@101 1249 // values:
Chris@101 1250 //
Chris@16 1251 T init_value = 0;
Chris@101 1252 bool optimised_invert = false;
Chris@16 1253 if(invert)
Chris@16 1254 {
Chris@101 1255 init_value = (normalised ? 1 : boost::math::tgamma(a, pol));
Chris@101 1256 if(normalised || (result >= 1) || (tools::max_value<T>() * result > init_value))
Chris@101 1257 {
Chris@101 1258 init_value /= result;
Chris@101 1259 if(normalised || (a < 1) || (tools::max_value<T>() / a > init_value))
Chris@101 1260 {
Chris@101 1261 init_value *= -a;
Chris@101 1262 optimised_invert = true;
Chris@101 1263 }
Chris@101 1264 else
Chris@101 1265 init_value = 0;
Chris@101 1266 }
Chris@101 1267 else
Chris@101 1268 init_value = 0;
Chris@16 1269 }
Chris@16 1270 result *= detail::lower_gamma_series(a, x, pol, init_value) / a;
Chris@101 1271 if(optimised_invert)
Chris@16 1272 {
Chris@16 1273 invert = false;
Chris@16 1274 result = -result;
Chris@16 1275 }
Chris@16 1276 }
Chris@16 1277 break;
Chris@16 1278 }
Chris@16 1279 case 3:
Chris@16 1280 {
Chris@16 1281 // Compute Q:
Chris@16 1282 invert = !invert;
Chris@16 1283 T g;
Chris@16 1284 result = tgamma_small_upper_part(a, x, pol, &g, invert, p_derivative);
Chris@16 1285 invert = false;
Chris@16 1286 if(normalised)
Chris@16 1287 result /= g;
Chris@16 1288 break;
Chris@16 1289 }
Chris@16 1290 case 4:
Chris@16 1291 {
Chris@16 1292 // Compute Q:
Chris@16 1293 result = normalised ? regularised_gamma_prefix(a, x, pol, lanczos_type()) : full_igamma_prefix(a, x, pol);
Chris@16 1294 if(p_derivative)
Chris@16 1295 *p_derivative = result;
Chris@16 1296 if(result != 0)
Chris@16 1297 result *= upper_gamma_fraction(a, x, policies::get_epsilon<T, Policy>());
Chris@16 1298 break;
Chris@16 1299 }
Chris@16 1300 case 5:
Chris@16 1301 {
Chris@16 1302 //
Chris@16 1303 // Use compile time dispatch to the appropriate
Chris@16 1304 // Temme asymptotic expansion. This may be dead code
Chris@16 1305 // if T does not have numeric limits support, or has
Chris@16 1306 // too many digits for the most precise version of
Chris@16 1307 // these expansions, in that case we'll be calling
Chris@16 1308 // an empty function.
Chris@16 1309 //
Chris@16 1310 typedef typename policies::precision<T, Policy>::type precision_type;
Chris@16 1311
Chris@16 1312 typedef typename mpl::if_<
Chris@16 1313 mpl::or_<mpl::equal_to<precision_type, mpl::int_<0> >,
Chris@16 1314 mpl::greater<precision_type, mpl::int_<113> > >,
Chris@16 1315 mpl::int_<0>,
Chris@16 1316 typename mpl::if_<
Chris@16 1317 mpl::less_equal<precision_type, mpl::int_<53> >,
Chris@16 1318 mpl::int_<53>,
Chris@16 1319 typename mpl::if_<
Chris@16 1320 mpl::less_equal<precision_type, mpl::int_<64> >,
Chris@16 1321 mpl::int_<64>,
Chris@16 1322 mpl::int_<113>
Chris@16 1323 >::type
Chris@16 1324 >::type
Chris@16 1325 >::type tag_type;
Chris@16 1326
Chris@16 1327 result = igamma_temme_large(a, x, pol, static_cast<tag_type const*>(0));
Chris@16 1328 if(x >= a)
Chris@16 1329 invert = !invert;
Chris@16 1330 if(p_derivative)
Chris@16 1331 *p_derivative = regularised_gamma_prefix(a, x, pol, lanczos_type());
Chris@16 1332 break;
Chris@16 1333 }
Chris@101 1334 case 6:
Chris@101 1335 {
Chris@101 1336 // x is so small that P is necessarily very small too,
Chris@101 1337 // use http://functions.wolfram.com/GammaBetaErf/GammaRegularized/06/01/05/01/01/
Chris@101 1338 result = !normalised ? pow(x, a) / (a) : pow(x, a) / boost::math::tgamma(a + 1, pol);
Chris@101 1339 result *= 1 - a * x / (a + 1);
Chris@101 1340 }
Chris@16 1341 }
Chris@16 1342
Chris@16 1343 if(normalised && (result > 1))
Chris@16 1344 result = 1;
Chris@16 1345 if(invert)
Chris@16 1346 {
Chris@16 1347 T gam = normalised ? 1 : boost::math::tgamma(a, pol);
Chris@16 1348 result = gam - result;
Chris@16 1349 }
Chris@16 1350 if(p_derivative)
Chris@16 1351 {
Chris@16 1352 //
Chris@16 1353 // Need to convert prefix term to derivative:
Chris@16 1354 //
Chris@16 1355 if((x < 1) && (tools::max_value<T>() * x < *p_derivative))
Chris@16 1356 {
Chris@16 1357 // overflow, just return an arbitrarily large value:
Chris@16 1358 *p_derivative = tools::max_value<T>() / 2;
Chris@16 1359 }
Chris@16 1360
Chris@16 1361 *p_derivative /= x;
Chris@16 1362 }
Chris@16 1363
Chris@16 1364 return result;
Chris@16 1365 }
Chris@16 1366
Chris@16 1367 //
Chris@16 1368 // Ratios of two gamma functions:
Chris@16 1369 //
Chris@16 1370 template <class T, class Policy, class Lanczos>
Chris@101 1371 T tgamma_delta_ratio_imp_lanczos(T z, T delta, const Policy& pol, const Lanczos& l)
Chris@16 1372 {
Chris@16 1373 BOOST_MATH_STD_USING
Chris@101 1374 if(z < tools::epsilon<T>())
Chris@101 1375 {
Chris@101 1376 //
Chris@101 1377 // We get spurious numeric overflow unless we're very careful, this
Chris@101 1378 // can occur either inside Lanczos::lanczos_sum(z) or in the
Chris@101 1379 // final combination of terms, to avoid this, split the product up
Chris@101 1380 // into 2 (or 3) parts:
Chris@101 1381 //
Chris@101 1382 // G(z) / G(L) = 1 / (z * G(L)) ; z < eps, L = z + delta = delta
Chris@101 1383 // z * G(L) = z * G(lim) * (G(L)/G(lim)) ; lim = largest factorial
Chris@101 1384 //
Chris@101 1385 if(boost::math::max_factorial<T>::value < delta)
Chris@101 1386 {
Chris@101 1387 T ratio = tgamma_delta_ratio_imp_lanczos(delta, T(boost::math::max_factorial<T>::value - delta), pol, l);
Chris@101 1388 ratio *= z;
Chris@101 1389 ratio *= boost::math::unchecked_factorial<T>(boost::math::max_factorial<T>::value - 1);
Chris@101 1390 return 1 / ratio;
Chris@101 1391 }
Chris@101 1392 else
Chris@101 1393 {
Chris@101 1394 return 1 / (z * boost::math::tgamma(z + delta, pol));
Chris@101 1395 }
Chris@101 1396 }
Chris@16 1397 T zgh = z + Lanczos::g() - constants::half<T>();
Chris@16 1398 T result;
Chris@16 1399 if(fabs(delta) < 10)
Chris@16 1400 {
Chris@16 1401 result = exp((constants::half<T>() - z) * boost::math::log1p(delta / zgh, pol));
Chris@16 1402 }
Chris@16 1403 else
Chris@16 1404 {
Chris@16 1405 result = pow(zgh / (zgh + delta), z - constants::half<T>());
Chris@16 1406 }
Chris@101 1407 // Split the calculation up to avoid spurious overflow:
Chris@101 1408 result *= Lanczos::lanczos_sum(z) / Lanczos::lanczos_sum(T(z + delta));
Chris@16 1409 result *= pow(constants::e<T>() / (zgh + delta), delta);
Chris@16 1410 return result;
Chris@16 1411 }
Chris@16 1412 //
Chris@16 1413 // And again without Lanczos support this time:
Chris@16 1414 //
Chris@16 1415 template <class T, class Policy>
Chris@16 1416 T tgamma_delta_ratio_imp_lanczos(T z, T delta, const Policy& pol, const lanczos::undefined_lanczos&)
Chris@16 1417 {
Chris@16 1418 BOOST_MATH_STD_USING
Chris@16 1419 //
Chris@16 1420 // The upper gamma fraction is *very* slow for z < 6, actually it's very
Chris@16 1421 // slow to converge everywhere but recursing until z > 6 gets rid of the
Chris@16 1422 // worst of it's behaviour.
Chris@16 1423 //
Chris@16 1424 T prefix = 1;
Chris@16 1425 T zd = z + delta;
Chris@16 1426 while((zd < 6) && (z < 6))
Chris@16 1427 {
Chris@16 1428 prefix /= z;
Chris@16 1429 prefix *= zd;
Chris@16 1430 z += 1;
Chris@16 1431 zd += 1;
Chris@16 1432 }
Chris@16 1433 if(delta < 10)
Chris@16 1434 {
Chris@16 1435 prefix *= exp(-z * boost::math::log1p(delta / z, pol));
Chris@16 1436 }
Chris@16 1437 else
Chris@16 1438 {
Chris@16 1439 prefix *= pow(z / zd, z);
Chris@16 1440 }
Chris@16 1441 prefix *= pow(constants::e<T>() / zd, delta);
Chris@16 1442 T sum = detail::lower_gamma_series(z, z, pol) / z;
Chris@16 1443 sum += detail::upper_gamma_fraction(z, z, ::boost::math::policies::get_epsilon<T, Policy>());
Chris@16 1444 T sumd = detail::lower_gamma_series(zd, zd, pol) / zd;
Chris@16 1445 sumd += detail::upper_gamma_fraction(zd, zd, ::boost::math::policies::get_epsilon<T, Policy>());
Chris@16 1446 sum /= sumd;
Chris@16 1447 if(fabs(tools::max_value<T>() / prefix) < fabs(sum))
Chris@16 1448 return policies::raise_overflow_error<T>("boost::math::tgamma_delta_ratio<%1%>(%1%, %1%)", "Result of tgamma is too large to represent.", pol);
Chris@16 1449 return sum * prefix;
Chris@16 1450 }
Chris@16 1451
Chris@16 1452 template <class T, class Policy>
Chris@16 1453 T tgamma_delta_ratio_imp(T z, T delta, const Policy& pol)
Chris@16 1454 {
Chris@16 1455 BOOST_MATH_STD_USING
Chris@16 1456
Chris@101 1457 if((z <= 0) || (z + delta <= 0))
Chris@101 1458 {
Chris@101 1459 // This isn't very sofisticated, or accurate, but it does work:
Chris@101 1460 return boost::math::tgamma(z, pol) / boost::math::tgamma(z + delta, pol);
Chris@101 1461 }
Chris@16 1462
Chris@16 1463 if(floor(delta) == delta)
Chris@16 1464 {
Chris@16 1465 if(floor(z) == z)
Chris@16 1466 {
Chris@16 1467 //
Chris@16 1468 // Both z and delta are integers, see if we can just use table lookup
Chris@16 1469 // of the factorials to get the result:
Chris@16 1470 //
Chris@16 1471 if((z <= max_factorial<T>::value) && (z + delta <= max_factorial<T>::value))
Chris@16 1472 {
Chris@16 1473 return unchecked_factorial<T>((unsigned)itrunc(z, pol) - 1) / unchecked_factorial<T>((unsigned)itrunc(T(z + delta), pol) - 1);
Chris@16 1474 }
Chris@16 1475 }
Chris@16 1476 if(fabs(delta) < 20)
Chris@16 1477 {
Chris@16 1478 //
Chris@16 1479 // delta is a small integer, we can use a finite product:
Chris@16 1480 //
Chris@16 1481 if(delta == 0)
Chris@16 1482 return 1;
Chris@16 1483 if(delta < 0)
Chris@16 1484 {
Chris@16 1485 z -= 1;
Chris@16 1486 T result = z;
Chris@16 1487 while(0 != (delta += 1))
Chris@16 1488 {
Chris@16 1489 z -= 1;
Chris@16 1490 result *= z;
Chris@16 1491 }
Chris@16 1492 return result;
Chris@16 1493 }
Chris@16 1494 else
Chris@16 1495 {
Chris@16 1496 T result = 1 / z;
Chris@16 1497 while(0 != (delta -= 1))
Chris@16 1498 {
Chris@16 1499 z += 1;
Chris@16 1500 result /= z;
Chris@16 1501 }
Chris@16 1502 return result;
Chris@16 1503 }
Chris@16 1504 }
Chris@16 1505 }
Chris@16 1506 typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
Chris@16 1507 return tgamma_delta_ratio_imp_lanczos(z, delta, pol, lanczos_type());
Chris@16 1508 }
Chris@16 1509
Chris@16 1510 template <class T, class Policy>
Chris@16 1511 T tgamma_ratio_imp(T x, T y, const Policy& pol)
Chris@16 1512 {
Chris@16 1513 BOOST_MATH_STD_USING
Chris@16 1514
Chris@101 1515 if((x <= 0) || (boost::math::isinf)(x))
Chris@101 1516 return policies::raise_domain_error<T>("boost::math::tgamma_ratio<%1%>(%1%, %1%)", "Gamma function ratios only implemented for positive arguments (got a=%1%).", x, pol);
Chris@101 1517 if((y <= 0) || (boost::math::isinf)(y))
Chris@101 1518 return policies::raise_domain_error<T>("boost::math::tgamma_ratio<%1%>(%1%, %1%)", "Gamma function ratios only implemented for positive arguments (got b=%1%).", y, pol);
Chris@101 1519
Chris@101 1520 if(x <= tools::min_value<T>())
Chris@101 1521 {
Chris@101 1522 // Special case for denorms...Ugh.
Chris@101 1523 T shift = ldexp(T(1), tools::digits<T>());
Chris@101 1524 return shift * tgamma_ratio_imp(T(x * shift), y, pol);
Chris@101 1525 }
Chris@16 1526
Chris@16 1527 if((x < max_factorial<T>::value) && (y < max_factorial<T>::value))
Chris@16 1528 {
Chris@16 1529 // Rather than subtracting values, lets just call the gamma functions directly:
Chris@16 1530 return boost::math::tgamma(x, pol) / boost::math::tgamma(y, pol);
Chris@16 1531 }
Chris@16 1532 T prefix = 1;
Chris@16 1533 if(x < 1)
Chris@16 1534 {
Chris@16 1535 if(y < 2 * max_factorial<T>::value)
Chris@16 1536 {
Chris@16 1537 // We need to sidestep on x as well, otherwise we'll underflow
Chris@16 1538 // before we get to factor in the prefix term:
Chris@16 1539 prefix /= x;
Chris@16 1540 x += 1;
Chris@16 1541 while(y >= max_factorial<T>::value)
Chris@16 1542 {
Chris@16 1543 y -= 1;
Chris@16 1544 prefix /= y;
Chris@16 1545 }
Chris@16 1546 return prefix * boost::math::tgamma(x, pol) / boost::math::tgamma(y, pol);
Chris@16 1547 }
Chris@16 1548 //
Chris@16 1549 // result is almost certainly going to underflow to zero, try logs just in case:
Chris@16 1550 //
Chris@16 1551 return exp(boost::math::lgamma(x, pol) - boost::math::lgamma(y, pol));
Chris@16 1552 }
Chris@16 1553 if(y < 1)
Chris@16 1554 {
Chris@16 1555 if(x < 2 * max_factorial<T>::value)
Chris@16 1556 {
Chris@16 1557 // We need to sidestep on y as well, otherwise we'll overflow
Chris@16 1558 // before we get to factor in the prefix term:
Chris@16 1559 prefix *= y;
Chris@16 1560 y += 1;
Chris@16 1561 while(x >= max_factorial<T>::value)
Chris@16 1562 {
Chris@16 1563 x -= 1;
Chris@16 1564 prefix *= x;
Chris@16 1565 }
Chris@16 1566 return prefix * boost::math::tgamma(x, pol) / boost::math::tgamma(y, pol);
Chris@16 1567 }
Chris@16 1568 //
Chris@16 1569 // Result will almost certainly overflow, try logs just in case:
Chris@16 1570 //
Chris@16 1571 return exp(boost::math::lgamma(x, pol) - boost::math::lgamma(y, pol));
Chris@16 1572 }
Chris@16 1573 //
Chris@16 1574 // Regular case, x and y both large and similar in magnitude:
Chris@16 1575 //
Chris@16 1576 return boost::math::tgamma_delta_ratio(x, y - x, pol);
Chris@16 1577 }
Chris@16 1578
Chris@16 1579 template <class T, class Policy>
Chris@16 1580 T gamma_p_derivative_imp(T a, T x, const Policy& pol)
Chris@16 1581 {
Chris@101 1582 BOOST_MATH_STD_USING
Chris@16 1583 //
Chris@16 1584 // Usual error checks first:
Chris@16 1585 //
Chris@16 1586 if(a <= 0)
Chris@101 1587 return policies::raise_domain_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", "Argument a to the incomplete gamma function must be greater than zero (got a=%1%).", a, pol);
Chris@16 1588 if(x < 0)
Chris@101 1589 return policies::raise_domain_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", "Argument x to the incomplete gamma function must be >= 0 (got x=%1%).", x, pol);
Chris@16 1590 //
Chris@16 1591 // Now special cases:
Chris@16 1592 //
Chris@16 1593 if(x == 0)
Chris@16 1594 {
Chris@16 1595 return (a > 1) ? 0 :
Chris@16 1596 (a == 1) ? 1 : policies::raise_overflow_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", 0, pol);
Chris@16 1597 }
Chris@16 1598 //
Chris@16 1599 // Normal case:
Chris@16 1600 //
Chris@16 1601 typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
Chris@16 1602 T f1 = detail::regularised_gamma_prefix(a, x, pol, lanczos_type());
Chris@16 1603 if((x < 1) && (tools::max_value<T>() * x < f1))
Chris@16 1604 {
Chris@16 1605 // overflow:
Chris@16 1606 return policies::raise_overflow_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", 0, pol);
Chris@16 1607 }
Chris@101 1608 if(f1 == 0)
Chris@101 1609 {
Chris@101 1610 // Underflow in calculation, use logs instead:
Chris@101 1611 f1 = a * log(x) - x - lgamma(a, pol) - log(x);
Chris@101 1612 f1 = exp(f1);
Chris@101 1613 }
Chris@101 1614 else
Chris@101 1615 f1 /= x;
Chris@16 1616
Chris@16 1617 return f1;
Chris@16 1618 }
Chris@16 1619
Chris@16 1620 template <class T, class Policy>
Chris@16 1621 inline typename tools::promote_args<T>::type
Chris@16 1622 tgamma(T z, const Policy& /* pol */, const mpl::true_)
Chris@16 1623 {
Chris@16 1624 BOOST_FPU_EXCEPTION_GUARD
Chris@16 1625 typedef typename tools::promote_args<T>::type result_type;
Chris@16 1626 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 1627 typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
Chris@16 1628 typedef typename policies::normalise<
Chris@16 1629 Policy,
Chris@16 1630 policies::promote_float<false>,
Chris@16 1631 policies::promote_double<false>,
Chris@16 1632 policies::discrete_quantile<>,
Chris@16 1633 policies::assert_undefined<> >::type forwarding_policy;
Chris@16 1634 return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::gamma_imp(static_cast<value_type>(z), forwarding_policy(), evaluation_type()), "boost::math::tgamma<%1%>(%1%)");
Chris@16 1635 }
Chris@16 1636
Chris@16 1637 template <class T, class Policy>
Chris@16 1638 struct igamma_initializer
Chris@16 1639 {
Chris@16 1640 struct init
Chris@16 1641 {
Chris@16 1642 init()
Chris@16 1643 {
Chris@16 1644 typedef typename policies::precision<T, Policy>::type precision_type;
Chris@16 1645
Chris@16 1646 typedef typename mpl::if_<
Chris@16 1647 mpl::or_<mpl::equal_to<precision_type, mpl::int_<0> >,
Chris@16 1648 mpl::greater<precision_type, mpl::int_<113> > >,
Chris@16 1649 mpl::int_<0>,
Chris@16 1650 typename mpl::if_<
Chris@16 1651 mpl::less_equal<precision_type, mpl::int_<53> >,
Chris@16 1652 mpl::int_<53>,
Chris@16 1653 typename mpl::if_<
Chris@16 1654 mpl::less_equal<precision_type, mpl::int_<64> >,
Chris@16 1655 mpl::int_<64>,
Chris@16 1656 mpl::int_<113>
Chris@16 1657 >::type
Chris@16 1658 >::type
Chris@16 1659 >::type tag_type;
Chris@16 1660
Chris@16 1661 do_init(tag_type());
Chris@16 1662 }
Chris@16 1663 template <int N>
Chris@16 1664 static void do_init(const mpl::int_<N>&)
Chris@16 1665 {
Chris@16 1666 boost::math::gamma_p(static_cast<T>(400), static_cast<T>(400), Policy());
Chris@16 1667 }
Chris@16 1668 static void do_init(const mpl::int_<53>&){}
Chris@16 1669 void force_instantiate()const{}
Chris@16 1670 };
Chris@16 1671 static const init initializer;
Chris@16 1672 static void force_instantiate()
Chris@16 1673 {
Chris@16 1674 initializer.force_instantiate();
Chris@16 1675 }
Chris@16 1676 };
Chris@16 1677
Chris@16 1678 template <class T, class Policy>
Chris@16 1679 const typename igamma_initializer<T, Policy>::init igamma_initializer<T, Policy>::initializer;
Chris@16 1680
Chris@16 1681 template <class T, class Policy>
Chris@16 1682 struct lgamma_initializer
Chris@16 1683 {
Chris@16 1684 struct init
Chris@16 1685 {
Chris@16 1686 init()
Chris@16 1687 {
Chris@16 1688 typedef typename policies::precision<T, Policy>::type precision_type;
Chris@16 1689 typedef typename mpl::if_<
Chris@16 1690 mpl::and_<
Chris@16 1691 mpl::less_equal<precision_type, mpl::int_<64> >,
Chris@16 1692 mpl::greater<precision_type, mpl::int_<0> >
Chris@16 1693 >,
Chris@16 1694 mpl::int_<64>,
Chris@16 1695 typename mpl::if_<
Chris@16 1696 mpl::and_<
Chris@16 1697 mpl::less_equal<precision_type, mpl::int_<113> >,
Chris@16 1698 mpl::greater<precision_type, mpl::int_<0> >
Chris@16 1699 >,
Chris@16 1700 mpl::int_<113>, mpl::int_<0> >::type
Chris@16 1701 >::type tag_type;
Chris@16 1702 do_init(tag_type());
Chris@16 1703 }
Chris@16 1704 static void do_init(const mpl::int_<64>&)
Chris@16 1705 {
Chris@16 1706 boost::math::lgamma(static_cast<T>(2.5), Policy());
Chris@16 1707 boost::math::lgamma(static_cast<T>(1.25), Policy());
Chris@16 1708 boost::math::lgamma(static_cast<T>(1.75), Policy());
Chris@16 1709 }
Chris@16 1710 static void do_init(const mpl::int_<113>&)
Chris@16 1711 {
Chris@16 1712 boost::math::lgamma(static_cast<T>(2.5), Policy());
Chris@16 1713 boost::math::lgamma(static_cast<T>(1.25), Policy());
Chris@16 1714 boost::math::lgamma(static_cast<T>(1.5), Policy());
Chris@16 1715 boost::math::lgamma(static_cast<T>(1.75), Policy());
Chris@16 1716 }
Chris@16 1717 static void do_init(const mpl::int_<0>&)
Chris@16 1718 {
Chris@16 1719 }
Chris@16 1720 void force_instantiate()const{}
Chris@16 1721 };
Chris@16 1722 static const init initializer;
Chris@16 1723 static void force_instantiate()
Chris@16 1724 {
Chris@16 1725 initializer.force_instantiate();
Chris@16 1726 }
Chris@16 1727 };
Chris@16 1728
Chris@16 1729 template <class T, class Policy>
Chris@16 1730 const typename lgamma_initializer<T, Policy>::init lgamma_initializer<T, Policy>::initializer;
Chris@16 1731
Chris@16 1732 template <class T1, class T2, class Policy>
Chris@16 1733 inline typename tools::promote_args<T1, T2>::type
Chris@16 1734 tgamma(T1 a, T2 z, const Policy&, const mpl::false_)
Chris@16 1735 {
Chris@16 1736 BOOST_FPU_EXCEPTION_GUARD
Chris@16 1737 typedef typename tools::promote_args<T1, T2>::type result_type;
Chris@16 1738 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 1739 // typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
Chris@16 1740 typedef typename policies::normalise<
Chris@16 1741 Policy,
Chris@16 1742 policies::promote_float<false>,
Chris@16 1743 policies::promote_double<false>,
Chris@16 1744 policies::discrete_quantile<>,
Chris@16 1745 policies::assert_undefined<> >::type forwarding_policy;
Chris@16 1746
Chris@16 1747 igamma_initializer<value_type, forwarding_policy>::force_instantiate();
Chris@16 1748
Chris@16 1749 return policies::checked_narrowing_cast<result_type, forwarding_policy>(
Chris@16 1750 detail::gamma_incomplete_imp(static_cast<value_type>(a),
Chris@16 1751 static_cast<value_type>(z), false, true,
Chris@16 1752 forwarding_policy(), static_cast<value_type*>(0)), "boost::math::tgamma<%1%>(%1%, %1%)");
Chris@16 1753 }
Chris@16 1754
Chris@16 1755 template <class T1, class T2>
Chris@16 1756 inline typename tools::promote_args<T1, T2>::type
Chris@16 1757 tgamma(T1 a, T2 z, const mpl::false_ tag)
Chris@16 1758 {
Chris@16 1759 return tgamma(a, z, policies::policy<>(), tag);
Chris@16 1760 }
Chris@16 1761
Chris@16 1762
Chris@16 1763 } // namespace detail
Chris@16 1764
Chris@16 1765 template <class T>
Chris@16 1766 inline typename tools::promote_args<T>::type
Chris@16 1767 tgamma(T z)
Chris@16 1768 {
Chris@16 1769 return tgamma(z, policies::policy<>());
Chris@16 1770 }
Chris@16 1771
Chris@16 1772 template <class T, class Policy>
Chris@16 1773 inline typename tools::promote_args<T>::type
Chris@16 1774 lgamma(T z, int* sign, const Policy&)
Chris@16 1775 {
Chris@16 1776 BOOST_FPU_EXCEPTION_GUARD
Chris@16 1777 typedef typename tools::promote_args<T>::type result_type;
Chris@16 1778 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 1779 typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
Chris@16 1780 typedef typename policies::normalise<
Chris@16 1781 Policy,
Chris@16 1782 policies::promote_float<false>,
Chris@16 1783 policies::promote_double<false>,
Chris@16 1784 policies::discrete_quantile<>,
Chris@16 1785 policies::assert_undefined<> >::type forwarding_policy;
Chris@16 1786
Chris@16 1787 detail::lgamma_initializer<value_type, forwarding_policy>::force_instantiate();
Chris@16 1788
Chris@16 1789 return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::lgamma_imp(static_cast<value_type>(z), forwarding_policy(), evaluation_type(), sign), "boost::math::lgamma<%1%>(%1%)");
Chris@16 1790 }
Chris@16 1791
Chris@16 1792 template <class T>
Chris@16 1793 inline typename tools::promote_args<T>::type
Chris@16 1794 lgamma(T z, int* sign)
Chris@16 1795 {
Chris@16 1796 return lgamma(z, sign, policies::policy<>());
Chris@16 1797 }
Chris@16 1798
Chris@16 1799 template <class T, class Policy>
Chris@16 1800 inline typename tools::promote_args<T>::type
Chris@16 1801 lgamma(T x, const Policy& pol)
Chris@16 1802 {
Chris@16 1803 return ::boost::math::lgamma(x, 0, pol);
Chris@16 1804 }
Chris@16 1805
Chris@16 1806 template <class T>
Chris@16 1807 inline typename tools::promote_args<T>::type
Chris@16 1808 lgamma(T x)
Chris@16 1809 {
Chris@16 1810 return ::boost::math::lgamma(x, 0, policies::policy<>());
Chris@16 1811 }
Chris@16 1812
Chris@16 1813 template <class T, class Policy>
Chris@16 1814 inline typename tools::promote_args<T>::type
Chris@16 1815 tgamma1pm1(T z, const Policy& /* pol */)
Chris@16 1816 {
Chris@16 1817 BOOST_FPU_EXCEPTION_GUARD
Chris@16 1818 typedef typename tools::promote_args<T>::type result_type;
Chris@16 1819 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 1820 typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
Chris@16 1821 typedef typename policies::normalise<
Chris@16 1822 Policy,
Chris@16 1823 policies::promote_float<false>,
Chris@16 1824 policies::promote_double<false>,
Chris@16 1825 policies::discrete_quantile<>,
Chris@16 1826 policies::assert_undefined<> >::type forwarding_policy;
Chris@16 1827
Chris@16 1828 return policies::checked_narrowing_cast<typename remove_cv<result_type>::type, forwarding_policy>(detail::tgammap1m1_imp(static_cast<value_type>(z), forwarding_policy(), evaluation_type()), "boost::math::tgamma1pm1<%!%>(%1%)");
Chris@16 1829 }
Chris@16 1830
Chris@16 1831 template <class T>
Chris@16 1832 inline typename tools::promote_args<T>::type
Chris@16 1833 tgamma1pm1(T z)
Chris@16 1834 {
Chris@16 1835 return tgamma1pm1(z, policies::policy<>());
Chris@16 1836 }
Chris@16 1837
Chris@16 1838 //
Chris@16 1839 // Full upper incomplete gamma:
Chris@16 1840 //
Chris@16 1841 template <class T1, class T2>
Chris@16 1842 inline typename tools::promote_args<T1, T2>::type
Chris@16 1843 tgamma(T1 a, T2 z)
Chris@16 1844 {
Chris@16 1845 //
Chris@16 1846 // Type T2 could be a policy object, or a value, select the
Chris@16 1847 // right overload based on T2:
Chris@16 1848 //
Chris@16 1849 typedef typename policies::is_policy<T2>::type maybe_policy;
Chris@16 1850 return detail::tgamma(a, z, maybe_policy());
Chris@16 1851 }
Chris@16 1852 template <class T1, class T2, class Policy>
Chris@16 1853 inline typename tools::promote_args<T1, T2>::type
Chris@16 1854 tgamma(T1 a, T2 z, const Policy& pol)
Chris@16 1855 {
Chris@16 1856 return detail::tgamma(a, z, pol, mpl::false_());
Chris@16 1857 }
Chris@16 1858 //
Chris@16 1859 // Full lower incomplete gamma:
Chris@16 1860 //
Chris@16 1861 template <class T1, class T2, class Policy>
Chris@16 1862 inline typename tools::promote_args<T1, T2>::type
Chris@16 1863 tgamma_lower(T1 a, T2 z, const Policy&)
Chris@16 1864 {
Chris@16 1865 BOOST_FPU_EXCEPTION_GUARD
Chris@16 1866 typedef typename tools::promote_args<T1, T2>::type result_type;
Chris@16 1867 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 1868 // typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
Chris@16 1869 typedef typename policies::normalise<
Chris@16 1870 Policy,
Chris@16 1871 policies::promote_float<false>,
Chris@16 1872 policies::promote_double<false>,
Chris@16 1873 policies::discrete_quantile<>,
Chris@16 1874 policies::assert_undefined<> >::type forwarding_policy;
Chris@16 1875
Chris@16 1876 detail::igamma_initializer<value_type, forwarding_policy>::force_instantiate();
Chris@16 1877
Chris@16 1878 return policies::checked_narrowing_cast<result_type, forwarding_policy>(
Chris@16 1879 detail::gamma_incomplete_imp(static_cast<value_type>(a),
Chris@16 1880 static_cast<value_type>(z), false, false,
Chris@16 1881 forwarding_policy(), static_cast<value_type*>(0)), "tgamma_lower<%1%>(%1%, %1%)");
Chris@16 1882 }
Chris@16 1883 template <class T1, class T2>
Chris@16 1884 inline typename tools::promote_args<T1, T2>::type
Chris@16 1885 tgamma_lower(T1 a, T2 z)
Chris@16 1886 {
Chris@16 1887 return tgamma_lower(a, z, policies::policy<>());
Chris@16 1888 }
Chris@16 1889 //
Chris@16 1890 // Regularised upper incomplete gamma:
Chris@16 1891 //
Chris@16 1892 template <class T1, class T2, class Policy>
Chris@16 1893 inline typename tools::promote_args<T1, T2>::type
Chris@16 1894 gamma_q(T1 a, T2 z, const Policy& /* pol */)
Chris@16 1895 {
Chris@16 1896 BOOST_FPU_EXCEPTION_GUARD
Chris@16 1897 typedef typename tools::promote_args<T1, T2>::type result_type;
Chris@16 1898 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 1899 // typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
Chris@16 1900 typedef typename policies::normalise<
Chris@16 1901 Policy,
Chris@16 1902 policies::promote_float<false>,
Chris@16 1903 policies::promote_double<false>,
Chris@16 1904 policies::discrete_quantile<>,
Chris@16 1905 policies::assert_undefined<> >::type forwarding_policy;
Chris@16 1906
Chris@16 1907 detail::igamma_initializer<value_type, forwarding_policy>::force_instantiate();
Chris@16 1908
Chris@16 1909 return policies::checked_narrowing_cast<result_type, forwarding_policy>(
Chris@16 1910 detail::gamma_incomplete_imp(static_cast<value_type>(a),
Chris@16 1911 static_cast<value_type>(z), true, true,
Chris@16 1912 forwarding_policy(), static_cast<value_type*>(0)), "gamma_q<%1%>(%1%, %1%)");
Chris@16 1913 }
Chris@16 1914 template <class T1, class T2>
Chris@16 1915 inline typename tools::promote_args<T1, T2>::type
Chris@16 1916 gamma_q(T1 a, T2 z)
Chris@16 1917 {
Chris@16 1918 return gamma_q(a, z, policies::policy<>());
Chris@16 1919 }
Chris@16 1920 //
Chris@16 1921 // Regularised lower incomplete gamma:
Chris@16 1922 //
Chris@16 1923 template <class T1, class T2, class Policy>
Chris@16 1924 inline typename tools::promote_args<T1, T2>::type
Chris@16 1925 gamma_p(T1 a, T2 z, const Policy&)
Chris@16 1926 {
Chris@16 1927 BOOST_FPU_EXCEPTION_GUARD
Chris@16 1928 typedef typename tools::promote_args<T1, T2>::type result_type;
Chris@16 1929 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 1930 // typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
Chris@16 1931 typedef typename policies::normalise<
Chris@16 1932 Policy,
Chris@16 1933 policies::promote_float<false>,
Chris@16 1934 policies::promote_double<false>,
Chris@16 1935 policies::discrete_quantile<>,
Chris@16 1936 policies::assert_undefined<> >::type forwarding_policy;
Chris@16 1937
Chris@16 1938 detail::igamma_initializer<value_type, forwarding_policy>::force_instantiate();
Chris@16 1939
Chris@16 1940 return policies::checked_narrowing_cast<result_type, forwarding_policy>(
Chris@16 1941 detail::gamma_incomplete_imp(static_cast<value_type>(a),
Chris@16 1942 static_cast<value_type>(z), true, false,
Chris@16 1943 forwarding_policy(), static_cast<value_type*>(0)), "gamma_p<%1%>(%1%, %1%)");
Chris@16 1944 }
Chris@16 1945 template <class T1, class T2>
Chris@16 1946 inline typename tools::promote_args<T1, T2>::type
Chris@16 1947 gamma_p(T1 a, T2 z)
Chris@16 1948 {
Chris@16 1949 return gamma_p(a, z, policies::policy<>());
Chris@16 1950 }
Chris@16 1951
Chris@16 1952 // ratios of gamma functions:
Chris@16 1953 template <class T1, class T2, class Policy>
Chris@16 1954 inline typename tools::promote_args<T1, T2>::type
Chris@16 1955 tgamma_delta_ratio(T1 z, T2 delta, const Policy& /* pol */)
Chris@16 1956 {
Chris@16 1957 BOOST_FPU_EXCEPTION_GUARD
Chris@16 1958 typedef typename tools::promote_args<T1, T2>::type result_type;
Chris@16 1959 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 1960 typedef typename policies::normalise<
Chris@16 1961 Policy,
Chris@16 1962 policies::promote_float<false>,
Chris@16 1963 policies::promote_double<false>,
Chris@16 1964 policies::discrete_quantile<>,
Chris@16 1965 policies::assert_undefined<> >::type forwarding_policy;
Chris@16 1966
Chris@16 1967 return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::tgamma_delta_ratio_imp(static_cast<value_type>(z), static_cast<value_type>(delta), forwarding_policy()), "boost::math::tgamma_delta_ratio<%1%>(%1%, %1%)");
Chris@16 1968 }
Chris@16 1969 template <class T1, class T2>
Chris@16 1970 inline typename tools::promote_args<T1, T2>::type
Chris@16 1971 tgamma_delta_ratio(T1 z, T2 delta)
Chris@16 1972 {
Chris@16 1973 return tgamma_delta_ratio(z, delta, policies::policy<>());
Chris@16 1974 }
Chris@16 1975 template <class T1, class T2, class Policy>
Chris@16 1976 inline typename tools::promote_args<T1, T2>::type
Chris@16 1977 tgamma_ratio(T1 a, T2 b, const Policy&)
Chris@16 1978 {
Chris@16 1979 typedef typename tools::promote_args<T1, T2>::type result_type;
Chris@16 1980 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 1981 typedef typename policies::normalise<
Chris@16 1982 Policy,
Chris@16 1983 policies::promote_float<false>,
Chris@16 1984 policies::promote_double<false>,
Chris@16 1985 policies::discrete_quantile<>,
Chris@16 1986 policies::assert_undefined<> >::type forwarding_policy;
Chris@16 1987
Chris@16 1988 return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::tgamma_ratio_imp(static_cast<value_type>(a), static_cast<value_type>(b), forwarding_policy()), "boost::math::tgamma_delta_ratio<%1%>(%1%, %1%)");
Chris@16 1989 }
Chris@16 1990 template <class T1, class T2>
Chris@16 1991 inline typename tools::promote_args<T1, T2>::type
Chris@16 1992 tgamma_ratio(T1 a, T2 b)
Chris@16 1993 {
Chris@16 1994 return tgamma_ratio(a, b, policies::policy<>());
Chris@16 1995 }
Chris@16 1996
Chris@16 1997 template <class T1, class T2, class Policy>
Chris@16 1998 inline typename tools::promote_args<T1, T2>::type
Chris@16 1999 gamma_p_derivative(T1 a, T2 x, const Policy&)
Chris@16 2000 {
Chris@16 2001 BOOST_FPU_EXCEPTION_GUARD
Chris@16 2002 typedef typename tools::promote_args<T1, T2>::type result_type;
Chris@16 2003 typedef typename policies::evaluation<result_type, Policy>::type value_type;
Chris@16 2004 typedef typename policies::normalise<
Chris@16 2005 Policy,
Chris@16 2006 policies::promote_float<false>,
Chris@16 2007 policies::promote_double<false>,
Chris@16 2008 policies::discrete_quantile<>,
Chris@16 2009 policies::assert_undefined<> >::type forwarding_policy;
Chris@16 2010
Chris@16 2011 return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::gamma_p_derivative_imp(static_cast<value_type>(a), static_cast<value_type>(x), forwarding_policy()), "boost::math::gamma_p_derivative<%1%>(%1%, %1%)");
Chris@16 2012 }
Chris@16 2013 template <class T1, class T2>
Chris@16 2014 inline typename tools::promote_args<T1, T2>::type
Chris@16 2015 gamma_p_derivative(T1 a, T2 x)
Chris@16 2016 {
Chris@16 2017 return gamma_p_derivative(a, x, policies::policy<>());
Chris@16 2018 }
Chris@16 2019
Chris@16 2020 } // namespace math
Chris@16 2021 } // namespace boost
Chris@16 2022
Chris@16 2023 #ifdef BOOST_MSVC
Chris@16 2024 # pragma warning(pop)
Chris@16 2025 #endif
Chris@16 2026
Chris@16 2027 #include <boost/math/special_functions/detail/igamma_inverse.hpp>
Chris@16 2028 #include <boost/math/special_functions/detail/gamma_inva.hpp>
Chris@16 2029 #include <boost/math/special_functions/erf.hpp>
Chris@16 2030
Chris@16 2031 #endif // BOOST_MATH_SF_GAMMA_HPP
Chris@16 2032
Chris@16 2033
Chris@16 2034
Chris@16 2035