sum_kahan.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // sum_kahan.hpp
  3. //
  4. // Copyright 2010 Gaetano Mendola, 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_SUM_KAHAN_HPP_EAN_26_07_2010
  8. #define BOOST_ACCUMULATORS_STATISTICS_SUM_KAHAN_HPP_EAN_26_07_2010
  9. #include <boost/accumulators/framework/accumulator_base.hpp>
  10. #include <boost/accumulators/framework/parameters/sample.hpp>
  11. #include <boost/accumulators/statistics_fwd.hpp>
  12. #include <boost/accumulators/statistics/sum.hpp>
  13. #include <boost/accumulators/statistics/weighted_sum_kahan.hpp>
  14. #include <boost/numeric/conversion/cast.hpp>
  15. namespace boost { namespace accumulators
  16. {
  17. namespace impl
  18. {
  19. #if _MSC_VER > 1400
  20. # pragma float_control(push)
  21. # pragma float_control(precise, on)
  22. #endif
  23. template<typename Sample, typename Tag>
  24. struct sum_kahan_impl
  25. : accumulator_base
  26. {
  27. typedef Sample result_type;
  28. ////////////////////////////////////////////////////////////////////////////
  29. // sum_kahan_impl
  30. /**
  31. @brief Kahan summation algorithm
  32. The Kahan summation algorithm reduces the numerical error obtained with standard
  33. sequential sum.
  34. */
  35. template<typename Args>
  36. sum_kahan_impl(Args const & args)
  37. : sum(args[parameter::keyword<Tag>::get() | Sample()]),
  38. compensation(boost::numeric_cast<Sample>(0.0))
  39. {
  40. }
  41. template<typename Args>
  42. void
  43. #if BOOST_ACCUMULATORS_GCC_VERSION > 40305
  44. __attribute__((__optimize__("no-associative-math")))
  45. #endif
  46. operator ()(Args const & args)
  47. {
  48. const Sample myTmp1 = args[parameter::keyword<Tag>::get()] - this->compensation;
  49. const Sample myTmp2 = this->sum + myTmp1;
  50. this->compensation = (myTmp2 - this->sum) - myTmp1;
  51. this->sum = myTmp2;
  52. }
  53. result_type result(dont_care) const
  54. {
  55. return this->sum;
  56. }
  57. private:
  58. Sample sum;
  59. Sample compensation;
  60. };
  61. #if _MSC_VER > 1400
  62. # pragma float_control(pop)
  63. #endif
  64. } // namespace impl
  65. ///////////////////////////////////////////////////////////////////////////////
  66. // tag::sum_kahan
  67. // tag::sum_of_weights_kahan
  68. // tag::sum_of_variates_kahan
  69. //
  70. namespace tag
  71. {
  72. struct sum_kahan
  73. : depends_on<>
  74. {
  75. /// INTERNAL ONLY
  76. ///
  77. typedef impl::sum_kahan_impl< mpl::_1, tag::sample > impl;
  78. };
  79. struct sum_of_weights_kahan
  80. : depends_on<>
  81. {
  82. typedef mpl::true_ is_weight_accumulator;
  83. /// INTERNAL ONLY
  84. ///
  85. typedef accumulators::impl::sum_kahan_impl<mpl::_2, tag::weight> impl;
  86. };
  87. template<typename VariateType, typename VariateTag>
  88. struct sum_of_variates_kahan
  89. : depends_on<>
  90. {
  91. /// INTERNAL ONLY
  92. ///
  93. typedef mpl::always<accumulators::impl::sum_kahan_impl<VariateType, VariateTag> > impl;
  94. };
  95. } // namespace tag
  96. ///////////////////////////////////////////////////////////////////////////////
  97. // extract::sum_kahan
  98. // extract::sum_of_weights_kahan
  99. // extract::sum_of_variates_kahan
  100. //
  101. namespace extract
  102. {
  103. extractor<tag::sum_kahan> const sum_kahan = {};
  104. extractor<tag::sum_of_weights_kahan> const sum_of_weights_kahan = {};
  105. extractor<tag::abstract_sum_of_variates> const sum_of_variates_kahan = {};
  106. BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_kahan)
  107. BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_weights_kahan)
  108. BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_variates_kahan)
  109. } // namespace extract
  110. using extract::sum_kahan;
  111. using extract::sum_of_weights_kahan;
  112. using extract::sum_of_variates_kahan;
  113. // sum(kahan) -> sum_kahan
  114. template<>
  115. struct as_feature<tag::sum(kahan)>
  116. {
  117. typedef tag::sum_kahan type;
  118. };
  119. // sum_of_weights(kahan) -> sum_of_weights_kahan
  120. template<>
  121. struct as_feature<tag::sum_of_weights(kahan)>
  122. {
  123. typedef tag::sum_of_weights_kahan type;
  124. };
  125. // So that sum_kahan can be automatically substituted with
  126. // weighted_sum_kahan when the weight parameter is non-void.
  127. template<>
  128. struct as_weighted_feature<tag::sum_kahan>
  129. {
  130. typedef tag::weighted_sum_kahan type;
  131. };
  132. template<>
  133. struct feature_of<tag::weighted_sum_kahan>
  134. : feature_of<tag::sum>
  135. {};
  136. // for the purposes of feature-based dependency resolution,
  137. // sum_kahan provides the same feature as sum
  138. template<>
  139. struct feature_of<tag::sum_kahan>
  140. : feature_of<tag::sum>
  141. {
  142. };
  143. // for the purposes of feature-based dependency resolution,
  144. // sum_of_weights_kahan provides the same feature as sum_of_weights
  145. template<>
  146. struct feature_of<tag::sum_of_weights_kahan>
  147. : feature_of<tag::sum_of_weights>
  148. {
  149. };
  150. template<typename VariateType, typename VariateTag>
  151. struct feature_of<tag::sum_of_variates_kahan<VariateType, VariateTag> >
  152. : feature_of<tag::abstract_sum_of_variates>
  153. {
  154. };
  155. }} // namespace boost::accumulators
  156. #endif