Boost-Geometry-Utils
view release on metacpan or search on metacpan
src/boost/math/special_functions/gamma.hpp view on Meta::CPAN
//
// Incomplete gamma functions follow:
//
template <class T>
struct upper_incomplete_gamma_fract
{
private:
T z, a;
int k;
public:
typedef std::pair<T,T> result_type;
upper_incomplete_gamma_fract(T a1, T z1)
: z(z1-a1+1), a(a1), k(0)
{
}
result_type operator()()
{
++k;
z += 2;
return result_type(k * (a - k), z);
}
};
template <class T>
inline T upper_gamma_fraction(T a, T z, T eps)
{
// Multiply result by z^a * e^-z to get the full
// upper incomplete integral. Divide by tgamma(z)
// to normalise.
upper_incomplete_gamma_fract<T> f(a, z);
return 1 / (z - a + 1 + boost::math::tools::continued_fraction_a(f, eps));
}
template <class T>
struct lower_incomplete_gamma_series
{
private:
T a, z, result;
public:
typedef T result_type;
lower_incomplete_gamma_series(T a1, T z1) : a(a1), z(z1), result(1){}
T operator()()
{
T r = result;
a += 1;
result *= z/a;
return r;
}
};
template <class T, class Policy>
inline T lower_gamma_series(T a, T z, const Policy& pol, T init_value = 0)
{
// Multiply result by ((z^a) * (e^-z) / a) to get the full
// lower incomplete integral. Then divide by tgamma(a)
// to get the normalised value.
lower_incomplete_gamma_series<T> s(a, z);
boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
T factor = policies::get_epsilon<T, Policy>();
T result = boost::math::tools::sum_series(s, factor, max_iter, init_value);
policies::check_series_iterations<T>("boost::math::detail::lower_gamma_series<%1%>(%1%)", max_iter, pol);
return result;
}
//
// Fully generic tgamma and lgamma use the incomplete partial
// sums added together:
//
template <class T, class Policy>
T gamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos& l)
{
static const char* function = "boost::math::tgamma<%1%>(%1%)";
BOOST_MATH_STD_USING
if((z <= 0) && (floor(z) == z))
return policies::raise_pole_error<T>(function, "Evaluation of tgamma at a negative integer %1%.", z, pol);
if(z <= -20)
{
T result = gamma_imp(T(-z), pol, l) * sinpx(z);
if((fabs(result) < 1) && (tools::max_value<T>() * fabs(result) < boost::math::constants::pi<T>()))
return policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
result = -boost::math::constants::pi<T>() / result;
if(result == 0)
return policies::raise_underflow_error<T>(function, "Result of tgamma is too small to represent.", pol);
if((boost::math::fpclassify)(result) == (int)FP_SUBNORMAL)
return policies::raise_denorm_error<T>(function, "Result of tgamma is denormalized.", result, pol);
return result;
}
//
// The upper gamma fraction is *very* slow for z < 6, actually it's very
// slow to converge everywhere but recursing until z > 6 gets rid of the
// worst of it's behaviour.
//
T prefix = 1;
while(z < 6)
{
prefix /= z;
z += 1;
}
BOOST_MATH_INSTRUMENT_CODE(prefix);
if((floor(z) == z) && (z < max_factorial<T>::value))
{
prefix *= unchecked_factorial<T>(itrunc(z, pol) - 1);
}
else
{
prefix = prefix * pow(z / boost::math::constants::e<T>(), z);
BOOST_MATH_INSTRUMENT_CODE(prefix);
T sum = detail::lower_gamma_series(z, z, pol) / z;
BOOST_MATH_INSTRUMENT_CODE(sum);
sum += detail::upper_gamma_fraction(z, z, ::boost::math::policies::get_epsilon<T, Policy>());
BOOST_MATH_INSTRUMENT_CODE(sum);
if(fabs(tools::max_value<T>() / prefix) < fabs(sum))
return policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
BOOST_MATH_INSTRUMENT_CODE((sum * prefix));
return sum * prefix;
}
return prefix;
}
template <class T, class Policy>
T lgamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos& l, int*sign)
src/boost/math/special_functions/gamma.hpp view on Meta::CPAN
BOOST_MATH_STD_USING
T limit = (std::max)(T(10), a);
T sum = detail::lower_gamma_series(a, limit, pol) / a;
sum += detail::upper_gamma_fraction(a, limit, ::boost::math::policies::get_epsilon<T, Policy>());
if(a < 10)
{
// special case for small a:
T prefix = pow(z / 10, a);
prefix *= exp(10-z);
if(0 == prefix)
{
prefix = pow((z * exp((10-z)/a)) / 10, a);
}
prefix /= sum;
return prefix;
}
T zoa = z / a;
T amz = a - z;
T alzoa = a * log(zoa);
T prefix;
if(((std::min)(alzoa, amz) <= tools::log_min_value<T>()) || ((std::max)(alzoa, amz) >= tools::log_max_value<T>()))
{
T amza = amz / a;
if((amza <= tools::log_min_value<T>()) || (amza >= tools::log_max_value<T>()))
{
prefix = exp(alzoa + amz);
}
else
{
prefix = pow(zoa * exp(amza), a);
}
}
else
{
prefix = pow(zoa, a) * exp(amz);
}
prefix /= sum;
return prefix;
}
//
// Upper gamma fraction for very small a:
//
template <class T, class Policy>
inline T tgamma_small_upper_part(T a, T x, const Policy& pol, T* pgam = 0, bool invert = false, T* pderivative = 0)
{
BOOST_MATH_STD_USING // ADL of std functions.
//
// Compute the full upper fraction (Q) when a is very small:
//
T result;
result = boost::math::tgamma1pm1(a, pol);
if(pgam)
*pgam = (result + 1) / a;
T p = boost::math::powm1(x, a, pol);
result -= p;
result /= a;
detail::small_gamma2_series<T> s(a, x);
boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>() - 10;
p += 1;
if(pderivative)
*pderivative = p / (*pgam * exp(x));
T init_value = invert ? *pgam : 0;
result = -p * tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, (init_value - result) / p);
policies::check_series_iterations<T>("boost::math::tgamma_small_upper_part<%1%>(%1%, %1%)", max_iter, pol);
if(invert)
result = -result;
return result;
}
//
// Upper gamma fraction for integer a:
//
template <class T, class Policy>
inline T finite_gamma_q(T a, T x, Policy const& pol, T* pderivative = 0)
{
//
// Calculates normalised Q when a is an integer:
//
BOOST_MATH_STD_USING
T e = exp(-x);
T sum = e;
if(sum != 0)
{
T term = sum;
for(unsigned n = 1; n < a; ++n)
{
term /= n;
term *= x;
sum += term;
}
}
if(pderivative)
{
*pderivative = e * pow(x, a) / boost::math::unchecked_factorial<T>(itrunc(T(a - 1), pol));
}
return sum;
}
//
// Upper gamma fraction for half integer a:
//
template <class T, class Policy>
T finite_half_gamma_q(T a, T x, T* p_derivative, const Policy& pol)
{
//
// Calculates normalised Q when a is a half-integer:
//
BOOST_MATH_STD_USING
T e = boost::math::erfc(sqrt(x), pol);
if((e != 0) && (a > 1))
{
T term = exp(-x) / sqrt(constants::pi<T>() * x);
term *= x;
static const T half = T(1) / 2;
term /= half;
T sum = term;
for(unsigned n = 2; n < a; ++n)
{
term /= n - half;
term *= x;
sum += term;
}
e += sum;
if(p_derivative)
{
*p_derivative = 0;
( run in 0.465 second using v1.01-cache-2.11-cpan-96521ef73a4 )