2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-26 06:42:12 +00:00

Replace uses of log(tgamma(x)) with lgamma(x)

This commit is contained in:
Matt Borland
2022-04-18 09:22:31 -07:00
parent 73708be874
commit 4dcddfb801
3 changed files with 7 additions and 15 deletions

View File

@@ -152,7 +152,7 @@ template <class RealType, class Policy>
inline RealType logpdf(const gamma_distribution<RealType, Policy>& dist, const RealType& x)
{
BOOST_MATH_STD_USING // for ADL of std functions
using boost::math::tgamma;
using boost::math::lgamma;
static const char* function = "boost::math::logpdf(const gamma_distribution<%1%>&, %1%)";
@@ -169,16 +169,8 @@ inline RealType logpdf(const gamma_distribution<RealType, Policy>& dist, const R
{
return std::numeric_limits<RealType>::quiet_NaN();
}
// The following calculation does not always work with float so take the naive road out
BOOST_IF_CONSTEXPR(std::is_same<RealType, float>::value)
{
result = log(pdf(dist, x));
}
else
{
result = -k*log(theta) + (k-1)*log(x) - log(tgamma(k)) - (x/theta);
}
result = -k*log(theta) + (k-1)*log(x) - lgamma(k) - (x/theta);
return result;
} // logpdf

View File

@@ -198,7 +198,7 @@ template <class RealType, class Policy>
inline RealType logpdf(const inverse_gamma_distribution<RealType, Policy>& dist, const RealType& x)
{
BOOST_MATH_STD_USING // for ADL of std functions
using boost::math::tgamma;
using boost::math::lgamma;
static const char* function = "boost::math::logpdf(const inverse_gamma_distribution<%1%>&, %1%)";
@@ -228,7 +228,7 @@ inline RealType logpdf(const inverse_gamma_distribution<RealType, Policy>& dist,
return policies::raise_overflow_error<RealType, Policy>(function, "PDF is infinite.", Policy());
}
return shape * log(scale) + (-shape-1)*log(x) - log(tgamma(shape)) - (scale/x);
return shape * log(scale) + (-shape-1)*log(x) - lgamma(shape) - (scale/x);
} // pdf
template <class RealType, class Policy>

View File

@@ -279,7 +279,7 @@ namespace boost
BOOST_FPU_EXCEPTION_GUARD
BOOST_MATH_STD_USING // for ADL of std functions.
using boost::math::tgamma;
using boost::math::lgamma;
RealType mean = dist.mean();
// Error check:
@@ -302,7 +302,7 @@ namespace boost
// Special case where k and lambda are both positive
if(k > 0 && mean > 0)
{
return -log(tgamma(k+1)) + k*log(mean) - mean;
return -lgamma(k+1) + k*log(mean) - mean;
}
result = log(pdf(dist, k));