rolling_mean.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // rolling_mean.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_ACCUMULATORS_STATISTICS_ROLLING_MEAN_HPP_EAN_26_12_2008
  8. #define BOOST_ACCUMULATORS_STATISTICS_ROLLING_MEAN_HPP_EAN_26_12_2008
  9. #include <boost/mpl/placeholders.hpp>
  10. #include <boost/accumulators/framework/accumulator_base.hpp>
  11. #include <boost/accumulators/framework/extractor.hpp>
  12. #include <boost/accumulators/numeric/functional.hpp>
  13. #include <boost/accumulators/framework/parameters/sample.hpp>
  14. #include <boost/accumulators/framework/depends_on.hpp>
  15. #include <boost/accumulators/statistics_fwd.hpp>
  16. #include <boost/accumulators/statistics/rolling_sum.hpp>
  17. #include <boost/accumulators/statistics/rolling_count.hpp>
  18. namespace boost { namespace accumulators
  19. {
  20. namespace impl
  21. {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // rolling_mean_impl
  24. // returns the unshifted results from the shifted rolling window
  25. template<typename Sample>
  26. struct rolling_mean_impl
  27. : accumulator_base
  28. {
  29. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
  30. rolling_mean_impl(dont_care)
  31. {}
  32. template<typename Args>
  33. result_type result(Args const &args) const
  34. {
  35. return numeric::fdiv(rolling_sum(args), rolling_count(args));
  36. }
  37. };
  38. } // namespace impl
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // tag::rolling_mean
  41. //
  42. namespace tag
  43. {
  44. struct rolling_mean
  45. : depends_on< rolling_sum, rolling_count >
  46. {
  47. /// INTERNAL ONLY
  48. ///
  49. typedef accumulators::impl::rolling_mean_impl< mpl::_1 > impl;
  50. #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
  51. /// tag::rolling_window::window_size named parameter
  52. static boost::parameter::keyword<tag::rolling_window_size> const window_size;
  53. #endif
  54. };
  55. } // namespace tag
  56. ///////////////////////////////////////////////////////////////////////////////
  57. // extract::rolling_mean
  58. //
  59. namespace extract
  60. {
  61. extractor<tag::rolling_mean> const rolling_mean = {};
  62. BOOST_ACCUMULATORS_IGNORE_GLOBAL(rolling_mean)
  63. }
  64. using extract::rolling_mean;
  65. }} // namespace boost::accumulators
  66. #endif