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