log1p.hpp 15 KB

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