weighted_sum.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/histogram/fwd.hpp>
  9. #include <type_traits>
  10. namespace boost {
  11. namespace histogram {
  12. namespace accumulators {
  13. /// Holds sum of weights and its variance estimate
  14. template <typename RealType>
  15. class weighted_sum {
  16. public:
  17. weighted_sum() = default;
  18. explicit weighted_sum(const RealType& value) noexcept
  19. : sum_of_weights_(value), sum_of_weights_squared_(value) {}
  20. weighted_sum(const RealType& value, const RealType& variance) noexcept
  21. : sum_of_weights_(value), sum_of_weights_squared_(variance) {}
  22. /// Increment by one.
  23. weighted_sum& operator++() { return operator+=(1); }
  24. /// Increment by value.
  25. template <typename T>
  26. weighted_sum& operator+=(const T& value) {
  27. sum_of_weights_ += value;
  28. sum_of_weights_squared_ += value * value;
  29. return *this;
  30. }
  31. /// Added another weighted sum.
  32. template <typename T>
  33. weighted_sum& operator+=(const weighted_sum<T>& rhs) {
  34. sum_of_weights_ += static_cast<RealType>(rhs.sum_of_weights_);
  35. sum_of_weights_squared_ += static_cast<RealType>(rhs.sum_of_weights_squared_);
  36. return *this;
  37. }
  38. /// Scale by value.
  39. weighted_sum& operator*=(const RealType& x) {
  40. sum_of_weights_ *= x;
  41. sum_of_weights_squared_ *= x * x;
  42. return *this;
  43. }
  44. bool operator==(const RealType& rhs) const noexcept {
  45. return sum_of_weights_ == rhs && sum_of_weights_squared_ == rhs;
  46. }
  47. template <typename T>
  48. bool operator==(const weighted_sum<T>& rhs) const noexcept {
  49. return sum_of_weights_ == rhs.sum_of_weights_ &&
  50. sum_of_weights_squared_ == rhs.sum_of_weights_squared_;
  51. }
  52. template <typename T>
  53. bool operator!=(const T& rhs) const noexcept {
  54. return !operator==(rhs);
  55. }
  56. /// Return value of the sum.
  57. const RealType& value() const noexcept { return sum_of_weights_; }
  58. /// Return estimated variance of the sum.
  59. const RealType& variance() const noexcept { return sum_of_weights_squared_; }
  60. // lossy conversion must be explicit
  61. template <class T>
  62. explicit operator T() const {
  63. return static_cast<T>(sum_of_weights_);
  64. }
  65. template <class Archive>
  66. void serialize(Archive&, unsigned /* version */);
  67. private:
  68. RealType sum_of_weights_ = RealType();
  69. RealType sum_of_weights_squared_ = RealType();
  70. };
  71. } // namespace accumulators
  72. } // namespace histogram
  73. } // namespace boost
  74. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  75. namespace std {
  76. template <class T, class U>
  77. struct common_type<boost::histogram::accumulators::weighted_sum<T>,
  78. boost::histogram::accumulators::weighted_sum<U>> {
  79. using type = boost::histogram::accumulators::weighted_sum<common_type_t<T, U>>;
  80. };
  81. template <class T, class U>
  82. struct common_type<boost::histogram::accumulators::weighted_sum<T>, 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<T, boost::histogram::accumulators::weighted_sum<U>> {
  87. using type = boost::histogram::accumulators::weighted_sum<common_type_t<T, U>>;
  88. };
  89. } // namespace std
  90. #endif
  91. #endif