series.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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)
  5. #include <boost/math/tools/series.hpp>
  6. #include <boost/assert.hpp>
  7. #include <iostream>
  8. #include <complex>
  9. #include <cassert>
  10. //[series_log1p
  11. template <class T>
  12. struct log1p_series
  13. {
  14. // we must define a result_type typedef:
  15. typedef T result_type;
  16. log1p_series(T x)
  17. : k(0), m_mult(-x), m_prod(-1) {}
  18. T operator()()
  19. {
  20. // This is the function operator invoked by the summation
  21. // algorithm, the first call to this operator should return
  22. // the first term of the series, the second call the second
  23. // term and so on.
  24. m_prod *= m_mult;
  25. return m_prod / ++k;
  26. }
  27. private:
  28. int k;
  29. const T m_mult;
  30. T m_prod;
  31. };
  32. //]
  33. //[series_log1p_func
  34. template <class T>
  35. T log1p(T x)
  36. {
  37. // We really should add some error checking on x here!
  38. BOOST_ASSERT(std::fabs(x) < 1);
  39. // Construct the series functor:
  40. log1p_series<T> s(x);
  41. // Set a limit on how many iterations we permit:
  42. boost::uintmax_t max_iter = 1000;
  43. // Add it up, with enough precision for full machine precision:
  44. return boost::math::tools::sum_series(s, std::numeric_limits<T>::epsilon(), max_iter);
  45. }
  46. //]
  47. //[series_clog1p_func
  48. template <class T>
  49. struct log1p_series<std::complex<T> >
  50. {
  51. // we must define a result_type typedef:
  52. typedef std::complex<T> result_type;
  53. log1p_series(std::complex<T> x)
  54. : k(0), m_mult(-x), m_prod(-1) {}
  55. std::complex<T> operator()()
  56. {
  57. // This is the function operator invoked by the summation
  58. // algorithm, the first call to this operator should return
  59. // the first term of the series, the second call the second
  60. // term and so on.
  61. m_prod *= m_mult;
  62. return m_prod / T(++k);
  63. }
  64. private:
  65. int k;
  66. const std::complex<T> m_mult;
  67. std::complex<T> m_prod;
  68. };
  69. template <class T>
  70. std::complex<T> log1p(std::complex<T> x)
  71. {
  72. // We really should add some error checking on x here!
  73. BOOST_ASSERT(abs(x) < 1);
  74. // Construct the series functor:
  75. log1p_series<std::complex<T> > s(x);
  76. // Set a limit on how many iterations we permit:
  77. boost::uintmax_t max_iter = 1000;
  78. // Add it up, with enough precision for full machine precision:
  79. return boost::math::tools::sum_series(s, std::complex<T>(std::numeric_limits<T>::epsilon()), max_iter);
  80. }
  81. //]
  82. int main()
  83. {
  84. using namespace boost::math::tools;
  85. std::cout << log1p(0.25) << std::endl;
  86. std::cout << log1p(std::complex<double>(0.25, 0.25)) << std::endl;
  87. return 0;
  88. }