1 ///////////////////////////////////////////////////////////////////////////////
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)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_HPP_EAN_28_10_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_HPP_EAN_28_10_2005
11 #include <boost/mpl/placeholders.hpp>
12 #include <boost/accumulators/framework/accumulator_base.hpp>
13 #include <boost/accumulators/framework/extractor.hpp>
14 #include <boost/accumulators/numeric/functional.hpp>
15 #include <boost/accumulators/framework/parameters/sample.hpp>
16 #include <boost/accumulators/framework/parameters/weight.hpp>
17 #include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
18 #include <boost/accumulators/framework/depends_on.hpp>
19 #include <boost/accumulators/statistics_fwd.hpp>
21 namespace boost { namespace accumulators
26 ///////////////////////////////////////////////////////////////////////////////
28 template<typename Sample, typename Weight, typename Tag>
29 struct weighted_sum_impl
32 typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
34 // for boost::result_of
35 typedef weighted_sample result_type;
37 template<typename Args>
38 weighted_sum_impl(Args const &args)
40 args[parameter::keyword<Tag>::get() | Sample()]
41 * numeric::one<Weight>::value
46 template<typename Args>
47 void operator ()(Args const &args)
49 // what about overflow?
50 this->weighted_sum_ += args[parameter::keyword<Tag>::get()] * args[weight];
53 result_type result(dont_care) const
55 return this->weighted_sum_;
60 weighted_sample weighted_sum_;
65 ///////////////////////////////////////////////////////////////////////////////
75 typedef accumulators::impl::weighted_sum_impl<mpl::_1, mpl::_2, tag::sample> impl;
78 template<typename VariateType, typename VariateTag>
79 struct weighted_sum_of_variates
84 typedef accumulators::impl::weighted_sum_impl<VariateType, mpl::_2, VariateTag> impl;
87 struct abstract_weighted_sum_of_variates
93 ///////////////////////////////////////////////////////////////////////////////
94 // extract::weighted_sum
98 extractor<tag::weighted_sum> const weighted_sum = {};
99 extractor<tag::abstract_weighted_sum_of_variates> const weighted_sum_of_variates = {};
101 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum)
102 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_of_variates)
105 using extract::weighted_sum;
106 using extract::weighted_sum_of_variates;
108 template<typename VariateType, typename VariateTag>
109 struct feature_of<tag::weighted_sum_of_variates<VariateType, VariateTag> >
110 : feature_of<tag::abstract_weighted_sum_of_variates>
114 }} // namespace boost::accumulators