-
Notifications
You must be signed in to change notification settings - Fork 263
Adding logarithm of incomplete gamma function #1338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
da8b635
0a2b01b
c80492f
dfb598f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,14 +7,15 @@ | |
| #include <limits> | ||
| #include <boost/math/special_functions/fpclassify.hpp> | ||
| #include <boost/math/constants/constants.hpp> | ||
| #include <complex> | ||
|
|
||
| namespace boost { namespace math { | ||
|
|
||
| // Calculates log(exp(x1) + exp(x2)) | ||
| template <typename Real> | ||
| Real logaddexp(Real x1, Real x2) noexcept | ||
| { | ||
| using std::log1p; | ||
| using std::log; | ||
| using std::exp; | ||
| using std::abs; | ||
|
|
||
|
|
@@ -30,12 +31,12 @@ Real logaddexp(Real x1, Real x2) noexcept | |
|
|
||
| const Real temp = x1 - x2; | ||
|
|
||
| if (temp > 0) | ||
| if (std::real(temp) > 0) | ||
| { | ||
| return x1 + log1p(exp(-temp)); | ||
| return x1 + log(1.0 + exp(-temp)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a really bad change that will kill precision when temp is small.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've reverted this for the |
||
| } | ||
|
|
||
| return x2 + log1p(exp(temp)); | ||
| return x2 + log(1.0 + exp(temp)); | ||
| } | ||
|
|
||
| }} // Namespace boost::math | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ | |
| #include <boost/math/tools/precision.hpp> | ||
| #include <boost/math/tools/complex.hpp> | ||
| #include <boost/math/tools/cstdint.hpp> | ||
| #include <boost/math/special_functions/logaddexp.hpp> | ||
| #include <limits> | ||
|
|
||
| namespace boost{ namespace math{ namespace tools{ | ||
|
|
||
|
|
@@ -110,7 +112,7 @@ namespace detail { | |
| // | ||
|
|
||
| template <typename Gen, typename U> | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b_impl(Gen& g, const U& factor, boost::math::uintmax_t& max_terms) | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b_impl(Gen& g, const U& factor, boost::math::uintmax_t& max_terms, bool logArithmetic) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the log and non-log versions basically have completely different code, I see this as two different functions?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've separated this into a new function in |
||
| noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) | ||
| #ifndef BOOST_MATH_HAS_GPU_SUPPORT | ||
| // SYCL can not handle this condition so we only check float on that platform | ||
|
|
@@ -140,19 +142,40 @@ BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type | |
| C = f; | ||
| D = 0; | ||
|
|
||
| if (logArithmetic){ | ||
| f = log(f); | ||
| C = log(C); | ||
| D = log(tiny); | ||
| } | ||
|
|
||
| boost::math::uintmax_t counter(max_terms); | ||
| do{ | ||
| v = g(); | ||
| D = traits::b(v) + traits::a(v) * D; | ||
| if(D == result_type(0)) | ||
| D = tiny; | ||
| C = traits::b(v) + traits::a(v) / C; | ||
| if(C == zero) | ||
| C = tiny; | ||
| D = one/D; | ||
| delta = C*D; | ||
| f = f * delta; | ||
| }while((abs(delta - one) > terminator) && --counter); | ||
| if(!logArithmetic){ | ||
| do{ | ||
| v = g(); | ||
| D = traits::b(v) + traits::a(v) * D; | ||
| if(D == result_type(0)) | ||
| D = tiny; | ||
| C = traits::b(v) + traits::a(v) / C; | ||
| if(C == zero) | ||
| C = tiny; | ||
| D = one/D; | ||
| delta = C*D; | ||
| f = f * delta; | ||
| }while((abs(delta - one) > terminator) && --counter); | ||
| } | ||
| else{ | ||
| do{ | ||
| v = g(); | ||
| D = -logaddexp(log(traits::b(v)), log(traits::a(v)) + D); | ||
| if(D == -std::numeric_limits<result_type>::infinity()) | ||
| D = log(tiny); | ||
| C = logaddexp(log(traits::b(v)), log(traits::a(v)) - C); | ||
| if(C == -std::numeric_limits<result_type>::infinity()) | ||
| C = log(tiny); | ||
| delta = C + D; | ||
| f = f + delta; | ||
| }while((abs(delta) > terminator) && --counter); | ||
| } | ||
|
|
||
| max_terms = max_terms - counter; | ||
|
|
||
|
|
@@ -162,30 +185,30 @@ BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type | |
| } // namespace detail | ||
|
|
||
| template <typename Gen, typename U> | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, const U& factor, boost::math::uintmax_t& max_terms) | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, const U& factor, boost::math::uintmax_t& max_terms, bool logArithmetic=false) | ||
| noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) | ||
| #ifndef BOOST_MATH_HAS_GPU_SUPPORT | ||
| && noexcept(std::declval<Gen>()()) | ||
| #endif | ||
| ) | ||
| { | ||
| return detail::continued_fraction_b_impl(g, factor, max_terms); | ||
| return detail::continued_fraction_b_impl(g, factor, max_terms, logArithmetic); | ||
| } | ||
|
|
||
| template <typename Gen, typename U> | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, const U& factor) | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, const U& factor, bool logArithmetic=false) | ||
| noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) | ||
| #ifndef BOOST_MATH_HAS_GPU_SUPPORT | ||
| && noexcept(std::declval<Gen>()()) | ||
| #endif | ||
| ) | ||
| { | ||
| boost::math::uintmax_t max_terms = (boost::math::numeric_limits<boost::math::uintmax_t>::max)(); | ||
| return detail::continued_fraction_b_impl(g, factor, max_terms); | ||
| return detail::continued_fraction_b_impl(g, factor, max_terms, logArithmetic); | ||
| } | ||
|
|
||
| template <typename Gen> | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, int bits) | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, int bits, bool logArithmetic=false) | ||
| noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) | ||
| #ifndef BOOST_MATH_HAS_GPU_SUPPORT | ||
| && noexcept(std::declval<Gen>()()) | ||
|
|
@@ -199,11 +222,11 @@ BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type | |
|
|
||
| result_type factor = ldexp(1.0f, 1 - bits); // 1 / pow(result_type(2), bits); | ||
| boost::math::uintmax_t max_terms = (boost::math::numeric_limits<boost::math::uintmax_t>::max)(); | ||
| return detail::continued_fraction_b_impl(g, factor, max_terms); | ||
| return detail::continued_fraction_b_impl(g, factor, max_terms, logArithmetic); | ||
| } | ||
|
|
||
| template <typename Gen> | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, int bits, boost::math::uintmax_t& max_terms) | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_b(Gen& g, int bits, boost::math::uintmax_t& max_terms, bool logArithmetic=false) | ||
| noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) | ||
| #ifndef BOOST_MATH_HAS_GPU_SUPPORT | ||
| && noexcept(std::declval<Gen>()()) | ||
|
|
@@ -216,7 +239,7 @@ BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type | |
| using result_type = typename traits::result_type; | ||
|
|
||
| result_type factor = ldexp(1.0f, 1 - bits); // 1 / pow(result_type(2), bits); | ||
| return detail::continued_fraction_b_impl(g, factor, max_terms); | ||
| return detail::continued_fraction_b_impl(g, factor, max_terms, logArithmetic); | ||
| } | ||
|
|
||
| namespace detail { | ||
|
|
@@ -236,7 +259,7 @@ namespace detail { | |
| // Note that the first a1 and b1 returned by generator Gen are both used. | ||
| // | ||
| template <typename Gen, typename U> | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a_impl(Gen& g, const U& factor, boost::math::uintmax_t& max_terms) | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a_impl(Gen& g, const U& factor, boost::math::uintmax_t& max_terms, bool logArithmetic=false) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again this looks like two different functions to me. |
||
| noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) | ||
| #ifndef BOOST_MATH_HAS_GPU_SUPPORT | ||
| && noexcept(std::declval<Gen>()()) | ||
|
|
@@ -266,53 +289,78 @@ BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type | |
| C = f; | ||
| D = 0; | ||
|
|
||
| if (logArithmetic){ | ||
| f = log(f); | ||
| C = log(C); | ||
| D = log(tiny); | ||
| } | ||
|
|
||
| boost::math::uintmax_t counter(max_terms); | ||
|
|
||
| do{ | ||
| v = g(); | ||
| D = traits::b(v) + traits::a(v) * D; | ||
| if(D == zero) | ||
| D = tiny; | ||
| C = traits::b(v) + traits::a(v) / C; | ||
| if(C == zero) | ||
| C = tiny; | ||
| D = one/D; | ||
| delta = C*D; | ||
| f = f * delta; | ||
| }while((abs(delta - one) > terminator) && --counter); | ||
| if (!logArithmetic){ | ||
| do{ | ||
| v = g(); | ||
| D = traits::b(v) + traits::a(v) * D; | ||
| if(D == zero) | ||
| D = tiny; | ||
| C = traits::b(v) + traits::a(v) / C; | ||
| if(C == zero) | ||
| C = tiny; | ||
| D = one/D; | ||
| delta = C*D; | ||
| f = f * delta; | ||
| }while((abs(delta - one) > terminator) && --counter); | ||
| } | ||
| else{ | ||
| do{ | ||
| v = g(); | ||
| D = -logaddexp(log(traits::b(v)), log(traits::a(v)) + D); | ||
| if(D == -std::numeric_limits<result_type>::infinity()) | ||
| D = log(tiny); | ||
| C = logaddexp(log(traits::b(v)), log(traits::a(v)) - C); | ||
| if(C == -std::numeric_limits<result_type>::infinity()) | ||
| C = log(tiny); | ||
| delta = C + D; | ||
| f = f + delta; | ||
| }while((abs(delta) > terminator) && --counter); | ||
| } | ||
|
|
||
| max_terms = max_terms - counter; | ||
|
|
||
| return a0/f; | ||
| if (!logArithmetic){ | ||
| return a0/f; | ||
| } | ||
| else{ | ||
| return log(a0) - f; | ||
| } | ||
| } | ||
|
|
||
| } // namespace detail | ||
|
|
||
| template <typename Gen, typename U> | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, const U& factor, boost::math::uintmax_t& max_terms) | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, const U& factor, boost::math::uintmax_t& max_terms, bool logArithmetic=false) | ||
| noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) | ||
| #ifndef BOOST_MATH_HAS_GPU_SUPPORT | ||
| && noexcept(std::declval<Gen>()()) | ||
| #endif | ||
| ) | ||
| { | ||
| return detail::continued_fraction_a_impl(g, factor, max_terms); | ||
| return detail::continued_fraction_a_impl(g, factor, max_terms, logArithmetic); | ||
| } | ||
|
|
||
| template <typename Gen, typename U> | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, const U& factor) | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, const U& factor, bool logArithmetic=false) | ||
| noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) | ||
| #ifndef BOOST_MATH_HAS_GPU_SUPPORT | ||
| && noexcept(std::declval<Gen>()()) | ||
| #endif | ||
| ) | ||
| { | ||
| boost::math::uintmax_t max_iter = (boost::math::numeric_limits<boost::math::uintmax_t>::max)(); | ||
| return detail::continued_fraction_a_impl(g, factor, max_iter); | ||
| return detail::continued_fraction_a_impl(g, factor, max_iter, logArithmetic); | ||
| } | ||
|
|
||
| template <typename Gen> | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, int bits) | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, int bits, bool logArithmetic=false) | ||
| noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) | ||
| #ifndef BOOST_MATH_HAS_GPU_SUPPORT | ||
| && noexcept(std::declval<Gen>()()) | ||
|
|
@@ -327,11 +375,11 @@ BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type | |
| result_type factor = ldexp(1.0f, 1-bits); // 1 / pow(result_type(2), bits); | ||
| boost::math::uintmax_t max_iter = (boost::math::numeric_limits<boost::math::uintmax_t>::max)(); | ||
|
|
||
| return detail::continued_fraction_a_impl(g, factor, max_iter); | ||
| return detail::continued_fraction_a_impl(g, factor, max_iter, logArithmetic); | ||
| } | ||
|
|
||
| template <typename Gen> | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, int bits, boost::math::uintmax_t& max_terms) | ||
| BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type continued_fraction_a(Gen& g, int bits, boost::math::uintmax_t& max_terms, bool logArithmetic=false) | ||
| noexcept(BOOST_MATH_IS_FLOAT(typename detail::fraction_traits<Gen>::result_type) | ||
| #ifndef BOOST_MATH_HAS_GPU_SUPPORT | ||
| && noexcept(std::declval<Gen>()()) | ||
|
|
@@ -344,7 +392,7 @@ BOOST_MATH_GPU_ENABLED inline typename detail::fraction_traits<Gen>::result_type | |
| using result_type = typename traits::result_type; | ||
|
|
||
| result_type factor = ldexp(1.0f, 1-bits); // 1 / pow(result_type(2), bits); | ||
| return detail::continued_fraction_a_impl(g, factor, max_terms); | ||
| return detail::continued_fraction_a_impl(g, factor, max_terms, logArithmetic); | ||
| } | ||
|
|
||
| } // namespace tools | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trick here, is to add
using std::realabove, and then callreal(temp), that way ADL can findboost::multiprecision::realin the multiprecision case (likewise for any other type that has their own namespace and is forbidden from putting stuff instd::).