sum_kahan.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // make this accumulator serializeable
  58. template<class Archive>
  59. void serialize(Archive & ar, const unsigned int file_version)
  60. {
  61. ar & sum;
  62. ar & compensation;
  63. }
  64. private:
  65. Sample sum;
  66. Sample compensation;
  67. };
  68. #if _MSC_VER > 1400
  69. # pragma float_control(pop)
  70. #endif
  71. } // namespace impl
  72. ///////////////////////////////////////////////////////////////////////////////
  73. // tag::sum_kahan
  74. // tag::sum_of_weights_kahan
  75. // tag::sum_of_variates_kahan
  76. //
  77. namespace tag
  78. {
  79. struct sum_kahan
  80. : depends_on<>
  81. {
  82. /// INTERNAL ONLY
  83. ///
  84. typedef impl::sum_kahan_impl< mpl::_1, tag::sample > impl;
  85. };
  86. struct sum_of_weights_kahan
  87. : depends_on<>
  88. {
  89. typedef mpl::true_ is_weight_accumulator;
  90. /// INTERNAL ONLY
  91. ///
  92. typedef accumulators::impl::sum_kahan_impl<mpl::_2, tag::weight> impl;
  93. };
  94. template<typename VariateType, typename VariateTag>
  95. struct sum_of_variates_kahan
  96. : depends_on<>
  97. {
  98. /// INTERNAL ONLY
  99. ///
  100. typedef mpl::always<accumulators::impl::sum_kahan_impl<VariateType, VariateTag> > impl;
  101. };
  102. } // namespace tag
  103. ///////////////////////////////////////////////////////////////////////////////
  104. // extract::sum_kahan
  105. // extract::sum_of_weights_kahan
  106. // extract::sum_of_variates_kahan
  107. //
  108. namespace extract
  109. {
  110. extractor<tag::sum_kahan> const sum_kahan = {};
  111. extractor<tag::sum_of_weights_kahan> const sum_of_weights_kahan = {};
  112. extractor<tag::abstract_sum_of_variates> const sum_of_variates_kahan = {};
  113. BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_kahan)
  114. BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_weights_kahan)
  115. BOOST_ACCUMULATORS_IGNORE_GLOBAL(sum_of_variates_kahan)
  116. } // namespace extract
  117. using extract::sum_kahan;
  118. using extract::sum_of_weights_kahan;
  119. using extract::sum_of_variates_kahan;
  120. // sum(kahan) -> sum_kahan
  121. template<>
  122. struct as_feature<tag::sum(kahan)>
  123. {
  124. typedef tag::sum_kahan type;
  125. };
  126. // sum_of_weights(kahan) -> sum_of_weights_kahan
  127. template<>
  128. struct as_feature<tag::sum_of_weights(kahan)>
  129. {
  130. typedef tag::sum_of_weights_kahan type;
  131. };
  132. // So that sum_kahan can be automatically substituted with
  133. // weighted_sum_kahan when the weight parameter is non-void.
  134. template<>
  135. struct as_weighted_feature<tag::sum_kahan>
  136. {
  137. typedef tag::weighted_sum_kahan type;
  138. };
  139. template<>
  140. struct feature_of<tag::weighted_sum_kahan>
  141. : feature_of<tag::sum>
  142. {};
  143. // for the purposes of feature-based dependency resolution,
  144. // sum_kahan provides the same feature as sum
  145. template<>
  146. struct feature_of<tag::sum_kahan>
  147. : feature_of<tag::sum>
  148. {
  149. };
  150. // for the purposes of feature-based dependency resolution,
  151. // sum_of_weights_kahan provides the same feature as sum_of_weights
  152. template<>
  153. struct feature_of<tag::sum_of_weights_kahan>
  154. : feature_of<tag::sum_of_weights>
  155. {
  156. };
  157. template<typename VariateType, typename VariateTag>
  158. struct feature_of<tag::sum_of_variates_kahan<VariateType, VariateTag> >
  159. : feature_of<tag::abstract_sum_of_variates>
  160. {
  161. };
  162. }} // namespace boost::accumulators
  163. #endif