log1p.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 dianostic 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 mpl::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 mpl::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 mpl::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 mpl::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 mpl::int_<N>&){}
  247. static void do_init(const mpl::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 typename mpl::if_<
  275. mpl::less_equal<precision_type, mpl::int_<0> >,
  276. mpl::int_<0>,
  277. typename mpl::if_<
  278. mpl::less_equal<precision_type, mpl::int_<53> >,
  279. mpl::int_<53>, // double
  280. typename mpl::if_<
  281. mpl::less_equal<precision_type, mpl::int_<64> >,
  282. mpl::int_<64>, // 80-bit long double
  283. mpl::int_<0> // too many bits, use generic version.
  284. >::type
  285. >::type
  286. >::type tag_type;
  287. detail::log1p_initializer<value_type, forwarding_policy, tag_type>::force_instantiate();
  288. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  289. detail::log1p_imp(static_cast<value_type>(x), forwarding_policy(), tag_type()), "boost::math::log1p<%1%>(%1%)");
  290. }
  291. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  292. // These overloads work around a type deduction bug:
  293. inline float log1p(float z)
  294. {
  295. return log1p<float>(z);
  296. }
  297. inline double log1p(double z)
  298. {
  299. return log1p<double>(z);
  300. }
  301. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  302. inline long double log1p(long double z)
  303. {
  304. return log1p<long double>(z);
  305. }
  306. #endif
  307. #endif
  308. #ifdef log1p
  309. # ifndef BOOST_HAS_LOG1P
  310. # define BOOST_HAS_LOG1P
  311. # endif
  312. # undef log1p
  313. #endif
  314. #if defined(BOOST_HAS_LOG1P) && !(defined(__osf__) && defined(__DECCXX_VER))
  315. # ifdef BOOST_MATH_USE_C99
  316. template <class Policy>
  317. inline float log1p(float x, const Policy& pol)
  318. {
  319. if(x < -1)
  320. return policies::raise_domain_error<float>(
  321. "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
  322. if(x == -1)
  323. return -policies::raise_overflow_error<float>(
  324. "log1p<%1%>(%1%)", 0, pol);
  325. return ::log1pf(x);
  326. }
  327. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  328. template <class Policy>
  329. inline long double log1p(long double x, const Policy& pol)
  330. {
  331. if(x < -1)
  332. return policies::raise_domain_error<long double>(
  333. "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
  334. if(x == -1)
  335. return -policies::raise_overflow_error<long double>(
  336. "log1p<%1%>(%1%)", 0, pol);
  337. return ::log1pl(x);
  338. }
  339. #endif
  340. #else
  341. template <class Policy>
  342. inline float log1p(float x, const Policy& pol)
  343. {
  344. if(x < -1)
  345. return policies::raise_domain_error<float>(
  346. "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
  347. if(x == -1)
  348. return -policies::raise_overflow_error<float>(
  349. "log1p<%1%>(%1%)", 0, pol);
  350. return ::log1p(x);
  351. }
  352. #endif
  353. template <class Policy>
  354. inline double log1p(double x, const Policy& pol)
  355. {
  356. if(x < -1)
  357. return policies::raise_domain_error<double>(
  358. "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
  359. if(x == -1)
  360. return -policies::raise_overflow_error<double>(
  361. "log1p<%1%>(%1%)", 0, pol);
  362. return ::log1p(x);
  363. }
  364. #elif defined(_MSC_VER) && (BOOST_MSVC >= 1400)
  365. //
  366. // You should only enable this branch if you are absolutely sure
  367. // that your compilers optimizer won't mess this code up!!
  368. // Currently tested with VC8 and Intel 9.1.
  369. //
  370. template <class Policy>
  371. inline double log1p(double x, const Policy& pol)
  372. {
  373. if(x < -1)
  374. return policies::raise_domain_error<double>(
  375. "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
  376. if(x == -1)
  377. return -policies::raise_overflow_error<double>(
  378. "log1p<%1%>(%1%)", 0, pol);
  379. double u = 1+x;
  380. if(u == 1.0)
  381. return x;
  382. else
  383. return ::log(u)*(x/(u-1.0));
  384. }
  385. template <class Policy>
  386. inline float log1p(float x, const Policy& pol)
  387. {
  388. return static_cast<float>(boost::math::log1p(static_cast<double>(x), pol));
  389. }
  390. #ifndef _WIN32_WCE
  391. //
  392. // For some reason this fails to compile under WinCE...
  393. // Needs more investigation.
  394. //
  395. template <class Policy>
  396. inline long double log1p(long double x, const Policy& pol)
  397. {
  398. if(x < -1)
  399. return policies::raise_domain_error<long double>(
  400. "log1p<%1%>(%1%)", "log1p(x) requires x > -1, but got x = %1%.", x, pol);
  401. if(x == -1)
  402. return -policies::raise_overflow_error<long double>(
  403. "log1p<%1%>(%1%)", 0, pol);
  404. long double u = 1+x;
  405. if(u == 1.0)
  406. return x;
  407. else
  408. return ::logl(u)*(x/(u-1.0));
  409. }
  410. #endif
  411. #endif
  412. template <class T>
  413. inline typename tools::promote_args<T>::type log1p(T x)
  414. {
  415. return boost::math::log1p(x, policies::policy<>());
  416. }
  417. //
  418. // Compute log(1+x)-x:
  419. //
  420. template <class T, class Policy>
  421. inline typename tools::promote_args<T>::type
  422. log1pmx(T x, const Policy& pol)
  423. {
  424. typedef typename tools::promote_args<T>::type result_type;
  425. BOOST_MATH_STD_USING
  426. static const char* function = "boost::math::log1pmx<%1%>(%1%)";
  427. if(x < -1)
  428. return policies::raise_domain_error<T>(
  429. function, "log1pmx(x) requires x > -1, but got x = %1%.", x, pol);
  430. if(x == -1)
  431. return -policies::raise_overflow_error<T>(
  432. function, 0, pol);
  433. result_type a = abs(result_type(x));
  434. if(a > result_type(0.95f))
  435. return log(1 + result_type(x)) - result_type(x);
  436. // Note that without numeric_limits specialisation support,
  437. // epsilon just returns zero, and our "optimisation" will always fail:
  438. if(a < tools::epsilon<result_type>())
  439. return -x * x / 2;
  440. boost::math::detail::log1p_series<T> s(x);
  441. s();
  442. boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  443. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  444. T zero = 0;
  445. T result = boost::math::tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter, zero);
  446. #else
  447. T result = boost::math::tools::sum_series(s, policies::get_epsilon<T, Policy>(), max_iter);
  448. #endif
  449. policies::check_series_iterations<T>(function, max_iter, pol);
  450. return result;
  451. }
  452. template <class T>
  453. inline typename tools::promote_args<T>::type log1pmx(T x)
  454. {
  455. return log1pmx(x, policies::policy<>());
  456. }
  457. } // namespace math
  458. } // namespace boost
  459. #ifdef _MSC_VER
  460. #pragma warning(pop)
  461. #endif
  462. #endif // BOOST_MATH_LOG1P_INCLUDED