/Src/Dependencies/Boost/boost/accumulators/statistics/weighted_moment.hpp

http://hadesmem.googlecode.com/ · C++ Header · 96 lines · 65 code · 14 blank · 17 comment · 0 complexity · 8a5396f3ca03e6a1c122f08b29bbf66c MD5 · raw file

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