/////////////////////////////////////////////////////////////////////////////// // weighted_moment.hpp // // Copyright 2006, Eric Niebler, Olivier Gygi. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // for pow() #include namespace boost { namespace accumulators { namespace impl { /////////////////////////////////////////////////////////////////////////////// // weighted_moment_impl template struct weighted_moment_impl : accumulator_base // TODO: also depends_on sum of powers { BOOST_MPL_ASSERT_RELATION(N::value, >, 0); typedef typename numeric::functional::multiplies::result_type weighted_sample; // for boost::result_of typedef typename numeric::functional::fdiv::result_type result_type; template weighted_moment_impl(Args const &args) : sum(args[sample | Sample()] * numeric::one::value) { } template void operator ()(Args const &args) { this->sum += args[weight] * numeric::pow(args[sample], N()); } template result_type result(Args const &args) const { return numeric::fdiv(this->sum, sum_of_weights(args)); } // make this accumulator serializeable template void serialize(Archive & ar, const unsigned int file_version) { ar & sum; } private: weighted_sample sum; }; } // namespace impl /////////////////////////////////////////////////////////////////////////////// // tag::weighted_moment // namespace tag { template struct weighted_moment : depends_on { /// INTERNAL ONLY /// typedef accumulators::impl::weighted_moment_impl, mpl::_1, mpl::_2> impl; }; } /////////////////////////////////////////////////////////////////////////////// // extract::weighted_moment // namespace extract { BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, weighted_moment, (int)) } using extract::weighted_moment; }} // namespace boost::accumulators #endif