1 ///////////////////////////////////////////////////////////////////////////////
4 // Copyright 2005 Eric Niebler. 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_MOMENT_HPP_EAN_15_11_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_MOMENT_HPP_EAN_15_11_2005
11 #include <boost/config/no_tr1/cmath.hpp>
12 #include <boost/mpl/int.hpp>
13 #include <boost/mpl/assert.hpp>
14 #include <boost/mpl/placeholders.hpp>
15 #include <boost/accumulators/framework/accumulator_base.hpp>
16 #include <boost/accumulators/framework/extractor.hpp>
17 #include <boost/accumulators/numeric/functional.hpp>
18 #include <boost/accumulators/framework/parameters/sample.hpp>
19 #include <boost/accumulators/framework/depends_on.hpp>
20 #include <boost/accumulators/statistics_fwd.hpp>
21 #include <boost/accumulators/statistics/count.hpp>
23 namespace boost { namespace numeric
28 T const &pow(T const &x, mpl::int_<1>)
35 template<typename T, int N>
36 T pow(T const &x, mpl::int_<N>)
38 using namespace operators;
39 T y = numeric::pow(x, mpl::int_<N/2>());
41 return (N % 2) ? (z * x) : z;
45 namespace boost { namespace accumulators
50 ///////////////////////////////////////////////////////////////////////////////
52 template<typename N, typename Sample>
54 : accumulator_base // TODO: also depends_on sum of powers
56 BOOST_MPL_ASSERT_RELATION(N::value, >, 0);
57 // for boost::result_of
58 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
60 template<typename Args>
61 moment_impl(Args const &args)
62 : sum(args[sample | Sample()])
66 template<typename Args>
67 void operator ()(Args const &args)
69 this->sum += numeric::pow(args[sample], N());
72 template<typename Args>
73 result_type result(Args const &args) const
75 return numeric::fdiv(this->sum, count(args));
78 // make this accumulator serializeable
79 template<class Archive>
80 void serialize(Archive & ar, const unsigned int file_version)
91 ///////////////////////////////////////////////////////////////////////////////
102 typedef accumulators::impl::moment_impl<mpl::int_<N>, mpl::_1> impl;
106 ///////////////////////////////////////////////////////////////////////////////
111 BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, moment, (int))
114 using extract::moment;
116 // So that moment<N> can be automatically substituted with
117 // weighted_moment<N> when the weight parameter is non-void
119 struct as_weighted_feature<tag::moment<N> >
121 typedef tag::weighted_moment<N> type;
125 struct feature_of<tag::weighted_moment<N> >
126 : feature_of<tag::moment<N> >
130 }} // namespace boost::accumulators