weighted_sum.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // make this accumulator serializeable
  50. template<class Archive>
  51. void serialize(Archive & ar, const unsigned int file_version)
  52. {
  53. ar & weighted_sum_;
  54. }
  55. private:
  56. weighted_sample weighted_sum_;
  57. };
  58. } // namespace impl
  59. ///////////////////////////////////////////////////////////////////////////////
  60. // tag::weighted_sum
  61. //
  62. namespace tag
  63. {
  64. struct weighted_sum
  65. : depends_on<>
  66. {
  67. /// INTERNAL ONLY
  68. ///
  69. typedef accumulators::impl::weighted_sum_impl<mpl::_1, mpl::_2, tag::sample> impl;
  70. };
  71. template<typename VariateType, typename VariateTag>
  72. struct weighted_sum_of_variates
  73. : depends_on<>
  74. {
  75. /// INTERNAL ONLY
  76. ///
  77. typedef accumulators::impl::weighted_sum_impl<VariateType, mpl::_2, VariateTag> impl;
  78. };
  79. struct abstract_weighted_sum_of_variates
  80. : depends_on<>
  81. {
  82. };
  83. }
  84. ///////////////////////////////////////////////////////////////////////////////
  85. // extract::weighted_sum
  86. //
  87. namespace extract
  88. {
  89. extractor<tag::weighted_sum> const weighted_sum = {};
  90. extractor<tag::abstract_weighted_sum_of_variates> const weighted_sum_of_variates = {};
  91. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum)
  92. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_of_variates)
  93. }
  94. using extract::weighted_sum;
  95. using extract::weighted_sum_of_variates;
  96. template<typename VariateType, typename VariateTag>
  97. struct feature_of<tag::weighted_sum_of_variates<VariateType, VariateTag> >
  98. : feature_of<tag::abstract_weighted_sum_of_variates>
  99. {
  100. };
  101. }} // namespace boost::accumulators
  102. #endif