weighted_sum.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <typename RealType>
  16. class weighted_sum {
  17. public:
  18. weighted_sum() = default;
  19. explicit weighted_sum(const RealType& value) noexcept
  20. : sum_of_weights_(value), sum_of_weights_squared_(value) {}
  21. weighted_sum(const RealType& value, const RealType& variance) noexcept
  22. : sum_of_weights_(value), sum_of_weights_squared_(variance) {}
  23. /// Increment by one.
  24. weighted_sum& operator++() { return operator+=(1); }
  25. /// Increment by value.
  26. template <typename T>
  27. weighted_sum& operator+=(const T& value) {
  28. sum_of_weights_ += value;
  29. sum_of_weights_squared_ += value * value;
  30. return *this;
  31. }
  32. /// Added another weighted sum.
  33. template <typename T>
  34. weighted_sum& operator+=(const weighted_sum<T>& rhs) {
  35. sum_of_weights_ += static_cast<RealType>(rhs.sum_of_weights_);
  36. sum_of_weights_squared_ += static_cast<RealType>(rhs.sum_of_weights_squared_);
  37. return *this;
  38. }
  39. /// Scale by value.
  40. weighted_sum& operator*=(const RealType& x) {
  41. sum_of_weights_ *= x;
  42. sum_of_weights_squared_ *= x * x;
  43. return *this;
  44. }
  45. bool operator==(const RealType& rhs) const noexcept {
  46. return sum_of_weights_ == rhs && sum_of_weights_squared_ == rhs;
  47. }
  48. template <typename T>
  49. bool operator==(const weighted_sum<T>& rhs) const noexcept {
  50. return sum_of_weights_ == rhs.sum_of_weights_ &&
  51. sum_of_weights_squared_ == rhs.sum_of_weights_squared_;
  52. }
  53. template <typename T>
  54. bool operator!=(const T& rhs) const noexcept {
  55. return !operator==(rhs);
  56. }
  57. /// Return value of the sum.
  58. const RealType& value() const noexcept { return sum_of_weights_; }
  59. /// Return estimated variance of the sum.
  60. const RealType& variance() const noexcept { return sum_of_weights_squared_; }
  61. // lossy conversion must be explicit
  62. template <class T>
  63. explicit operator T() const {
  64. return static_cast<T>(sum_of_weights_);
  65. }
  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. RealType sum_of_weights_ = RealType();
  73. RealType sum_of_weights_squared_ = RealType();
  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