annotate DEPENDENCIES/generic/include/boost/accumulators/statistics/moment.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 ///////////////////////////////////////////////////////////////////////////////
Chris@16 2 // moment.hpp
Chris@16 3 //
Chris@16 4 // Copyright 2005 Eric Niebler. Distributed under the Boost
Chris@16 5 // Software License, Version 1.0. (See accompanying file
Chris@16 6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7
Chris@16 8 #ifndef BOOST_ACCUMULATORS_STATISTICS_MOMENT_HPP_EAN_15_11_2005
Chris@16 9 #define BOOST_ACCUMULATORS_STATISTICS_MOMENT_HPP_EAN_15_11_2005
Chris@16 10
Chris@16 11 #include <boost/config/no_tr1/cmath.hpp>
Chris@16 12 #include <boost/mpl/int.hpp>
Chris@16 13 #include <boost/mpl/assert.hpp>
Chris@16 14 #include <boost/mpl/placeholders.hpp>
Chris@16 15 #include <boost/accumulators/framework/accumulator_base.hpp>
Chris@16 16 #include <boost/accumulators/framework/extractor.hpp>
Chris@16 17 #include <boost/accumulators/numeric/functional.hpp>
Chris@16 18 #include <boost/accumulators/framework/parameters/sample.hpp>
Chris@16 19 #include <boost/accumulators/framework/depends_on.hpp>
Chris@16 20 #include <boost/accumulators/statistics_fwd.hpp>
Chris@16 21 #include <boost/accumulators/statistics/count.hpp>
Chris@16 22
Chris@16 23 namespace boost { namespace numeric
Chris@16 24 {
Chris@16 25 /// INTERNAL ONLY
Chris@16 26 ///
Chris@16 27 template<typename T>
Chris@16 28 T const &pow(T const &x, mpl::int_<1>)
Chris@16 29 {
Chris@16 30 return x;
Chris@16 31 }
Chris@16 32
Chris@16 33 /// INTERNAL ONLY
Chris@16 34 ///
Chris@16 35 template<typename T, int N>
Chris@16 36 T pow(T const &x, mpl::int_<N>)
Chris@16 37 {
Chris@16 38 using namespace operators;
Chris@16 39 T y = numeric::pow(x, mpl::int_<N/2>());
Chris@16 40 T z = y * y;
Chris@16 41 return (N % 2) ? (z * x) : z;
Chris@16 42 }
Chris@16 43 }}
Chris@16 44
Chris@16 45 namespace boost { namespace accumulators
Chris@16 46 {
Chris@16 47
Chris@16 48 namespace impl
Chris@16 49 {
Chris@16 50 ///////////////////////////////////////////////////////////////////////////////
Chris@16 51 // moment_impl
Chris@16 52 template<typename N, typename Sample>
Chris@16 53 struct moment_impl
Chris@16 54 : accumulator_base // TODO: also depends_on sum of powers
Chris@16 55 {
Chris@16 56 BOOST_MPL_ASSERT_RELATION(N::value, >, 0);
Chris@16 57 // for boost::result_of
Chris@16 58 typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
Chris@16 59
Chris@16 60 template<typename Args>
Chris@16 61 moment_impl(Args const &args)
Chris@16 62 : sum(args[sample | Sample()])
Chris@16 63 {
Chris@16 64 }
Chris@16 65
Chris@16 66 template<typename Args>
Chris@16 67 void operator ()(Args const &args)
Chris@16 68 {
Chris@16 69 this->sum += numeric::pow(args[sample], N());
Chris@16 70 }
Chris@16 71
Chris@16 72 template<typename Args>
Chris@16 73 result_type result(Args const &args) const
Chris@16 74 {
Chris@16 75 return numeric::fdiv(this->sum, count(args));
Chris@16 76 }
Chris@16 77
Chris@16 78 private:
Chris@16 79 Sample sum;
Chris@16 80 };
Chris@16 81
Chris@16 82 } // namespace impl
Chris@16 83
Chris@16 84 ///////////////////////////////////////////////////////////////////////////////
Chris@16 85 // tag::moment
Chris@16 86 //
Chris@16 87 namespace tag
Chris@16 88 {
Chris@16 89 template<int N>
Chris@16 90 struct moment
Chris@16 91 : depends_on<count>
Chris@16 92 {
Chris@16 93 /// INTERNAL ONLY
Chris@16 94 ///
Chris@16 95 typedef accumulators::impl::moment_impl<mpl::int_<N>, mpl::_1> impl;
Chris@16 96 };
Chris@16 97 }
Chris@16 98
Chris@16 99 ///////////////////////////////////////////////////////////////////////////////
Chris@16 100 // extract::moment
Chris@16 101 //
Chris@16 102 namespace extract
Chris@16 103 {
Chris@16 104 BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, moment, (int))
Chris@16 105 }
Chris@16 106
Chris@16 107 using extract::moment;
Chris@16 108
Chris@16 109 // So that moment<N> can be automatically substituted with
Chris@16 110 // weighted_moment<N> when the weight parameter is non-void
Chris@16 111 template<int N>
Chris@16 112 struct as_weighted_feature<tag::moment<N> >
Chris@16 113 {
Chris@16 114 typedef tag::weighted_moment<N> type;
Chris@16 115 };
Chris@16 116
Chris@16 117 template<int N>
Chris@16 118 struct feature_of<tag::weighted_moment<N> >
Chris@16 119 : feature_of<tag::moment<N> >
Chris@16 120 {
Chris@16 121 };
Chris@16 122
Chris@16 123 }} // namespace boost::accumulators
Chris@16 124
Chris@16 125 #endif