Boost logo

Boost-Commit :

From: john_at_[hidden]
Date: 2008-05-29 08:38:04


Author: johnmaddock
Date: 2008-05-29 08:38:04 EDT (Thu, 29 May 2008)
New Revision: 45898
URL: http://svn.boost.org/trac/boost/changeset/45898

Log:
Added rational approximations for log1p.
Text files modified:
   sandbox/math_toolkit/boost/math/special_functions/log1p.hpp | 192 +++++++++++++++++++++++++++++++++++++++
   1 files changed, 188 insertions(+), 4 deletions(-)

Modified: sandbox/math_toolkit/boost/math/special_functions/log1p.hpp
==============================================================================
--- sandbox/math_toolkit/boost/math/special_functions/log1p.hpp (original)
+++ sandbox/math_toolkit/boost/math/special_functions/log1p.hpp 2008-05-29 08:38:04 EDT (Thu, 29 May 2008)
@@ -15,6 +15,7 @@
 #include <boost/limits.hpp>
 #include <boost/math/tools/config.hpp>
 #include <boost/math/tools/series.hpp>
+#include <boost/math/tools/rational.hpp>
 #include <boost/math/policies/error_handling.hpp>
 #include <boost/math/special_functions/math_fwd.hpp>
 
@@ -59,8 +60,6 @@
      log1p_series& operator=(const log1p_series&);
   };
 
-} // namespace detail
-
 // Algorithm log1p is part of C99, but is not yet provided by many compilers.
 //
 // This version uses a Taylor series expansion for 0.5 > x > epsilon, which may
@@ -71,9 +70,8 @@
 // it performs no better than log(1+x): which is to say not very well at all.
 //
 template <class T, class Policy>
-typename tools::promote_args<T>::type log1p(T x, const Policy& pol)
+T log1p_imp(T const & x, const Policy& pol, const mpl::int_<0>&)
 { // The function returns the natural logarithm of 1 + x.
- // A domain error occurs if x < -1. TODO should there be a check?
    typedef typename tools::promote_args<T>::type result_type;
    BOOST_MATH_STD_USING
    using std::abs;
@@ -106,6 +104,192 @@
    return result;
 }
 
+template <class T, class Policy>
+T log1p_imp(T const& x, const Policy& pol, const mpl::int_<53>&)
+{ // The function returns the natural logarithm of 1 + x.
+ BOOST_MATH_STD_USING
+
+ static const char* function = "boost::math::log1p<%1%>(%1%)";
+
+ if(x < -1)
+ return policies::raise_domain_error<T>(
+ function, "log1p(x) requires x > -1, but got x = %1%.", x, pol);
+ if(x == -1)
+ return -policies::raise_overflow_error<T>(
+ function, 0, pol);
+
+ T a = fabs(x);
+ if(a > 0.5f)
+ return log(1 + x);
+ // Note that without numeric_limits specialisation support,
+ // epsilon just returns zero, and our "optimisation" will always fail:
+ if(a < tools::epsilon<T>())
+ return x;
+
+ // Maximum Deviation Found: 1.846e-017
+ // Expected Error Term: 1.843e-017
+ // Maximum Relative Change in Control Points: 8.138e-004
+ // Max Error found at double precision = 3.250766e-016
+ static const T P[] = {
+ 0.15141069795941984e-16L,
+ 0.35495104378055055e-15L,
+ 0.33333333333332835L,
+ 0.99249063543365859L,
+ 1.1143969784156509L,
+ 0.58052937949269651L,
+ 0.13703234928513215L,
+ 0.011294864812099712L
+ };
+ static const T Q[] = {
+ 1L,
+ 3.7274719063011499L,
+ 5.5387948649720334L,
+ 4.159201143419005L,
+ 1.6423855110312755L,
+ 0.31706251443180914L,
+ 0.022665554431410243L,
+ -0.29252538135177773e-5L
+ };
+
+ T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x);
+ result *= x;
+
+ return result;
+}
+
+template <class T, class Policy>
+T log1p_imp(T const& x, const Policy& pol, const mpl::int_<64>&)
+{ // The function returns the natural logarithm of 1 + x.
+ BOOST_MATH_STD_USING
+
+ static const char* function = "boost::math::log1p<%1%>(%1%)";
+
+ if(x < -1)
+ return policies::raise_domain_error<T>(
+ function, "log1p(x) requires x > -1, but got x = %1%.", x, pol);
+ if(x == -1)
+ return -policies::raise_overflow_error<T>(
+ function, 0, pol);
+
+ T a = fabs(x);
+ if(a > 0.5f)
+ return log(1 + x);
+ // Note that without numeric_limits specialisation support,
+ // epsilon just returns zero, and our "optimisation" will always fail:
+ if(a < tools::epsilon<T>())
+ return x;
+
+ // Maximum Deviation Found: 8.089e-20
+ // Expected Error Term: 8.088e-20
+ // Maximum Relative Change in Control Points: 9.648e-05
+ // Max Error found at long double precision = 2.242324e-19
+ static const T P[] = {
+ -0.807533446680736736712e-19L,
+ -0.490881544804798926426e-18L,
+ 0.333333333333333373941L,
+ 1.17141290782087994162L,
+ 1.62790522814926264694L,
+ 1.13156411870766876113L,
+ 0.408087379932853785336L,
+ 0.0706537026422828914622L,
+ 0.00441709903782239229447L
+ };
+ static const T Q[] = {
+ 1L,
+ 4.26423872346263928361L,
+ 7.48189472704477708962L,
+ 6.94757016732904280913L,
+ 3.6493508622280767304L,
+ 1.06884863623790638317L,
+ 0.158292216998514145947L,
+ 0.00885295524069924328658L,
+ -0.560026216133415663808e-6L
+ };
+
+ T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x);
+ result *= x;
+
+ return result;
+}
+
+template <class T, class Policy>
+T log1p_imp(T const& x, const Policy& pol, const mpl::int_<24>&)
+{ // The function returns the natural logarithm of 1 + x.
+ BOOST_MATH_STD_USING
+
+ static const char* function = "boost::math::log1p<%1%>(%1%)";
+
+ if(x < -1)
+ return policies::raise_domain_error<T>(
+ function, "log1p(x) requires x > -1, but got x = %1%.", x, pol);
+ if(x == -1)
+ return -policies::raise_overflow_error<T>(
+ function, 0, pol);
+
+ T a = fabs(x);
+ if(a > 0.5f)
+ return log(1 + x);
+ // Note that without numeric_limits specialisation support,
+ // epsilon just returns zero, and our "optimisation" will always fail:
+ if(a < tools::epsilon<T>())
+ return x;
+
+ // Maximum Deviation Found: 6.910e-08
+ // Expected Error Term: 6.910e-08
+ // Maximum Relative Change in Control Points: 2.509e-04
+ // Max Error found at double precision = 6.910422e-08
+ // Max Error found at float precision = 8.357242e-08
+ static const T P[] = {
+ -0.671192866803148236519e-7L,
+ 0.119670999140731844725e-6L,
+ 0.333339469182083148598L,
+ 0.237827183019664122066L
+ };
+ static const T Q[] = {
+ 1L,
+ 1.46348272586988539733L,
+ 0.497859871350117338894L,
+ -0.00471666268910169651936L
+ };
+
+ T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x);
+ result *= x;
+
+ return result;
+}
+
+} // namespace detail
+
+template <class T, class Policy>
+inline typename tools::promote_args<T>::type log1p(T x, const Policy&)
+{
+ typedef typename tools::promote_args<T>::type result_type;
+ typedef typename policies::evaluation<result_type, Policy>::type value_type;
+ typedef typename policies::precision<result_type, Policy>::type precision_type;
+ typedef typename policies::normalise<
+ Policy,
+ policies::promote_float<false>,
+ policies::promote_double<false>,
+ policies::discrete_quantile<>,
+ policies::assert_undefined<> >::type forwarding_policy;
+
+ typedef typename mpl::if_<
+ mpl::less_equal<precision_type, mpl::int_<0> >,
+ mpl::int_<0>,
+ typename mpl::if_<
+ mpl::less_equal<precision_type, mpl::int_<53> >,
+ mpl::int_<53>, // double
+ typename mpl::if_<
+ mpl::less_equal<precision_type, mpl::int_<64> >,
+ mpl::int_<64>, // 80-bit long double
+ mpl::int_<0> // too many bits, use generic version.
+ >::type
+ >::type
+ >::type tag_type;
+ return policies::checked_narrowing_cast<result_type, forwarding_policy>(
+ detail::log1p_imp(static_cast<value_type>(x), forwarding_policy(), tag_type()), "boost::math::log1p<%1%>(%1%)");
+}
+
 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
 // These overloads work around a type deduction bug:
 inline float log1p(float z)


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk