weighted_sum.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015-2018 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_HISTOGRAM_ACCUMULATORS_WEIGHTED_SUM_HPP
  7. #define BOOST_HISTOGRAM_ACCUMULATORS_WEIGHTED_SUM_HPP
  8. #include <boost/core/nvp.hpp>
  9. #include <boost/histogram/fwd.hpp> // for weighted_sum<>
  10. #include <type_traits>
  11. namespace boost {
  12. namespace histogram {
  13. namespace accumulators {
  14. /// Holds sum of weights and its variance estimate
  15. template <class ValueType>
  16. class weighted_sum {
  17. public:
  18. using value_type = ValueType;
  19. using const_reference = const value_type&;
  20. weighted_sum() = default;
  21. /// Initialize sum to value and allow implicit conversion
  22. weighted_sum(const_reference value) noexcept : weighted_sum(value, value) {}
  23. /// Allow implicit conversion from sum<T>
  24. template <class T>
  25. weighted_sum(const weighted_sum<T>& s) noexcept
  26. : weighted_sum(s.value(), s.variance()) {}
  27. /// Initialize sum to value and variance
  28. weighted_sum(const_reference value, const_reference variance) noexcept
  29. : sum_of_weights_(value), sum_of_weights_squared_(variance) {}
  30. /// Increment by one.
  31. weighted_sum& operator++() {
  32. ++sum_of_weights_;
  33. ++sum_of_weights_squared_;
  34. return *this;
  35. }
  36. /// Increment by weight.
  37. weighted_sum& operator+=(const weight_type<value_type>& w) {
  38. sum_of_weights_ += w.value;
  39. sum_of_weights_squared_ += w.value * w.value;
  40. return *this;
  41. }
  42. /// Added another weighted sum.
  43. weighted_sum& operator+=(const weighted_sum& rhs) {
  44. sum_of_weights_ += rhs.sum_of_weights_;
  45. sum_of_weights_squared_ += rhs.sum_of_weights_squared_;
  46. return *this;
  47. }
  48. /// Scale by value.
  49. weighted_sum& operator*=(const_reference x) {
  50. sum_of_weights_ *= x;
  51. sum_of_weights_squared_ *= x * x;
  52. return *this;
  53. }
  54. bool operator==(const weighted_sum& rhs) const noexcept {
  55. return sum_of_weights_ == rhs.sum_of_weights_ &&
  56. sum_of_weights_squared_ == rhs.sum_of_weights_squared_;
  57. }
  58. bool operator!=(const weighted_sum& rhs) const noexcept { return !operator==(rhs); }
  59. /// Return value of the sum.
  60. const_reference value() const noexcept { return sum_of_weights_; }
  61. /// Return estimated variance of the sum.
  62. const_reference variance() const noexcept { return sum_of_weights_squared_; }
  63. // lossy conversion must be explicit
  64. explicit operator const_reference() const { return sum_of_weights_; }
  65. template <class Archive>
  66. void serialize(Archive& ar, unsigned /* version */) {
  67. ar& make_nvp("sum_of_weights", sum_of_weights_);
  68. ar& make_nvp("sum_of_weights_squared", sum_of_weights_squared_);
  69. }
  70. private:
  71. value_type sum_of_weights_{};
  72. value_type sum_of_weights_squared_{};
  73. };
  74. } // namespace accumulators
  75. } // namespace histogram
  76. } // namespace boost
  77. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  78. namespace std {
  79. template <class T, class U>
  80. struct common_type<boost::histogram::accumulators::weighted_sum<T>,
  81. boost::histogram::accumulators::weighted_sum<U>> {
  82. using type = boost::histogram::accumulators::weighted_sum<common_type_t<T, U>>;
  83. };
  84. template <class T, class U>
  85. struct common_type<boost::histogram::accumulators::weighted_sum<T>, U> {
  86. using type = boost::histogram::accumulators::weighted_sum<common_type_t<T, U>>;
  87. };
  88. template <class T, class U>
  89. struct common_type<T, boost::histogram::accumulators::weighted_sum<U>> {
  90. using type = boost::histogram::accumulators::weighted_sum<common_type_t<T, U>>;
  91. };
  92. } // namespace std
  93. #endif
  94. #endif