log1p.hpp 16 KB

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