Back to home page

EIC code displayed by LXR

 
 

    


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

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // weighted_moment.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_MOMENT_HPP_EAN_15_11_2005
0009 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005
0010 
0011 #include <boost/config/no_tr1/cmath.hpp>
0012 #include <boost/mpl/int.hpp>
0013 #include <boost/mpl/assert.hpp>
0014 #include <boost/mpl/placeholders.hpp>
0015 #include <boost/preprocessor/arithmetic/inc.hpp>
0016 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
0017 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
0018 #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
0019 #include <boost/accumulators/framework/accumulator_base.hpp>
0020 #include <boost/accumulators/framework/extractor.hpp>
0021 #include <boost/accumulators/numeric/functional.hpp>
0022 #include <boost/accumulators/framework/parameters/sample.hpp>
0023 #include <boost/accumulators/framework/depends_on.hpp>
0024 #include <boost/accumulators/statistics_fwd.hpp>
0025 #include <boost/accumulators/statistics/count.hpp>
0026 #include <boost/accumulators/statistics/moment.hpp> // for pow()
0027 #include <boost/accumulators/statistics/sum.hpp>
0028 
0029 namespace boost { namespace accumulators
0030 {
0031 
0032 namespace impl
0033 {
0034     ///////////////////////////////////////////////////////////////////////////////
0035     // weighted_moment_impl
0036     template<typename N, typename Sample, typename Weight>
0037     struct weighted_moment_impl
0038       : accumulator_base // TODO: also depends_on sum of powers
0039     {
0040         BOOST_MPL_ASSERT_RELATION(N::value, >, 0);
0041         typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
0042         // for boost::result_of
0043         typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
0044 
0045         template<typename Args>
0046         weighted_moment_impl(Args const &args)
0047           : sum(args[sample | Sample()] * numeric::one<Weight>::value)
0048         {
0049         }
0050 
0051         template<typename Args>
0052         void operator ()(Args const &args)
0053         {
0054             this->sum += args[weight] * numeric::pow(args[sample], N());
0055         }
0056 
0057         template<typename Args>
0058         result_type result(Args const &args) const
0059         {
0060             return numeric::fdiv(this->sum, sum_of_weights(args));
0061         }
0062 
0063         // make this accumulator serializeable
0064         template<class Archive>
0065         void serialize(Archive & ar, const unsigned int file_version)
0066         { 
0067             ar & sum;
0068         }
0069 
0070     private:
0071         weighted_sample sum;
0072     };
0073 
0074 } // namespace impl
0075 
0076 ///////////////////////////////////////////////////////////////////////////////
0077 // tag::weighted_moment
0078 //
0079 namespace tag
0080 {
0081     template<int N>
0082     struct weighted_moment
0083       : depends_on<count, sum_of_weights>
0084     {
0085         /// INTERNAL ONLY
0086         ///
0087         typedef accumulators::impl::weighted_moment_impl<mpl::int_<N>, mpl::_1, mpl::_2> impl;
0088     };
0089 }
0090 
0091 ///////////////////////////////////////////////////////////////////////////////
0092 // extract::weighted_moment
0093 //
0094 namespace extract
0095 {
0096     BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, weighted_moment, (int))
0097 }
0098 
0099 using extract::weighted_moment;
0100 
0101 }} // namespace boost::accumulators
0102 
0103 #endif