/////////////////////////////////////////////////////////////////////////////// // moment.hpp // // Copyright 2005 Eric Niebler. 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_MOMENT_HPP_EAN_15_11_2005 #define BOOST_ACCUMULATORS_STATISTICS_MOMENT_HPP_EAN_15_11_2005 #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace numeric { /// INTERNAL ONLY /// template T const &pow(T const &x, mpl::int_<1>) { return x; } /// INTERNAL ONLY /// template T pow(T const &x, mpl::int_) { using namespace operators; T y = numeric::pow(x, mpl::int_()); T z = y * y; return (N % 2) ? (z * x) : z; } }} namespace boost { namespace accumulators { namespace impl { /////////////////////////////////////////////////////////////////////////////// // moment_impl template struct moment_impl : accumulator_base // TODO: also depends_on sum of powers { BOOST_MPL_ASSERT_RELATION(N::value, >, 0); // for boost::result_of typedef typename numeric::functional::average::result_type result_type; template moment_impl(Args const &args) : sum(args[sample | Sample()]) { } template void operator ()(Args const &args) { this->sum += numeric::pow(args[sample], N()); } template result_type result(Args const &args) const { return numeric::average(this->sum, count(args)); } private: Sample sum; }; } // namespace impl /////////////////////////////////////////////////////////////////////////////// // tag::moment // namespace tag { template struct moment : depends_on { /// INTERNAL ONLY /// typedef accumulators::impl::moment_impl, mpl::_1> impl; }; } /////////////////////////////////////////////////////////////////////////////// // extract::moment // namespace extract { BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, moment, (int)) } using extract::moment; // So that moment can be automatically substituted with // weighted_moment when the weight parameter is non-void template struct as_weighted_feature > { typedef tag::weighted_moment type; }; template struct feature_of > : feature_of > { }; }} // namespace boost::accumulators #endif