log1p.hpp 16 KB

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