Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:22

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // weighted_sum.hpp
0003 //
0004 //  Copyright 2006 Eric Niebler, Olivier Gygi. Distributed under the Boost
0005 //  Software License, Version 1.0. (See accompanying file
0006 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_HPP_EAN_28_10_2005
0009 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_HPP_EAN_28_10_2005
0010 
0011 #include <boost/mpl/placeholders.hpp>
0012 #include <boost/accumulators/framework/accumulator_base.hpp>
0013 #include <boost/accumulators/framework/extractor.hpp>
0014 #include <boost/accumulators/numeric/functional.hpp>
0015 #include <boost/accumulators/framework/parameters/sample.hpp>
0016 #include <boost/accumulators/framework/parameters/weight.hpp>
0017 #include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
0018 #include <boost/accumulators/framework/depends_on.hpp>
0019 #include <boost/accumulators/statistics_fwd.hpp>
0020 
0021 namespace boost { namespace accumulators
0022 {
0023 
0024 namespace impl
0025 {
0026     ///////////////////////////////////////////////////////////////////////////////
0027     // weighted_sum_impl
0028     template<typename Sample, typename Weight, typename Tag>
0029     struct weighted_sum_impl
0030       : accumulator_base
0031     {
0032         typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
0033 
0034         // for boost::result_of
0035         typedef weighted_sample result_type;
0036 
0037         template<typename Args>
0038         weighted_sum_impl(Args const &args)
0039           : weighted_sum_(
0040                 args[parameter::keyword<Tag>::get() | Sample()]
0041                   * numeric::one<Weight>::value
0042             )
0043         {
0044         }
0045 
0046         template<typename Args>
0047         void operator ()(Args const &args)
0048         {
0049             // what about overflow?
0050             this->weighted_sum_ += args[parameter::keyword<Tag>::get()] * args[weight];
0051         }
0052 
0053         result_type result(dont_care) const
0054         {
0055             return this->weighted_sum_;
0056         }
0057 
0058         // make this accumulator serializeable
0059         template<class Archive>
0060         void serialize(Archive & ar, const unsigned int /* file_version */)
0061         {
0062             ar & weighted_sum_;
0063         }
0064 
0065     private:
0066 
0067         weighted_sample weighted_sum_;
0068     };
0069 
0070 } // namespace impl
0071 
0072 ///////////////////////////////////////////////////////////////////////////////
0073 // tag::weighted_sum
0074 //
0075 namespace tag
0076 {
0077     struct weighted_sum
0078       : depends_on<>
0079     {
0080         /// INTERNAL ONLY
0081         ///
0082         typedef accumulators::impl::weighted_sum_impl<mpl::_1, mpl::_2, tag::sample> impl;
0083     };
0084 
0085     template<typename VariateType, typename VariateTag>
0086     struct weighted_sum_of_variates
0087       : depends_on<>
0088     {
0089         /// INTERNAL ONLY
0090         ///
0091         typedef accumulators::impl::weighted_sum_impl<VariateType, mpl::_2, VariateTag> impl;
0092     };
0093 
0094     struct abstract_weighted_sum_of_variates
0095       : depends_on<>
0096     {
0097     };
0098 }
0099 
0100 ///////////////////////////////////////////////////////////////////////////////
0101 // extract::weighted_sum
0102 //
0103 namespace extract
0104 {
0105     extractor<tag::weighted_sum> const weighted_sum = {};
0106     extractor<tag::abstract_weighted_sum_of_variates> const weighted_sum_of_variates = {};
0107 
0108     BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum)
0109     BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_of_variates)
0110 }
0111 
0112 using extract::weighted_sum;
0113 using extract::weighted_sum_of_variates;
0114 
0115 template<typename VariateType, typename VariateTag>
0116 struct feature_of<tag::weighted_sum_of_variates<VariateType, VariateTag> >
0117   : feature_of<tag::abstract_weighted_sum_of_variates>
0118 {
0119 };
0120 
0121 }} // namespace boost::accumulators
0122 
0123 #endif