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