weighted_sum_kahan.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // weighted_sum_kahan.hpp
  3. //
  4. // Copyright 2011 Simon West. 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_SUM_KAHAN_HPP_EAN_11_05_2011
  8. #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SUM_KAHAN_HPP_EAN_11_05_2011
  9. #include <boost/mpl/placeholders.hpp>
  10. #include <boost/accumulators/framework/accumulator_base.hpp>
  11. #include <boost/accumulators/framework/extractor.hpp>
  12. #include <boost/accumulators/numeric/functional.hpp>
  13. #include <boost/accumulators/framework/parameters/sample.hpp>
  14. #include <boost/accumulators/framework/parameters/weight.hpp>
  15. #include <boost/accumulators/framework/accumulators/external_accumulator.hpp>
  16. #include <boost/accumulators/framework/depends_on.hpp>
  17. #include <boost/accumulators/statistics_fwd.hpp>
  18. #include <boost/accumulators/statistics/weighted_sum.hpp>
  19. #include <boost/numeric/conversion/cast.hpp>
  20. namespace boost { namespace accumulators
  21. {
  22. namespace impl
  23. {
  24. #if _MSC_VER > 1400
  25. # pragma float_control(push)
  26. # pragma float_control(precise, on)
  27. #endif
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // weighted_sum_kahan_impl
  30. template<typename Sample, typename Weight, typename Tag>
  31. struct weighted_sum_kahan_impl
  32. : accumulator_base
  33. {
  34. typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
  35. // for boost::result_of
  36. typedef weighted_sample result_type;
  37. template<typename Args>
  38. weighted_sum_kahan_impl(Args const &args)
  39. : weighted_sum_(
  40. args[parameter::keyword<Tag>::get() | Sample()] * numeric::one<Weight>::value),
  41. compensation(boost::numeric_cast<weighted_sample>(0.0))
  42. {
  43. }
  44. template<typename Args>
  45. void
  46. #if BOOST_ACCUMULATORS_GCC_VERSION > 40305
  47. __attribute__((__optimize__("no-associative-math")))
  48. #endif
  49. operator ()(Args const &args)
  50. {
  51. const weighted_sample myTmp1 = args[parameter::keyword<Tag>::get()] * args[weight] - this->compensation;
  52. const weighted_sample myTmp2 = this->weighted_sum_ + myTmp1;
  53. this->compensation = (myTmp2 - this->weighted_sum_) - myTmp1;
  54. this->weighted_sum_ = myTmp2;
  55. }
  56. result_type result(dont_care) const
  57. {
  58. return this->weighted_sum_;
  59. }
  60. // make this accumulator serializeable
  61. template<class Archive>
  62. void serialize(Archive & ar, const unsigned int file_version)
  63. {
  64. ar & weighted_sum_;
  65. ar & compensation;
  66. }
  67. private:
  68. weighted_sample weighted_sum_;
  69. weighted_sample compensation;
  70. };
  71. #if _MSC_VER > 1400
  72. # pragma float_control(pop)
  73. #endif
  74. } // namespace impl
  75. ///////////////////////////////////////////////////////////////////////////////
  76. // tag::weighted_sum_kahan
  77. // tag::weighted_sum_of_variates_kahan
  78. //
  79. namespace tag
  80. {
  81. struct weighted_sum_kahan
  82. : depends_on<>
  83. {
  84. /// INTERNAL ONLY
  85. ///
  86. typedef accumulators::impl::weighted_sum_kahan_impl<mpl::_1, mpl::_2, tag::sample> impl;
  87. };
  88. template<typename VariateType, typename VariateTag>
  89. struct weighted_sum_of_variates_kahan
  90. : depends_on<>
  91. {
  92. /// INTERNAL ONLY
  93. ///
  94. typedef accumulators::impl::weighted_sum_kahan_impl<VariateType, mpl::_2, VariateTag> impl;
  95. };
  96. }
  97. ///////////////////////////////////////////////////////////////////////////////
  98. // extract::weighted_sum_kahan
  99. // extract::weighted_sum_of_variates_kahan
  100. //
  101. namespace extract
  102. {
  103. extractor<tag::weighted_sum_kahan> const weighted_sum_kahan = {};
  104. extractor<tag::abstract_weighted_sum_of_variates> const weighted_sum_of_variates_kahan = {};
  105. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_kahan)
  106. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_sum_of_variates_kahan)
  107. }
  108. using extract::weighted_sum_kahan;
  109. using extract::weighted_sum_of_variates_kahan;
  110. // weighted_sum(kahan) -> weighted_sum_kahan
  111. template<>
  112. struct as_feature<tag::weighted_sum(kahan)>
  113. {
  114. typedef tag::weighted_sum_kahan type;
  115. };
  116. template<typename VariateType, typename VariateTag>
  117. struct feature_of<tag::weighted_sum_of_variates_kahan<VariateType, VariateTag> >
  118. : feature_of<tag::abstract_weighted_sum_of_variates>
  119. {
  120. };
  121. }} // namespace boost::accumulators
  122. #endif