weighted_sum.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // weighted_sum.hpp
  3. //
  4. // Copyright 2006 Eric Niebler, Olivier Gygi. 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_WEIGHTED_SUM_HPP_EAN_28_10_2005
  8. #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_HPP_EAN_28_10_2005
  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/parameters/weight.hpp>
  15. #include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
  16. #include <boost/accumulators/framework/depends_on.hpp>
  17. #include <boost/accumulators/statistics_fwd.hpp>
  18. namespace boost { namespace accumulators
  19. {
  20. namespace impl
  21. {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // weighted_sum_impl
  24. template<typename Sample, typename Weight, typename Tag>
  25. struct weighted_sum_impl
  26. : accumulator_base
  27. {
  28. typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
  29. // for boost::result_of
  30. typedef weighted_sample result_type;
  31. template<typename Args>
  32. weighted_sum_impl(Args const &args)
  33. : weighted_sum_(
  34. args[parameter::keyword<Tag>::get() | Sample()]
  35. * numeric::one<Weight>::value
  36. )
  37. {
  38. }
  39. template<typename Args>
  40. void operator ()(Args const &args)
  41. {
  42. // what about overflow?
  43. this->weighted_sum_ += args[parameter::keyword<Tag>::get()] * args[weight];
  44. }
  45. result_type result(dont_care) const
  46. {
  47. return this->weighted_sum_;
  48. }
  49. private:
  50. weighted_sample weighted_sum_;
  51. };
  52. } // namespace impl
  53. ///////////////////////////////////////////////////////////////////////////////
  54. // tag::weighted_sum
  55. //
  56. namespace tag
  57. {
  58. struct weighted_sum
  59. : depends_on<>
  60. {
  61. /// INTERNAL ONLY
  62. ///
  63. typedef accumulators::impl::weighted_sum_impl<mpl::_1, mpl::_2, tag::sample> impl;
  64. };
  65. template<typename VariateType, typename VariateTag>
  66. struct weighted_sum_of_variates
  67. : depends_on<>
  68. {
  69. /// INTERNAL ONLY
  70. ///
  71. typedef accumulators::impl::weighted_sum_impl<VariateType, mpl::_2, VariateTag> impl;
  72. };
  73. struct abstract_weighted_sum_of_variates
  74. : depends_on<>
  75. {
  76. };
  77. }
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // extract::weighted_sum
  80. //
  81. namespace extract
  82. {
  83. extractor<tag::weighted_sum> const weighted_sum = {};
  84. extractor<tag::abstract_weighted_sum_of_variates> const weighted_sum_of_variates = {};
  85. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum)
  86. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_of_variates)
  87. }
  88. using extract::weighted_sum;
  89. using extract::weighted_sum_of_variates;
  90. template<typename VariateType, typename VariateTag>
  91. struct feature_of<tag::weighted_sum_of_variates<VariateType, VariateTag> >
  92. : feature_of<tag::abstract_weighted_sum_of_variates>
  93. {
  94. };
  95. }} // namespace boost::accumulators
  96. #endif