weighted_sum.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. template <class T>
  38. weighted_sum& operator+=(const weight_type<T>& w) {
  39. sum_of_weights_ += w.value;
  40. sum_of_weights_squared_ += w.value * w.value;
  41. return *this;
  42. }
  43. /// Added another weighted sum.
  44. weighted_sum& operator+=(const weighted_sum& rhs) {
  45. sum_of_weights_ += rhs.sum_of_weights_;
  46. sum_of_weights_squared_ += rhs.sum_of_weights_squared_;
  47. return *this;
  48. }
  49. /// Scale by value.
  50. weighted_sum& operator*=(const_reference x) {
  51. sum_of_weights_ *= x;
  52. sum_of_weights_squared_ *= x * x;
  53. return *this;
  54. }
  55. bool operator==(const weighted_sum& rhs) const noexcept {
  56. return sum_of_weights_ == rhs.sum_of_weights_ &&
  57. sum_of_weights_squared_ == rhs.sum_of_weights_squared_;
  58. }
  59. bool operator!=(const weighted_sum& rhs) const noexcept { return !operator==(rhs); }
  60. /// Return value of the sum.
  61. const_reference value() const noexcept { return sum_of_weights_; }
  62. /// Return estimated variance of the sum.
  63. const_reference variance() const noexcept { return sum_of_weights_squared_; }
  64. // lossy conversion must be explicit
  65. explicit operator const_reference() const { return sum_of_weights_; }
  66. template <class Archive>
  67. void serialize(Archive& ar, unsigned /* version */) {
  68. ar& make_nvp("sum_of_weights", sum_of_weights_);
  69. ar& make_nvp("sum_of_weights_squared", sum_of_weights_squared_);
  70. }
  71. private:
  72. value_type sum_of_weights_{};
  73. value_type sum_of_weights_squared_{};
  74. };
  75. } // namespace accumulators
  76. } // namespace histogram
  77. } // namespace boost
  78. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  79. namespace std {
  80. template <class T, class U>
  81. struct common_type<boost::histogram::accumulators::weighted_sum<T>,
  82. boost::histogram::accumulators::weighted_sum<U>> {
  83. using type = boost::histogram::accumulators::weighted_sum<common_type_t<T, U>>;
  84. };
  85. template <class T, class U>
  86. struct common_type<boost::histogram::accumulators::weighted_sum<T>, U> {
  87. using type = boost::histogram::accumulators::weighted_sum<common_type_t<T, U>>;
  88. };
  89. template <class T, class U>
  90. struct common_type<T, boost::histogram::accumulators::weighted_sum<U>> {
  91. using type = boost::histogram::accumulators::weighted_sum<common_type_t<T, U>>;
  92. };
  93. } // namespace std
  94. #endif
  95. #endif