]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // weighted_moment.hpp | |
3 | // | |
4 | // Copyright 2006, Eric Niebler, Olivier Gygi. 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) | |
7 | ||
8 | #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005 | |
9 | #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005 | |
10 | ||
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/preprocessor/arithmetic/inc.hpp> | |
16 | #include <boost/preprocessor/repetition/repeat_from_to.hpp> | |
17 | #include <boost/preprocessor/repetition/enum_trailing_params.hpp> | |
18 | #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp> | |
19 | #include <boost/accumulators/framework/accumulator_base.hpp> | |
20 | #include <boost/accumulators/framework/extractor.hpp> | |
21 | #include <boost/accumulators/numeric/functional.hpp> | |
22 | #include <boost/accumulators/framework/parameters/sample.hpp> | |
23 | #include <boost/accumulators/framework/depends_on.hpp> | |
24 | #include <boost/accumulators/statistics_fwd.hpp> | |
25 | #include <boost/accumulators/statistics/count.hpp> | |
26 | #include <boost/accumulators/statistics/moment.hpp> // for pow() | |
27 | #include <boost/accumulators/statistics/sum.hpp> | |
28 | ||
29 | namespace boost { namespace accumulators | |
30 | { | |
31 | ||
32 | namespace impl | |
33 | { | |
34 | /////////////////////////////////////////////////////////////////////////////// | |
35 | // weighted_moment_impl | |
36 | template<typename N, typename Sample, typename Weight> | |
37 | struct weighted_moment_impl | |
38 | : accumulator_base // TODO: also depends_on sum of powers | |
39 | { | |
40 | BOOST_MPL_ASSERT_RELATION(N::value, >, 0); | |
41 | typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample; | |
42 | // for boost::result_of | |
43 | typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type; | |
44 | ||
45 | template<typename Args> | |
46 | weighted_moment_impl(Args const &args) | |
47 | : sum(args[sample | Sample()] * numeric::one<Weight>::value) | |
48 | { | |
49 | } | |
50 | ||
51 | template<typename Args> | |
52 | void operator ()(Args const &args) | |
53 | { | |
54 | this->sum += args[weight] * numeric::pow(args[sample], N()); | |
55 | } | |
56 | ||
57 | template<typename Args> | |
58 | result_type result(Args const &args) const | |
59 | { | |
60 | return numeric::fdiv(this->sum, sum_of_weights(args)); | |
61 | } | |
62 | ||
63 | private: | |
64 | weighted_sample sum; | |
65 | }; | |
66 | ||
67 | } // namespace impl | |
68 | ||
69 | /////////////////////////////////////////////////////////////////////////////// | |
70 | // tag::weighted_moment | |
71 | // | |
72 | namespace tag | |
73 | { | |
74 | template<int N> | |
75 | struct weighted_moment | |
76 | : depends_on<count, sum_of_weights> | |
77 | { | |
78 | /// INTERNAL ONLY | |
79 | /// | |
80 | typedef accumulators::impl::weighted_moment_impl<mpl::int_<N>, mpl::_1, mpl::_2> impl; | |
81 | }; | |
82 | } | |
83 | ||
84 | /////////////////////////////////////////////////////////////////////////////// | |
85 | // extract::weighted_moment | |
86 | // | |
87 | namespace extract | |
88 | { | |
89 | BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, weighted_moment, (int)) | |
90 | } | |
91 | ||
92 | using extract::weighted_moment; | |
93 | ||
94 | }} // namespace boost::accumulators | |
95 | ||
96 | #endif |