| 1 |
// (C) Copyright John Maddock 2005-2006. |
| 2 |
// Use, modification and distribution are subject to the |
| 3 |
// Boost Software License, Version 1.0. (See accompanying file |
| 4 |
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 |
|
| 6 |
#ifndef BOOST_MATH_LOG1P_INCLUDED |
| 7 |
#define BOOST_MATH_LOG1P_INCLUDED |
| 8 |
|
| 9 |
#ifdef _MSC_VER |
| 10 |
#pragma once |
| 11 |
#pragma warning(push) |
| 12 |
#pragma warning(disable:4702) // Unreachable code (release mode only warning) |
| 13 |
#endif |
| 14 |
|
| 15 |
#if defined __has_include |
| 16 |
# if ((__cplusplus > 202002L) || (defined(_MSVC_LANG) && (_MSVC_LANG > 202002L))) |
| 17 |
# if __has_include (<stdfloat>) |
| 18 |
# include <stdfloat> |
| 19 |
# endif |
| 20 |
# endif |
| 21 |
#endif |
| 22 |
|
| 23 |
#include <boost/math/tools/config.hpp> |
| 24 |
#include <boost/math/tools/series.hpp> |
| 25 |
#include <boost/math/tools/rational.hpp> |
| 26 |
#include <boost/math/tools/big_constant.hpp> |
| 27 |
#include <boost/math/tools/numeric_limits.hpp> |
| 28 |
#include <boost/math/tools/cstdint.hpp> |
| 29 |
#include <boost/math/tools/promotion.hpp> |
| 30 |
#include <boost/math/tools/precision.hpp> |
| 31 |
#include <boost/math/policies/error_handling.hpp> |
| 32 |
#include <boost/math/special_functions/math_fwd.hpp> |
| 33 |
#include <boost/math/tools/assert.hpp> |
| 34 |
#include <boost/math/special_functions/fpclassify.hpp> |
| 35 |
|
| 36 |
#if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128) |
| 37 |
// |
| 38 |
// This is the only way we can avoid |
| 39 |
// warning: non-standard suffix on floating constant [-Wpedantic] |
| 40 |
// when building with -Wall -pedantic. Neither __extension__ |
| 41 |
// nor #pragma diagnostic ignored work :( |
| 42 |
// |
| 43 |
#pragma GCC system_header |
| 44 |
#endif |
| 45 |
|
| 46 |
namespace boost{ namespace math{ |
| 47 |
|
| 48 |
namespace detail |
| 49 |
{ |
| 50 |
// Functor log1p_series returns the next term in the Taylor series |
| 51 |
// pow(-1, k-1)*pow(x, k) / k |
| 52 |
// each time that operator() is invoked. |
| 53 |
// |
| 54 |
template <class T> |
| 55 |
struct log1p_series |
| 56 |
{ |
| 57 |
typedef T result_type; |
| 58 |
|
| 59 |
BOOST_MATH_GPU_ENABLED log1p_series(T x) |
| 60 |
: k(0), m_mult(-x), m_prod(-1){} |
| 61 |
|
| 62 |
BOOST_MATH_GPU_ENABLED T operator()() |
| 63 |
{ |
| 64 |
m_prod *= m_mult; |
| 65 |
return m_prod / ++k; |
| 66 |
} |
| 67 |
|
| 68 |
BOOST_MATH_GPU_ENABLED int count()const |
| 69 |
{ |
| 70 |
return k; |
| 71 |
} |
| 72 |
|
| 73 |
private: |
| 74 |
int k; |
| 75 |
const T m_mult; |
| 76 |
T m_prod; |
| 77 |
log1p_series(const log1p_series&) = delete; |
| 78 |
log1p_series& operator=(const log1p_series&) = delete; |
| 79 |
}; |
| 80 |
|
| 81 |
// Algorithm log1p is part of C99, but is not yet provided by many compilers. |
| 82 |
// |
| 83 |
// This version uses a Taylor series expansion for 0.5 > x > epsilon, which may |
| 84 |
// require up to std::numeric_limits<T>::digits+1 terms to be calculated. |
| 85 |
// It would be much more efficient to use the equivalence: |
| 86 |
// log(1+x) == (log(1+x) * x) / ((1-x) - 1) |
| 87 |
// Unfortunately many optimizing compilers make such a mess of this, that |
| 88 |
// it performs no better than log(1+x): which is to say not very well at all. |
| 89 |
// |
| 90 |
template <class T, class Policy> |
| 91 |
BOOST_MATH_GPU_ENABLED T log1p_imp(T const & x, const Policy& pol, const boost::math::integral_constant<int, 0>&) |
| 92 |
{ // The function returns the natural logarithm of 1 + x. |
| 93 |
typedef typename tools::promote_args<T>::type result_type; |
| 94 |
BOOST_MATH_STD_USING |
| 95 |
|
| 96 |
constexpr auto function = "boost::math::log1p<%1%>(%1%)"; |
| 97 |
|
| 98 |
if((x < -1) || (boost::math::isnan)(x)) |
| 99 |
return policies::raise_domain_error<T>(function, "log1p(x) requires x > -1, but got x = %1%.", x, pol); |
| 100 |
if(x == -1) |
| 101 |
return -policies::raise_overflow_error<T>(function, nullptr, pol); |
| 102 |
|
| 103 |
result_type a = abs(result_type(x)); |
| 104 |
if(a > result_type(0.5f)) |
| 105 |
return log(1 + result_type(x)); |
| 106 |
// Note that without numeric_limits specialisation support, |
| 107 |
// epsilon just returns zero, and our "optimisation" will always fail: |
| 108 |
if(a < tools::epsilon<result_type>()) |
| 109 |
return x; |
| 110 |
detail::log1p_series<result_type> s(x); |
| 111 |
boost::math::uintmax_t max_iter = policies::get_max_series_iterations<Policy>(); |
| 112 |
|
| 113 |
result_type result = tools::sum_series(s, policies::get_epsilon<result_type, Policy>(), max_iter); |
| 114 |
|
| 115 |
policies::check_series_iterations<T>(function, max_iter, pol); |
| 116 |
return result; |
| 117 |
} |
| 118 |
|
| 119 |
template <class T, class Policy> |
| 120 |
BOOST_MATH_GPU_ENABLED T log1p_imp(T const& x, const Policy& pol, const boost::math::integral_constant<int, 53>&) |
| 121 |
{ // The function returns the natural logarithm of 1 + x. |
| 122 |
BOOST_MATH_STD_USING |
| 123 |
|
| 124 |
constexpr auto function = "boost::math::log1p<%1%>(%1%)"; |
| 125 |
|
| 126 |
if(x < -1) |
| 127 |
return policies::raise_domain_error<T>(function, "log1p(x) requires x > -1, but got x = %1%.", x, pol); |
| 128 |
if(x == -1) |
| 129 |
return -policies::raise_overflow_error<T>(function, nullptr, pol); |
| 130 |
|
| 131 |
T a = fabs(x); |
| 132 |
if(a > 0.5f) |
| 133 |
return log(1 + x); |
| 134 |
// Note that without numeric_limits specialisation support, |
| 135 |
// epsilon just returns zero, and our "optimisation" will always fail: |
| 136 |
if(a < tools::epsilon<T>()) |
| 137 |
return x; |
| 138 |
|
| 139 |
// Maximum Deviation Found: 1.846e-017 |
| 140 |
// Expected Error Term: 1.843e-017 |
| 141 |
// Maximum Relative Change in Control Points: 8.138e-004 |
| 142 |
// Max Error found at double precision = 3.250766e-016 |
| 143 |
BOOST_MATH_STATIC const T P[] = { |
| 144 |
static_cast<T>(0.15141069795941984e-16L), |
| 145 |
static_cast<T>(0.35495104378055055e-15L), |
| 146 |
static_cast<T>(0.33333333333332835L), |
| 147 |
static_cast<T>(0.99249063543365859L), |
| 148 |
static_cast<T>(1.1143969784156509L), |
| 149 |
static_cast<T>(0.58052937949269651L), |
| 150 |
static_cast<T>(0.13703234928513215L), |
| 151 |
static_cast<T>(0.011294864812099712L) |
| 152 |
}; |
| 153 |
BOOST_MATH_STATIC const T Q[] = { |
| 154 |
static_cast<T>(1L), |
| 155 |
static_cast<T>(3.7274719063011499L), |
| 156 |
static_cast<T>(5.5387948649720334L), |
| 157 |
static_cast<T>(4.159201143419005L), |
| 158 |
static_cast<T>(1.6423855110312755L), |
| 159 |
static_cast<T>(0.31706251443180914L), |
| 160 |
static_cast<T>(0.022665554431410243L), |
| 161 |
static_cast<T>(-0.29252538135177773e-5L) |
| 162 |
}; |
| 163 |
|
| 164 |
T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x); |
| 165 |
result *= x; |
| 166 |
|
| 167 |
return result; |
| 168 |
} |
| 169 |
|
| 170 |
template <class T, class Policy> |
| 171 |
BOOST_MATH_GPU_ENABLED T log1p_imp(T const& x, const Policy& pol, const boost::math::integral_constant<int, 64>&) |
| 172 |
{ // The function returns the natural logarithm of 1 + x. |
| 173 |
BOOST_MATH_STD_USING |
| 174 |
|
| 175 |
constexpr auto function = "boost::math::log1p<%1%>(%1%)"; |
| 176 |
|
| 177 |
if(x < -1) |
| 178 |
return policies::raise_domain_error<T>(function, "log1p(x) requires x > -1, but got x = %1%.", x, pol); |
| 179 |
if(x == -1) |
| 180 |
return -policies::raise_overflow_error<T>(function, nullptr, pol); |
| 181 |
|
| 182 |
T a = fabs(x); |
| 183 |
if(a > 0.5f) |
| 184 |
return log(1 + x); |
| 185 |
// Note that without numeric_limits specialisation support, |
| 186 |
// epsilon just returns zero, and our "optimisation" will always fail: |
| 187 |
if(a < tools::epsilon<T>()) |
| 188 |
return x; |
| 189 |
|
| 190 |
// Maximum Deviation Found: 8.089e-20 |
| 191 |
// Expected Error Term: 8.088e-20 |
| 192 |
// Maximum Relative Change in Control Points: 9.648e-05 |
| 193 |
// Max Error found at long double precision = 2.242324e-19 |
| 194 |
BOOST_MATH_STATIC const T P[] = { |
| 195 |
BOOST_MATH_BIG_CONSTANT(T, 64, -0.807533446680736736712e-19), |
| 196 |
BOOST_MATH_BIG_CONSTANT(T, 64, -0.490881544804798926426e-18), |
| 197 |
BOOST_MATH_BIG_CONSTANT(T, 64, 0.333333333333333373941), |
| 198 |
BOOST_MATH_BIG_CONSTANT(T, 64, 1.17141290782087994162), |
| 199 |
BOOST_MATH_BIG_CONSTANT(T, 64, 1.62790522814926264694), |
| 200 |
BOOST_MATH_BIG_CONSTANT(T, 64, 1.13156411870766876113), |
| 201 |
BOOST_MATH_BIG_CONSTANT(T, 64, 0.408087379932853785336), |
| 202 |
BOOST_MATH_BIG_CONSTANT(T, 64, 0.0706537026422828914622), |
| 203 |
BOOST_MATH_BIG_CONSTANT(T, 64, 0.00441709903782239229447) |
| 204 |
}; |
| 205 |
BOOST_MATH_STATIC const T Q[] = { |
| 206 |
BOOST_MATH_BIG_CONSTANT(T, 64, 1.0), |
| 207 |
BOOST_MATH_BIG_CONSTANT(T, 64, 4.26423872346263928361), |
| 208 |
BOOST_MATH_BIG_CONSTANT(T, 64, 7.48189472704477708962), |
| 209 |
BOOST_MATH_BIG_CONSTANT(T, 64, 6.94757016732904280913), |
| 210 |
BOOST_MATH_BIG_CONSTANT(T, 64, 3.6493508622280767304), |
| 211 |
BOOST_MATH_BIG_CONSTANT(T, 64, 1.06884863623790638317), |
| 212 |
BOOST_MATH_BIG_CONSTANT(T, 64, 0.158292216998514145947), |
| 213 |
BOOST_MATH_BIG_CONSTANT(T, 64, 0.00885295524069924328658), |
| 214 |
BOOST_MATH_BIG_CONSTANT(T, 64, -0.560026216133415663808e-6) |
| 215 |
}; |
| 216 |
|
| 217 |
T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x); |
| 218 |
result *= x; |
| 219 |
|
| 220 |
return result; |
| 221 |
} |
| 222 |
|
| 223 |
template <class T, class Policy> |
| 224 |
BOOST_MATH_GPU_ENABLED T log1p_imp(T const& x, const Policy& pol, const boost::math::integral_constant<int, 24>&) |
| 225 |
{ // The function returns the natural logarithm of 1 + x. |
| 226 |
BOOST_MATH_STD_USING |
| 227 |
|
| 228 |
constexpr auto function = "boost::math::log1p<%1%>(%1%)"; |
| 229 |
|
| 230 |
if(x < -1) |
| 231 |
return policies::raise_domain_error<T>( |
| 232 |
function, "log1p(x) requires x > -1, but got x = %1%.", x, pol); |
| 233 |
if(x == -1) |
| 234 |
return -policies::raise_overflow_error<T>( |
| 235 |
function, nullptr, pol); |
| 236 |
|
| 237 |
T a = fabs(x); |
| 238 |
if(a > 0.5f) |
| 239 |
return log(1 + x); |
| 240 |
// Note that without numeric_limits specialisation support, |
| 241 |
// epsilon just returns zero, and our "optimisation" will always fail: |
| 242 |
if(a < tools::epsilon<T>()) |
| 243 |
return x; |
| 244 |
|
| 245 |
// Maximum Deviation Found: 6.910e-08 |
| 246 |
// Expected Error Term: 6.910e-08 |
| 247 |
// Maximum Relative Change in Control Points: 2.509e-04 |
| 248 |
// Max Error found at double precision = 6.910422e-08 |
| 249 |
// Max Error found at float precision = 8.357242e-08 |
| 250 |
BOOST_MATH_STATIC const T P[] = { |
| 251 |
-0.671192866803148236519e-7L, |
| 252 |
0.119670999140731844725e-6L, |
| 253 |
0.333339469182083148598L, |
| 254 |
0.237827183019664122066L |
| 255 |
}; |
| 256 |
BOOST_MATH_STATIC const T Q[] = { |
| 257 |
1L, |
| 258 |
1.46348272586988539733L, |
| 259 |
0.497859871350117338894L, |
| 260 |
-0.00471666268910169651936L |
| 261 |
}; |
| 262 |
|
| 263 |
T result = 1 - x / 2 + tools::evaluate_polynomial(P, x) / tools::evaluate_polynomial(Q, x); |
| 264 |
result *= x; |
| 265 |
|
| 266 |
return result; |
| 267 |
} |
| 268 |
|
| 269 |
} // namespace detail |
| 270 |
|
| 271 |
template <class T, class Policy> |
| 272 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type log1p(T x, const Policy&) |
| 273 |
{ |
| 274 |
typedef typename tools::promote_args<T>::type result_type; |
| 275 |
typedef typename policies::evaluation<result_type, Policy>::type value_type; |
| 276 |
typedef typename policies::precision<result_type, Policy>::type precision_type; |
| 277 |
typedef typename policies::normalise< |
| 278 |
Policy, |
| 279 |
policies::promote_float<false>, |
| 280 |
policies::promote_double<false>, |
| 281 |
policies::discrete_quantile<>, |
| 282 |
policies::assert_undefined<> >::type forwarding_policy; |
| 283 |
|
| 284 |
typedef boost::math::integral_constant<int, |
| 285 |
precision_type::value <= 0 ? 0 : |
| 286 |
precision_type::value <= 53 ? 53 : |
| 287 |
precision_type::value <= 64 ? 64 : 0 |
| 288 |
> tag_type; |
| 289 |
|
| 290 |
return policies::checked_narrowing_cast<result_type, forwarding_policy>( |
| 291 |
detail::log1p_imp(static_cast<value_type>(x), forwarding_policy(), tag_type()), "boost::math::log1p<%1%>(%1%)"); |
| 292 |
} |
| 293 |
|
| 294 |
template <class Policy> |
| 295 |
BOOST_MATH_GPU_ENABLED inline float log1p(float x, const Policy& pol) |
| 296 |
{ |
| 297 |
if(x < -1) |
| 298 |
return policies::raise_domain_error<float>("log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol); |
| 299 |
if(x == -1) |
| 300 |
return -policies::raise_overflow_error<float>("log1p<%1%>(%1%)", nullptr, pol); |
| 301 |
#ifndef BOOST_MATH_HAS_NVRTC |
| 302 |
return std::log1p(x); |
| 303 |
#else |
| 304 |
return ::log1pf(x); |
| 305 |
#endif |
| 306 |
} |
| 307 |
#ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS |
| 308 |
template <class Policy> |
| 309 |
BOOST_MATH_GPU_ENABLED inline long double log1p(long double x, const Policy& pol) |
| 310 |
{ |
| 311 |
if(x < -1) |
| 312 |
return policies::raise_domain_error<long double>("log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol); |
| 313 |
if(x == -1) |
| 314 |
return -policies::raise_overflow_error<long double>("log1p<%1%>(%1%)", nullptr, pol); |
| 315 |
return std::log1p(x); |
| 316 |
} |
| 317 |
#endif |
| 318 |
template <class Policy> |
| 319 |
BOOST_MATH_GPU_ENABLED inline double log1p(double x, const Policy& pol) |
| 320 |
{ |
| 321 |
if(x < -1) |
| 322 |
return policies::raise_domain_error<double>("log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol); |
| 323 |
if(x == -1) |
| 324 |
return -policies::raise_overflow_error<double>("log1p<%1%>(%1%)", nullptr, pol); |
| 325 |
#ifndef BOOST_MATH_HAS_NVRTC |
| 326 |
return std::log1p(x); |
| 327 |
#else |
| 328 |
return ::log1p(x); |
| 329 |
#endif |
| 330 |
} |
| 331 |
|
| 332 |
template <class T> |
| 333 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type log1p(T x) |
| 334 |
{ |
| 335 |
return boost::math::log1p(x, policies::policy<>()); |
| 336 |
} |
| 337 |
// |
| 338 |
// Compute log(1+x)-x: |
| 339 |
// |
| 340 |
template <class T, class Policy> |
| 341 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type |
| 342 |
log1pmx(T x, const Policy& pol) |
| 343 |
{ |
| 344 |
typedef typename tools::promote_args<T>::type result_type; |
| 345 |
BOOST_MATH_STD_USING |
| 346 |
constexpr auto function = "boost::math::log1pmx<%1%>(%1%)"; |
| 347 |
|
| 348 |
if(x < -1) |
| 349 |
return policies::raise_domain_error<T>(function, "log1pmx(x) requires x > -1, but got x = %1%.", x, pol); |
| 350 |
if(x == -1) |
| 351 |
return -policies::raise_overflow_error<T>(function, nullptr, pol); |
| 352 |
|
| 353 |
result_type a = abs(result_type(x)); |
| 354 |
if(a > result_type(0.95f)) |
| 355 |
return log(1 + result_type(x)) - result_type(x); |
| 356 |
// Note that without numeric_limits specialisation support, |
| 357 |
// epsilon just returns zero, and our "optimisation" will always fail: |
| 358 |
if(a < tools::epsilon<result_type>()) |
| 359 |
return -x * x / 2; |
| 360 |
boost::math::detail::log1p_series<T> s(x); |
| 361 |
s(); |
| 362 |
boost::math::uintmax_t max_iter = policies::get_max_series_iterations<Policy>(); |
| 363 |
|
| 364 |
T result = boost::math::tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter); |
| 365 |
|
| 366 |
policies::check_series_iterations<T>(function, max_iter, pol); |
| 367 |
return result; |
| 368 |
} |
| 369 |
|
| 370 |
template <class T> |
| 371 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type log1pmx(T x) |
| 372 |
{ |
| 373 |
return log1pmx(x, policies::policy<>()); |
| 374 |
} |
| 375 |
|
| 376 |
// |
| 377 |
// Specific width floating point types: |
| 378 |
// |
| 379 |
#ifdef __STDCPP_FLOAT32_T__ |
| 380 |
template <class Policy> |
| 381 |
BOOST_MATH_GPU_ENABLED inline std::float32_t log1p(std::float32_t x, const Policy& pol) |
| 382 |
{ |
| 383 |
return boost::math::log1p(static_cast<float>(x), pol); |
| 384 |
} |
| 385 |
#endif |
| 386 |
#ifdef __STDCPP_FLOAT64_T__ |
| 387 |
template <class Policy> |
| 388 |
BOOST_MATH_GPU_ENABLED inline std::float64_t log1p(std::float64_t x, const Policy& pol) |
| 389 |
{ |
| 390 |
return boost::math::log1p(static_cast<double>(x), pol); |
| 391 |
} |
| 392 |
#endif |
| 393 |
#ifdef __STDCPP_FLOAT128_T__ |
| 394 |
template <class Policy> |
| 395 |
BOOST_MATH_GPU_ENABLED inline std::float128_t log1p(std::float128_t x, const Policy& pol) |
| 396 |
{ |
| 397 |
if constexpr (std::numeric_limits<long double>::digits == std::numeric_limits<std::float128_t>::digits) |
| 398 |
{ |
| 399 |
return boost::math::log1p(static_cast<long double>(x), pol); |
| 400 |
} |
| 401 |
else |
| 402 |
{ |
| 403 |
return boost::math::detail::log1p_imp(x, pol, boost::math::integral_constant<int, 0>()); |
| 404 |
} |
| 405 |
} |
| 406 |
#endif |
| 407 |
} // namespace math |
| 408 |
} // namespace boost |
| 409 |
|
| 410 |
#ifdef _MSC_VER |
| 411 |
#pragma warning(pop) |
| 412 |
#endif |
| 413 |
|
| 414 |
#endif // BOOST_MATH_LOG1P_INCLUDED |
| 415 |
|
| 416 |
|
| 417 |
|