1 // (C) Copyright John Maddock 2018.
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)
6 #include <boost/math/tools/series.hpp>
7 #include <boost/assert.hpp>
17 // we must define a result_type typedef:
18 typedef T result_type;
21 : k(0), m_mult(-x), m_prod(-1) {}
25 // This is the function operator invoked by the summation
26 // algorithm, the first call to this operator should return
27 // the first term of the series, the second call the second
44 // We really should add some error checking on x here!
45 BOOST_ASSERT(std::fabs(x) < 1);
47 // Construct the series functor:
49 // Set a limit on how many iterations we permit:
50 boost::uintmax_t max_iter = 1000;
51 // Add it up, with enough precision for full machine precision:
52 return boost::math::tools::sum_series(s, std::numeric_limits<T>::epsilon(), max_iter);
58 struct log1p_series<std::complex<T> >
60 // we must define a result_type typedef:
61 typedef std::complex<T> result_type;
63 log1p_series(std::complex<T> x)
64 : k(0), m_mult(-x), m_prod(-1) {}
66 std::complex<T> operator()()
68 // This is the function operator invoked by the summation
69 // algorithm, the first call to this operator should return
70 // the first term of the series, the second call the second
73 return m_prod / T(++k);
78 const std::complex<T> m_mult;
79 std::complex<T> m_prod;
84 std::complex<T> log1p(std::complex<T> x)
86 // We really should add some error checking on x here!
87 BOOST_ASSERT(abs(x) < 1);
89 // Construct the series functor:
90 log1p_series<std::complex<T> > s(x);
91 // Set a limit on how many iterations we permit:
92 boost::uintmax_t max_iter = 1000;
93 // Add it up, with enough precision for full machine precision:
94 return boost::math::tools::sum_series(s, std::complex<T>(std::numeric_limits<T>::epsilon()), max_iter);
100 using namespace boost::math::tools;
102 std::cout << log1p(0.25) << std::endl;
104 std::cout << log1p(std::complex<double>(0.25, 0.25)) << std::endl;