log1p.hpp 16 KB

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