log1p.hpp 16 KB

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