From f1b711d634fbb6b2f3bcaf4ec31d3cfd5fe2db1b Mon Sep 17 00:00:00 2001 From: Hans Dembinski Date: Thu, 1 Mar 2018 15:57:55 +0100 Subject: [PATCH] separate bin type properties from histogram interface, made interface more flexible, histogram_test is failing, needs investigation --- doc/getting_started.qbk | 18 +- doc/guide.qbk | 78 ++-- include/boost/histogram/detail/meta.hpp | 6 +- include/boost/histogram/detail/utility.hpp | 18 - include/boost/histogram/dynamic_histogram.hpp | 116 ++--- include/boost/histogram/histogram_fwd.hpp | 1 + include/boost/histogram/iterator.hpp | 47 +- include/boost/histogram/static_histogram.hpp | 76 ++-- .../histogram/storage/adaptive_storage.hpp | 94 ++-- .../boost/histogram/storage/array_storage.hpp | 61 +-- include/boost/histogram/storage/operators.hpp | 18 +- .../histogram/storage/weight_counter.hpp | 46 +- src/python/histogram.cpp | 45 +- test/adaptive_storage_test.cpp | 128 +++--- test/array_storage_test.cpp | 56 +-- test/histogram_test.cpp | 406 +++++++++--------- test/meta_test.cpp | 6 +- test/python_suite_test.py | 62 +-- test/weight_counter_test.cpp | 9 +- 19 files changed, 612 insertions(+), 679 deletions(-) diff --git a/doc/getting_started.qbk b/doc/getting_started.qbk index 1c202727..448e9f4e 100644 --- a/doc/getting_started.qbk +++ b/doc/getting_started.qbk @@ -49,22 +49,22 @@ int main(int, char**) { - `variance(index)` method returns a variance estimate of the bin count at index (see Rationale section for what this means) */ - for (const auto& bin : h.axis(0_c)) { - std::cout << "bin " << bin.first << " x in [" - << bin.second.lower() << ", " << bin.second.upper() << "): " - << h.value(bin.first) << " +/- " - << std::sqrt(h.variance(bin.first)) + for (const auto& a : h.axis(0_c)) { + std::cout << "bin " << a.first << " x in [" + << a.second.lower() << ", " << a.second.upper() << "): " + << h.bin(a.first).value() << " +/- " + << std::sqrt(h.bin(a.first).variance()) << std::endl; } // accessing under- and overflow bins is easy, use indices -1 and 10 std::cout << "underflow bin [" << h.axis(0_c)[-1].lower() << ", " << h.axis(0_c)[-1].upper() << "): " - << h.value(-1) << " +/- " << std::sqrt(h.variance(-1)) + << h.bin(-1).value() << " +/- " << std::sqrt(h.bin(-1).variance()) << std::endl; std::cout << "overflow bin [" << h.axis(0_c)[10].lower() << ", " << h.axis(0_c)[10].upper() << "): " - << h.value(10) << " +/- " << std::sqrt(h.variance(10)) + << h.bin(10).value() << " +/- " << std::sqrt(h.bin(10).variance()) << std::endl; /* program output: @@ -134,7 +134,7 @@ int main() { std::printf("%s\n", cbin.second.c_str()); for (const auto& ybin : h.axis(2)) { // rows for (const auto& xbin : h.axis(1)) { // columns - std::printf("%3.0f ", h.value(cbin.first, xbin.first, ybin.first)); + std::printf("%3.0f ", h.bin(cbin.first, xbin.first, ybin.first).value()); } std::printf("\n"); } @@ -225,7 +225,7 @@ for x in (2e0, 2e1, 2e2, 2e3, 2e4): # iterate over bins and access bin counter for idx, (lower, upper) in enumerate(h.axis(0)): print "bin {0} x in [{1}, {2}): {3} +/- {4}".format( - idx, lower, upper, h.value(idx), h.variance(idx) ** 0.5) + idx, lower, upper, h.bin(idx).value, h.bin(idx).variance ** 0.5) `` [endsect] diff --git a/doc/guide.qbk b/doc/guide.qbk index 8ceef4bf..8da35d5c 100644 --- a/doc/guide.qbk +++ b/doc/guide.qbk @@ -144,8 +144,8 @@ int main() { // fill histogram, number of arguments must be equal to number of axes h.fill(0, 4.1); // increases bin counter by one - h.fill(3, 3.4, bh::weight(1.5)); // increase bin counter by weight 1.5 - h.fill(bh::weight(1.5), 3, 3.4); // the same as the previous call + h.fill(3, 3.4, bh::weight(2)); // increase bin counter by 2 + h.fill(bh::weight(2), 3, 3.4); // the same as the previous call // a dynamic histogram also supports fills from an interator range while // a static histogram does not allow it; using a range of wrong length @@ -194,10 +194,9 @@ int main() { After the histogram has been filled, you want to access the bin counts at some point. You may want to visualize the counts, or compute some quantities like the mean from the data distribution approximated by the histogram. -To access the count of each bin, you use a multi-dimensional index, which consists of a sequence of bin indices for the axes in order. You can use this index to access the value for each and the variance estimate, using these methods: +To access each bin, you use a multi-dimensional index, which consists of a sequence of bin indices for the axes in order. You can use this index to access the value for each and the variance estimate, using these methods: -* `histogram::value(...)` takes `N` arguments, where `N` is equal to the number of axes of the histogram, and returns the count of the associated bin -* `histogram::variance(...)` does the same but returns a variance estimate for the bin, purpose and meaning of the variance is explained in [link histogram.rationale.variance the rationale] +* `histogram::bin(...)` takes `N` arguments, where `N` is equal to the number of axes of the histogram, and returns the associated bin type Both calls are demonstrated in the next example. @@ -209,27 +208,29 @@ namespace bh = boost::histogram; int main() { // make histogram with 2 x 2 = 4 bins (not counting under-/overflow bins) - auto h = bh::make_dynamic_histogram(bh::axis::regular<>(2, -1, 1), - bh::axis::regular<>(2, 2, 4)); + auto h = bh::make_dynamic_histogram( + bh::axis::regular<>(2, -1, 1), + bh::axis::regular<>(2, 2, 4) + ); h.fill(-0.5, 2.5, bh::weight(1)); // low, low h.fill(-0.5, 3.5, bh::weight(2)); // low, high h.fill( 0.5, 2.5, bh::weight(3)); // high, low h.fill( 0.5, 3.5, bh::weight(4)); // high, high // access value of bin count, number of arguments must be equal - std::cout << h.value(0, 0) << " " // low, low - << h.value(0, 1) << " " // low, high - << h.value(1, 0) << " " // high, low - << h.value(1, 1) // high, high + std::cout << h.bin(0, 0).value() << " " // low, low + << h.bin(0, 1).value() << " " // low, high + << h.bin(1, 0).value() << " " // high, low + << h.bin(1, 1).value() // high, high << std::endl; // prints: 1 2 3 4 - // access variance of bin count - std::cout << h.variance(0, 0) << " " // low, low - << h.variance(0, 1) << " " // low, high - << h.variance(1, 0) << " " // high, low - << h.variance(1, 1) // high, high + // access variance of bin count, this only works for weighted histograms + std::cout << h.bin(0, 0).variance() << " " // low, low + << h.bin(0, 1).variance() << " " // low, high + << h.bin(1, 0).variance() << " " // high, low + << h.bin(1, 1).variance() // high, high << std::endl; // prints: 1 4 9 16 @@ -239,8 +240,7 @@ int main() { // is an error std::vector idx(2); idx = {0, 1}; - std::cout << h.value(idx.begin(), idx.end()) << " " - << h.variance(idx.begin(), idx.end()) << std::endl; + std::cout << h.bin(idx.begin(), idx.end()).value() << std::endl; } `` @@ -296,15 +296,15 @@ int main() { // accept and return rvalue references where possible auto h4 = h1 + h2 + h3; - std::cout << h4.value(0) << " " << h4.value(1) << std::endl; + std::cout << h4.bin(0).value() << " " << h4.bin(1).value() << std::endl; // prints: 2 2 - // histograms are scalable by real numbers - h4 *= 2.5; - auto h5 = h4 / 5; + // histograms are scalable + h4 *= 2; + auto h5 = h4 / 4; - std::cout << h5.value(0) << " " << h5.value(1) << std::endl; + std::cout << h5.bin(0).value() << " " << h5.bin(1).value() << std::endl; // prints: 1 1 @@ -316,10 +316,10 @@ int main() { // beware the special effect of multiplying a histogram on its variance auto h = bh::make_static_histogram(bh::axis::regular<>(2, -1, 1)); h.fill(-0.5); - std::cout << "value " << (2 * h).value(0) - << " " << (h + h).value(0) << "\n" - << "variance " << (2 * h).variance(0) - << " " << (h + h).variance(0) << std::endl; + std::cout << "value " << (2 * h).bin(0).value() + << " " << (h + h).bin(0).value() << "\n" + << "variance " << (2 * h).bin(0).variance() + << " " << (h + h).bin(0).variance() << std::endl; // equality operator also checks variances, so the statement is false std::cout << (h + h == 2 * h) << std::endl; @@ -366,21 +366,21 @@ int main() { - bin counters are summed over removed axes - hr0 and hr1 have same number of total counts as h */ - std::cout << h.sum() << " " << hr0.sum() << " " << hr1.sum() << std::endl; + std::cout << h.sum().value() << " " << hr0.sum().value() << " " << hr1.sum().value() << std::endl; for (const auto& ybin : h.axis(1_c)) { for (const auto& xbin : h.axis(0_c)) { - std::cout << h.value(xbin.first, ybin.first) << " "; + std::cout << h.bin(xbin.first, ybin.first).value() << " "; } std::cout << std::endl; } for (const auto& bin : hr0.axis()) - std::cout << hr0.value(bin.first) << " "; + std::cout << hr0.bin(bin.first).value() << " "; std::cout << std::endl; for (const auto& bin : hr1.axis()) - std::cout << hr1.value(bin.first) << " "; + std::cout << hr1.bin(bin.first).value() << " "; std::cout << std::endl; } `` @@ -514,13 +514,13 @@ for x in (2e0, 2e1, 2e2, 2e3, 2e4): # iterate over bins and access bin counter for idx, (lower, upper) in enumerate(h.axis(0)): print "bin {0} x in [{1}, {2}): {3} +/- {4}".format( - idx, lower, upper, h.value(idx), h.variance(idx) ** 0.5) + idx, lower, upper, h.bin(idx).value, h.bin(idx).variance ** 0.5) # under- and overflow bins are accessed like in C++ lo, up = h.axis(0)[-1] -print "underflow [{0}, {1}): {2} +/- {3}".format(lo, up, h.value(-1), h.variance(-1)) +print "underflow [{0}, {1}): {2} +/- {3}".format(lo, up, h.bin(-1).value, h.bin(-1).variance) lo, up = h.axis(0)[5] -print "overflow [{0}, {1}): {2} +/- {3}".format(lo, up, h.value(5), h.variance(5)) +print "overflow [{0}, {1}): {2} +/- {3}".format(lo, up, h.bin(5).value, h.bin(5).variance) # prints: # bin 0 x in [1.0, 10.0): 4.0 +/- 4.0 @@ -644,7 +644,7 @@ h2.fill(0.5) h3 = h1 + h2 h4 = h3 * 2 -print h4.value(0), h4.value(1) +print h4.bin(0), h4.bin(1) # prints: 2.0 2.0 @@ -710,7 +710,7 @@ cpp_filler.process(h) # histogram is filled with input values in C++ for iy in range(len(h.axis(1))): for ix in range(len(h.axis(0))): - print h.value(ix, iy), + print h.bin(ix, iy), print # prints: @@ -761,9 +761,9 @@ int main() { h.fill("1"); h.fill("9"); - for (const auto& bin : h.axis()) { - std::cout << "bin " << bin.first << " [" << bin.second.lower() << ", " - << bin.second.upper() << ") " << h.value(bin.first) + for (const auto& a : h.axis()) { + std::cout << "bin " << a.first << " [" << a.second.lower() << ", " + << a.second.upper() << ") " << h.bin(a.first).value() << std::endl; } diff --git a/include/boost/histogram/detail/meta.hpp b/include/boost/histogram/detail/meta.hpp index 58708928..dd3795d3 100644 --- a/include/boost/histogram/detail/meta.hpp +++ b/include/boost/histogram/detail/meta.hpp @@ -32,13 +32,13 @@ namespace detail { template ().size(), std::declval().increase(0), - std::declval().value(0))> + std::declval()[0])> struct requires_storage {}; template struct has_variance_support { - template + template ().variance())> struct SFINAE {}; - template static std::true_type Test(SFINAE *); + template static std::true_type Test(SFINAE *); template static std::false_type Test(...); using type = decltype(Test(nullptr)); }; diff --git a/include/boost/histogram/detail/utility.hpp b/include/boost/histogram/detail/utility.hpp index 66a44b3a..92c30338 100644 --- a/include/boost/histogram/detail/utility.hpp +++ b/include/boost/histogram/detail/utility.hpp @@ -17,24 +17,6 @@ namespace boost { namespace histogram { namespace detail { -template -void storage_add_impl(std::true_type, Storage &result, const Storage &input, - std::size_t dst, std::size_t src) { - result.add(dst, input.value(src), input.variance(src)); -} - -template -void storage_add_impl(std::false_type, Storage &result, const Storage &input, - std::size_t dst, std::size_t src) { - result.add(dst, input.value(src)); -} - -template -void storage_add(Storage &result, const Storage &input, std::size_t dst, - std::size_t src) { - storage_add_impl(has_variance_support_t(), result, input, dst, src); -} - inline void escape(std::ostream &os, const string_view s) { os << '\''; for (auto sit = s.begin(); sit != s.end(); ++sit) { diff --git a/include/boost/histogram/dynamic_histogram.hpp b/include/boost/histogram/dynamic_histogram.hpp index 89365e72..c2db8439 100644 --- a/include/boost/histogram/dynamic_histogram.hpp +++ b/include/boost/histogram/dynamic_histogram.hpp @@ -18,11 +18,15 @@ #include #include #include +#include +#include #include #include +#include #include #include #include +#include #include #include #include @@ -54,8 +58,8 @@ class histogram { public: using any_axis_type = axis::any; using axes_type = std::vector; - using value_type = typename Storage::value_type; - using value_iterator = value_iterator_over; + using bin_type = typename Storage::bin_type; + using bin_iterator = bin_iterator_over; public: histogram() = default; @@ -120,12 +124,14 @@ public: return *this; } - histogram &operator*=(const value_type rhs) { + template + histogram &operator*=(const T& rhs) { storage_ *= rhs; return *this; } - histogram &operator/=(const value_type rhs) { + template + histogram &operator/=(const T& rhs) { storage_ *= 1.0 / rhs; return *this; } @@ -167,11 +173,11 @@ public: std::size_t idx = 0, stride = 1; xlin_iter(idx, stride, begin); if (stride) { - storage_.increase_by_weight(idx, w.value); + storage_.add(idx, w.value); } } - template value_type value(Indices &&... indices) const { + template bin_type bin(Indices &&... indices) const { if (dim() != sizeof...(indices)) throw std::invalid_argument( "value arguments does not match histogram dimension"); @@ -179,47 +185,19 @@ public: lin<0>(idx, stride, indices...); if (stride == 0) throw std::out_of_range("invalid index"); - return storage_.value(idx); + return storage_[idx]; } template > - value_type value(Iterator begin, Iterator end) const { + bin_type bin(Iterator begin, Iterator end) const { if (dim() != std::distance(begin, end)) throw std::invalid_argument( - "value iterator range does not match histogram dimension"); + "iterator range in bin(...) does not match histogram dimension"); std::size_t idx = 0, stride = 1; lin_iter(idx, stride, begin); if (stride == 0) throw std::out_of_range("invalid index"); - return storage_.value(idx); - } - - template - detail::requires_variance_support variance(Indices &&... indices) const { - if (dim() != sizeof...(indices)) - throw std::invalid_argument( - "variance arguments does not match histogram dimension"); - std::size_t idx = 0, stride = 1; - lin<0>(idx, stride, indices...); - if (stride == 0) { - throw std::out_of_range("invalid index"); - } - return storage_.variance(idx); - } - - template > - detail::requires_variance_support variance(Iterator begin, - Iterator end) const { - if (dim() != std::distance(begin, end)) - throw std::invalid_argument( - "variance iterator range does not match histogram dimension"); - std::size_t idx = 0, stride = 1; - lin_iter(idx, stride, begin); - if (stride == 0) { - throw std::out_of_range("invalid index"); - } - return storage_.variance(idx); + return storage_[idx]; } /// Number of axes (dimensions) of histogram @@ -229,11 +207,11 @@ public: std::size_t bincount() const noexcept { return storage_.size(); } /// Sum of all counts in the histogram - double sum() const noexcept { - double result = 0.0; + bin_type sum() const noexcept { + bin_type result(0); // don't use bincount() here, so sum() still works in a moved-from object for (std::size_t i = 0, n = storage_.size(); i < n; ++i) { - result += storage_.value(i); + result += storage_[i]; } return result; } @@ -287,11 +265,11 @@ public: return reduce_impl(b); } - value_iterator begin() const noexcept { - return value_iterator(*this, storage_); + bin_iterator begin() const noexcept { + return bin_iterator(*this, storage_); } - value_iterator end() const noexcept { return value_iterator(storage_); } + bin_iterator end() const noexcept { return bin_iterator(storage_); } private: axes_type axes_; @@ -306,8 +284,8 @@ private: template inline void fill_impl(mpl::false_, mpl::false_, const Args &... args) { std::size_t idx = 0, stride = 1; - double w; - xlin<0>(idx, stride, w, args...); + int dummy; + xlin<0>(idx, stride, dummy, args...); if (stride) { storage_.increase(idx); } @@ -316,10 +294,12 @@ private: template inline void fill_impl(mpl::true_, mpl::false_, const Args &... args) { std::size_t idx = 0, stride = 1; - double w; + typename mpl::deref< + typename mpl::find_if, detail::is_weight>::type + >::type w; xlin<0>(idx, stride, w, args...); if (stride) { - storage_.increase_by_weight(idx, w); + storage_.add(idx, w); } } @@ -376,21 +356,21 @@ private: } }; - template - inline void xlin(std::size_t &, std::size_t &, double &) const {} + template + inline void xlin(std::size_t &, std::size_t &, Weight &) const {} - template - inline void xlin(std::size_t &idx, std::size_t &stride, double &w, + template + inline void xlin(std::size_t &idx, std::size_t &stride, Weight &w, const First &first, const Rest &... rest) const { apply_visitor(xlin_visitor{idx, stride, first}, axes_[D]); return xlin(idx, stride, w, rest...); } - template - inline void xlin(std::size_t &idx, std::size_t &stride, double &w, - const detail::weight_t &first, + template + inline void xlin(std::size_t &idx, std::size_t &stride, Weight& w, + const Weight &first, const Rest &... rest) const { - w = first.value; + w = first; return xlin(idx, stride, w, rest...); } @@ -426,7 +406,7 @@ private: histogram h(axes.begin(), axes.end()); detail::index_mapper m(n, b); do { - detail::storage_add(h.storage_, storage_, m.second, m.first); + h.storage_.add(m.second, storage_[m.first]); } while (m.next()); return h; } @@ -445,6 +425,16 @@ make_dynamic_histogram(Axes &&... axes) { std::forward(axes)...); } +template +histogram>, +array_storage>> +make_dynamic_weighted_histogram(Axes &&... axes) { + return histogram>, + array_storage> + >(std::forward(axes)...); +} + template histogram>, Storage> @@ -464,6 +454,18 @@ make_dynamic_histogram(Iterator begin, Iterator end) { begin, end); } +template > +histogram, + array_storage>> +make_dynamic_weighted_histogram(Iterator begin, Iterator end) { + return histogram< + dynamic_tag, + detail::combine_t, + array_storage>>( + begin, end); +} + template > histogram< diff --git a/include/boost/histogram/histogram_fwd.hpp b/include/boost/histogram/histogram_fwd.hpp index a4095ef2..465d6547 100644 --- a/include/boost/histogram/histogram_fwd.hpp +++ b/include/boost/histogram/histogram_fwd.hpp @@ -15,6 +15,7 @@ namespace boost { namespace histogram { class adaptive_storage; +template class array_storage; namespace axis { diff --git a/include/boost/histogram/iterator.hpp b/include/boost/histogram/iterator.hpp index c600dbec..13bcabb1 100644 --- a/include/boost/histogram/iterator.hpp +++ b/include/boost/histogram/iterator.hpp @@ -66,58 +66,29 @@ protected: } // namespace detail template -class value_iterator_over +class bin_iterator_over : public iterator_facade< - value_iterator_over, typename Storage::value_type, - forward_traversal_tag, typename Storage::value_type>, + bin_iterator_over, typename Storage::bin_type, + forward_traversal_tag, typename Storage::bin_type>, public detail::multi_index { public: /// begin iterator template - value_iterator_over(const Histogram &h, const Storage &s) + bin_iterator_over(const Histogram &h, const Storage &s) : detail::multi_index(h), s_(s) {} /// end iterator - explicit value_iterator_over(const Storage &s) : s_(s) {} + explicit bin_iterator_over(const Storage &s) : s_(s) {} - value_iterator_over(const value_iterator_over &) = default; - value_iterator_over &operator=(const value_iterator_over &) = default; + bin_iterator_over(const bin_iterator_over &) = default; + bin_iterator_over &operator=(const bin_iterator_over &) = default; private: - bool equal(const value_iterator_over &other) const noexcept { + bool equal(const bin_iterator_over &other) const noexcept { return &s_ == &(other.s_) && idx_ == other.idx_; } - typename Storage::value_type dereference() const { return s_.value(idx_); } - - const Storage &s_; - friend class ::boost::iterator_core_access; -}; - -template -class variance_iterator_over - : public iterator_facade< - variance_iterator_over, typename Storage::value_type, - forward_traversal_tag, typename Storage::value_type>, - public detail::multi_index { - -public: - /// begin iterator - template - variance_iterator_over(const Histogram &h, const Storage &s) - : detail::multi_index(h), s_(s) {} - - /// end iterator - explicit variance_iterator_over(const Storage &s) : s_(s) {} - - variance_iterator_over(const variance_iterator_over &) = default; - variance_iterator_over &operator=(const variance_iterator_over &) = default; - -private: - bool equal(const variance_iterator_over &other) const noexcept { - return &s_ == &(other.s_) && idx_ == other.idx_; - } - typename Storage::value_type dereference() const { return s_.variance(idx_); } + typename Storage::bin_type dereference() const { return s_[idx_]; } const Storage &s_; friend class ::boost::iterator_core_access; diff --git a/include/boost/histogram/static_histogram.hpp b/include/boost/histogram/static_histogram.hpp index f42461f7..3cd567db 100644 --- a/include/boost/histogram/static_histogram.hpp +++ b/include/boost/histogram/static_histogram.hpp @@ -28,6 +28,8 @@ #include #include #include +#include +#include #include #include #include @@ -50,8 +52,8 @@ class histogram { public: using axes_type = typename fusion::result_of::as_vector::type; - using value_type = typename Storage::value_type; - using value_iterator = value_iterator_over; + using bin_type = typename Storage::bin_type; + using bin_iterator = bin_iterator_over; histogram() = default; histogram(const histogram &rhs) = default; @@ -132,12 +134,12 @@ public: return *this; } - histogram &operator*=(const value_type rhs) { + template histogram &operator*=(const T &rhs) { storage_ *= rhs; return *this; } - histogram &operator/=(const value_type rhs) { + template histogram &operator/=(const T &rhs) { storage_ *= 1.0 / rhs; return *this; } @@ -159,7 +161,7 @@ public: } template - value_type value(const Indices &... indices) const { + bin_type bin(const Indices &... indices) const { static_assert(sizeof...(indices) == axes_size::value, "number of arguments does not match histogram dimension"); std::size_t idx = 0, stride = 1; @@ -167,20 +169,7 @@ public: if (stride == 0) { throw std::out_of_range("invalid index"); } - return storage_.value(idx); - } - - template - detail::requires_variance_support - variance(const Indices &... indices) const { - static_assert(sizeof...(indices) == axes_size::value, - "number of arguments does not match histogram dimension"); - std::size_t idx = 0, stride = 1; - lin<0>(idx, stride, indices...); - if (stride == 0) { - throw std::out_of_range("invalid index"); - } - return storage_.variance(idx); + return storage_[idx]; } /// Number of axes (dimensions) of histogram @@ -190,10 +179,10 @@ public: std::size_t bincount() const noexcept { return storage_.size(); } /// Sum of all counts in the histogram - double sum() const noexcept { - double result = 0.0; + bin_type sum() const noexcept { + bin_type result(0); for (std::size_t i = 0, n = storage_.size(); i < n; ++i) - result += storage_.value(i); + result += storage_[i]; return result; } @@ -252,11 +241,11 @@ public: return hr; } - value_iterator begin() const noexcept { - return value_iterator(*this, storage_); + bin_iterator begin() const noexcept { + return bin_iterator(*this, storage_); } - value_iterator end() const noexcept { return value_iterator(storage_); } + bin_iterator end() const noexcept { return bin_iterator(storage_); } private: axes_type axes_; @@ -271,8 +260,8 @@ private: template inline void fill_impl(mpl::false_, mpl::false_, const Args &... args) { std::size_t idx = 0, stride = 1; - double w; - xlin<0>(idx, stride, w, args...); + int dummy; + xlin<0>(idx, stride, dummy, args...); if (stride) { storage_.increase(idx); } @@ -281,10 +270,12 @@ private: template inline void fill_impl(mpl::true_, mpl::false_, const Args &... args) { std::size_t idx = 0, stride = 1; - double w; + typename mpl::deref< + typename mpl::find_if, detail::is_weight>::type + >::type w; xlin<0>(idx, stride, w, args...); if (stride) - storage_.increase_by_weight(idx, w); + storage_.add(idx, w); } template @@ -307,21 +298,20 @@ private: return lin(idx, stride, rest...); } - template - inline void xlin(std::size_t &, std::size_t &, double &) const {} + template + inline void xlin(std::size_t &, std::size_t &, Weight &) const {} - template - inline void xlin(std::size_t &idx, std::size_t &stride, double &w, + template + inline void xlin(std::size_t &idx, std::size_t &stride, Weight &w, const First &first, const Rest &... rest) const { detail::xlin(idx, stride, fusion::at_c(axes_), first); return xlin(idx, stride, w, rest...); } - template - inline void xlin(std::size_t &idx, std::size_t &stride, double &w, - const detail::weight_t &first, - const Rest &... rest) const { - w = first.value; + template + inline void xlin(std::size_t &idx, std::size_t &stride, Weight &w, + const Weight &first, const Rest &... rest) const { + w = first; return xlin(idx, stride, w, rest...); } @@ -339,7 +329,7 @@ private: for_each_axis(shape_assign_visitor{n.begin()}); detail::index_mapper m(n, b); do { - detail::storage_add(h.storage_, storage_, m.second, m.first); + h.storage_.add(m.second, storage_[m.first]); } while (m.next()); } @@ -356,6 +346,14 @@ make_static_histogram(Axis &&... axis) { return h(typename h::axes_type(std::forward(axis)...)); } +template +inline histogram, array_storage>> +make_static_weighted_histogram(Axis &&... axis) { + using h = histogram, array_storage>>; + return h(typename h::axes_type(std::forward(axis)...)); +} + + /// static type factory with variable storage type template inline histogram, Storage> diff --git a/include/boost/histogram/storage/adaptive_storage.hpp b/include/boost/histogram/storage/adaptive_storage.hpp index 4ea4b446..419cd3d7 100644 --- a/include/boost/histogram/storage/adaptive_storage.hpp +++ b/include/boost/histogram/storage/adaptive_storage.hpp @@ -224,24 +224,25 @@ struct increase_visitor : public static_visitor { struct wincrease_visitor : public static_visitor { any_array &lhs_any; const std::size_t idx; - const double rhs; - wincrease_visitor(any_array &l, const std::size_t i, const double r) - : lhs_any(l), idx(i), rhs(r) {} + const detail::weight_t rhs; + template + wincrease_visitor(any_array &l, const std::size_t i, const detail::weight_t& r) + : lhs_any(l), idx(i), rhs{static_cast(r.value)} {} template void operator()(array &lhs) const { array a(lhs); - a[idx].increase_by_weight(rhs); + a[idx] += rhs; lhs_any = std::move(a); } void operator()(array &lhs) const { array a(lhs.size); - a[idx].increase_by_weight(rhs); + a[idx] += rhs; lhs_any = std::move(a); } void operator()(array &lhs) const { - lhs[idx].increase_by_weight(rhs); + lhs[idx] += rhs; } }; @@ -301,7 +302,37 @@ template struct radd_visitor : public static_visitor { } void operator()(array &lhs) const { - lhs[idx].increase_by_count(static_cast(rhs)); + lhs[idx] += rhs; + } +}; + +template <> struct radd_visitor : public static_visitor { + any_array &lhs_any; + const std::size_t idx; + const mp_int &rhs; + radd_visitor(any_array &l, const std::size_t i, const mp_int &r) + : lhs_any(l), idx(i), rhs(r) {} + + template void operator()(array &lhs) const { + if (!safe_radd(lhs[idx], rhs)) { + lhs_any = array>(lhs); + operator()(get>>(lhs_any)); + } + } + + void operator()(array &lhs) const { + if (rhs != 0) { + lhs_any = array(lhs.size); + operator()(get>(lhs_any)); + } + } + + void operator()(array &lhs) const { + lhs[idx] += rhs; + } + + void operator()(array &lhs) const { + lhs[idx] += static_cast(rhs); } }; @@ -382,7 +413,7 @@ class adaptive_storage { using buffer_type = detail::any_array; public: - using value_type = double; + using bin_type = weight_counter; explicit adaptive_storage(std::size_t s) : buffer_(detail::array(s)) {} @@ -395,22 +426,22 @@ public: template explicit adaptive_storage(const RHS &rhs) : buffer_(detail::array(rhs.size())) { - using T = typename RHS::value_type; + using T = typename RHS::bin_type; for (auto i = 0ul, n = rhs.size(); i < n; ++i) { - apply_visitor(detail::assign_visitor(buffer_, i, rhs.value(i)), + apply_visitor(detail::assign_visitor(buffer_, i, rhs[i]), buffer_); } } template adaptive_storage &operator=(const RHS &rhs) { - using T = typename RHS::value_type; + using T = typename RHS::bin_type; if (static_cast(this) != static_cast(&rhs)) { const auto n = rhs.size(); if (size() != n) { buffer_ = detail::array(n); } for (auto i = 0ul; i < n; ++i) { - apply_visitor(detail::assign_visitor(buffer_, i, rhs.value(i)), + apply_visitor(detail::assign_visitor(buffer_, i, rhs[i]), buffer_); } } @@ -429,28 +460,24 @@ public: apply_visitor(detail::radd_visitor(buffer_, i, value), buffer_); } - template - void add(std::size_t i, const T &value, const T &variance) { - if (value == variance) { - apply_visitor(detail::radd_visitor(buffer_, i, value), buffer_); + template void add(std::size_t i, const detail::weight_t &w) { + apply_visitor(detail::wincrease_visitor(buffer_, i, w), + buffer_); + } + + void add(std::size_t i, const bin_type& x) { + if (x.value() == x.variance()) { + apply_visitor(detail::radd_visitor(buffer_, i, x.value()), buffer_); } else { apply_visitor(detail::radd_visitor( - buffer_, i, detail::weight_counter(value, variance)), + buffer_, i, detail::weight_counter(x.value(), x.variance())), buffer_); } } - void increase_by_weight(std::size_t i, const value_type weight_counter) { - apply_visitor(detail::wincrease_visitor(buffer_, i, weight_counter), - buffer_); - } - - value_type value(std::size_t i) const { - return apply_visitor(detail::value_visitor(i), buffer_); - } - - value_type variance(std::size_t i) const { - return apply_visitor(detail::variance_visitor(i), buffer_); + bin_type operator[](std::size_t i) const { + return {apply_visitor(detail::value_visitor(i), buffer_), + apply_visitor(detail::variance_visitor(i), buffer_)}; } bool operator==(const adaptive_storage &rhs) const { @@ -460,8 +487,9 @@ public: // precondition: storages have same size adaptive_storage &operator+=(const adaptive_storage &rhs) { if (this == &rhs) { - for (auto i = 0ul, n = size(); i < n; ++i) - add(i, rhs.value(i), rhs.variance(i)); // this is losing precision + for (auto i = 0ul, n = size(); i < n; ++i) { + add(i, rhs[i]); // may loose precision + } } else { apply_visitor(detail::radd_array_visitor(buffer_), rhs.buffer_); } @@ -471,13 +499,13 @@ public: // precondition: storages have same size template adaptive_storage &operator+=(const RHS &rhs) { for (auto i = 0ul, n = size(); i < n; ++i) - apply_visitor(detail::radd_visitor( - buffer_, i, rhs.value(i)), + apply_visitor(detail::radd_visitor( + buffer_, i, rhs[i]), buffer_); return *this; } - adaptive_storage &operator*=(const value_type x) { + template adaptive_storage &operator*=(const T& x) { apply_visitor(detail::rmul_visitor(buffer_, x), buffer_); return *this; } diff --git a/include/boost/histogram/storage/array_storage.hpp b/include/boost/histogram/storage/array_storage.hpp index ee66ce78..43460a0f 100644 --- a/include/boost/histogram/storage/array_storage.hpp +++ b/include/boost/histogram/storage/array_storage.hpp @@ -23,18 +23,9 @@ class access; namespace boost { namespace histogram { -namespace detail { -template struct counter_traits { - using value_type = T; - static value_type value(const T &t) noexcept { return t; } - static void increase_by_count(T &lhs, const T &n) noexcept { lhs += n; } - static void increase_by_weight(T &lhs, const T &w) noexcept { lhs += w; } -}; -} // namespace detail - template class array_storage { public: - using value_type = typename detail::counter_traits::value_type; + using bin_type = T; explicit array_storage(std::size_t s) { init(s); } @@ -65,14 +56,14 @@ public: template explicit array_storage(const S &other) { reset(other.size()); for (decltype(size_) i = 0u; i < size_; ++i) { - array_[i] = other.value(i); + array_[i] = static_cast(other[i]); } } template array_storage &operator=(const S &other) { reset(other.size()); for (decltype(size_) i = 0u; i < size_; ++i) { - array_[i] = other.value(i); + array_[i] = static_cast(other[i]); } return *this; } @@ -81,53 +72,45 @@ public: void increase(std::size_t i) noexcept { ++array_[i]; } - void increase_by_weight(std::size_t i, const value_type &w) noexcept { - detail::counter_traits::increase_by_weight(array_[i], w); + template void add(std::size_t i, const U &x) noexcept { + array_[i] += x; } - void add(std::size_t i, const value_type &n) noexcept { - detail::counter_traits::increase_by_count(array_[i], n); - } - - template - void add(std::size_t i, const value_type &value, - const value_type &variance) noexcept { - array_[i] += T(value, variance); - } - - value_type value(std::size_t i) const noexcept { - return detail::counter_traits::value(array_[i]); - } - - template ().variance())> - value_type variance(std::size_t i) const noexcept { - return array_[i].variance(); + const bin_type& operator[](std::size_t i) const noexcept { + return array_[i]; } template - array_storage &operator+=(const array_storage &rhs) noexcept { - for (auto i = 0ul; i < size_; ++i) - array_[i] += rhs.array_[i]; + bool operator==(const array_storage &rhs) const noexcept { + if (size_ != rhs.size_) + return false; + return std::equal(array_.get(), array_.get() + size_, + rhs.array_.get()); + } + + template array_storage &operator+=(const S &rhs) noexcept { + for (std::size_t i = 0; i < size_; ++i) + array_[i] += static_cast(rhs[i]); return *this; } - array_storage &operator*=(const value_type x) noexcept { - for (auto i = 0ul; i < size_; ++i) + template array_storage &operator*=(const U &x) noexcept { + for (std::size_t i = 0; i < size_; ++i) array_[i] *= x; return *this; } private: std::size_t size_ = 0; - std::unique_ptr array_; + std::unique_ptr array_; void reset(std::size_t size) { size_ = size; - array_.reset(new T[size]); + array_.reset(new bin_type[size]); } void init(std::size_t size) { reset(size); - std::fill(array_.get(), array_.get() + size, T(0)); + std::fill(array_.get(), array_.get() + size, bin_type(0)); } template friend class array_storage; diff --git a/include/boost/histogram/storage/operators.hpp b/include/boost/histogram/storage/operators.hpp index ef4326e3..a2af99f6 100644 --- a/include/boost/histogram/storage/operators.hpp +++ b/include/boost/histogram/storage/operators.hpp @@ -14,17 +14,23 @@ namespace histogram { namespace detail { template bool equal_impl(std::true_type, std::true_type, const S1 &s1, const S2 &s2) { - for (decltype(s1.size()) i = 0, n = s1.size(); i < n; ++i) - if (s1.value(i) != s2.value(i) || s1.variance(i) != s2.variance(i)) + for (std::size_t i = 0, n = s1.size(); i < n; ++i) { + auto x1 = s1[i]; + auto x2 = s2[i]; + if (x1.value() != x2.value() || x1.variance() != x2.variance()) return false; + } return true; } template bool equal_impl(std::true_type, std::false_type, const S1 &s1, const S2 &s2) { - for (decltype(s1.size()) i = 0, n = s1.size(); i < n; ++i) - if (s1.value(i) != s2.value(i) || s1.value(i) != s1.variance(i)) + for (std::size_t i = 0, n = s1.size(); i < n; ++i) { + auto x1 = s1[i]; + auto x2 = s2[i]; + if (x1.value() != x2 || x1.value() != x1.variance()) return false; + } return true; } @@ -35,8 +41,8 @@ bool equal_impl(std::false_type, std::true_type, const S1 &s1, const S2 &s2) { template bool equal_impl(std::false_type, std::false_type, const S1 &s1, const S2 &s2) { - for (decltype(s1.size()) i = 0, n = s1.size(); i < n; ++i) - if (s1.value(i) != s2.value(i)) + for (std::size_t i = 0, n = s1.size(); i < n; ++i) + if (s1[i] != s2[i]) return false; return true; } diff --git a/include/boost/histogram/storage/weight_counter.hpp b/include/boost/histogram/storage/weight_counter.hpp index c00ae687..b24dda53 100644 --- a/include/boost/histogram/storage/weight_counter.hpp +++ b/include/boost/histogram/storage/weight_counter.hpp @@ -7,6 +7,8 @@ #ifndef _BOOST_HISTOGRAM_STORAGE_WEIGHT_COUNTER_HPP_ #define _BOOST_HISTOGRAM_STORAGE_WEIGHT_COUNTER_HPP_ +#include + namespace boost { namespace serialization { @@ -50,24 +52,25 @@ public: return *this; } + template + weight_counter &operator+=(const detail::weight_t &rhs) { + w += rhs.value; + w2 += rhs.value * rhs.value; + return *this; + } + + weight_counter &operator+=(const RealType &x) { + w += x; + w2 += x; + return *this; + } + weight_counter &operator*=(const RealType x) { w *= x; w2 *= x * x; return *this; } - weight_counter &increase_by_count(const RealType n) { - w += n; - w2 += n; - return *this; - } - - weight_counter &increase_by_weight(const RealType x) { - w += x; - w2 += x * x; - return *this; - } - bool operator==(const weight_counter &rhs) const { return w == rhs.w && w2 == rhs.w2; } @@ -92,6 +95,7 @@ public: RealType value() const noexcept { return w; } RealType variance() const noexcept { return w2; } + template explicit operator T() const noexcept { return w; } private: friend class ::boost::serialization::access; @@ -111,24 +115,6 @@ bool operator!=(const T &t, const weight_counter &w) { return !(w == t); } -namespace detail { -template struct counter_traits; // fwd declaration - -// specialization -template struct counter_traits> { - using value_type = T; - static value_type value(const weight_counter &w) noexcept { - return w.value(); - } - static void increase_by_count(weight_counter &lhs, const T &n) noexcept { - lhs.increase_by_count(n); - } - static void increase_by_weight(weight_counter &lhs, const T &w) noexcept { - lhs.increase_by_weight(w); - } -}; -} // namespace detail - } // namespace histogram } // namespace boost diff --git a/src/python/histogram.cpp b/src/python/histogram.cpp index 253a43b2..4d0a8985 100644 --- a/src/python/histogram.cpp +++ b/src/python/histogram.cpp @@ -276,7 +276,7 @@ bp::object histogram_fill(bp::tuple args, bp::dict kwargs) { return bp::object(); } -bp::object histogram_value(bp::tuple args, bp::dict kwargs) { +bp::object histogram_bin(bp::tuple args, bp::dict kwargs) { const pyhistogram & self = bp::extract(args[0]); const unsigned dim = len(args) - 1; @@ -301,35 +301,7 @@ bp::object histogram_value(bp::tuple args, bp::dict kwargs) { for (unsigned i = 0; i < self.dim(); ++i) idx[i] = bp::extract(args[1 + i]); - return bp::object(self.value(idx, idx + self.dim())); -} - -bp::object histogram_variance(bp::tuple args, bp::dict kwargs) { - const pyhistogram& self = bp::extract(args[0]); - - const unsigned dim = len(args) - 1; - if (self.dim() != dim) { - PyErr_SetString(PyExc_RuntimeError, "wrong number of arguments"); - bp::throw_error_already_set(); - } - - if (dim > BOOST_HISTOGRAM_AXIS_LIMIT) { - std::ostringstream os; - os << "too many axes, maximum is " << BOOST_HISTOGRAM_AXIS_LIMIT; - PyErr_SetString(PyExc_RuntimeError, os.str().c_str()); - bp::throw_error_already_set(); - } - - if (kwargs) { - PyErr_SetString(PyExc_RuntimeError, "no keyword arguments allowed"); - bp::throw_error_already_set(); - } - - int idx[BOOST_HISTOGRAM_AXIS_LIMIT]; - for (auto i = 0u; i < self.dim(); ++i) - idx[i] = bp::extract(args[1 + i]); - - return bp::object(self.variance(idx, idx + self.dim())); + return bp::object(self.bin(idx, idx + self.dim())); } bp::object histogram_reduce_to(bp::tuple args, bp::dict kwargs) { @@ -365,6 +337,12 @@ std::string histogram_repr(const pyhistogram &h) { void register_histogram() { bp::docstring_options dopt(true, true, false); + bp::class_( + "bin_type", "Holds value and variance of bin count.", bp::no_init) + .add_property("value", &pyhistogram::bin_type::value) + .add_property("variance", &pyhistogram::bin_type::variance) + ; + bp::class_>( "histogram", "N-dimensional histogram for real-valued data.", bp::no_init) .def("__init__", bp::raw_function(histogram_init), @@ -391,12 +369,9 @@ void register_histogram() { ":return: total number of bins, including under- and overflow") .add_property("sum", &pyhistogram::sum, ":return: sum of all entries, including under- and overflow bins") - .def("value", bp::raw_function(histogram_value), + .def("bin", bp::raw_function(histogram_bin), ":param int args: indices of the bin (number must match dimension)" - "\n:return: count for the bin") - .def("variance", bp::raw_function(histogram_variance), - ":param int args: indices of the bin (number must match dimension)" - "\n:return: variance estimate for the bin") + "\n:return: bin content") .def("reduce_to", bp::raw_function(histogram_reduce_to), ":param int args: indices of the axes in the reduced histogram" "\n:return: reduced histogram with subset of axes") diff --git a/test/adaptive_storage_test.cpp b/test/adaptive_storage_test.cpp index 7d1187e6..6ab69c46 100644 --- a/test/adaptive_storage_test.cpp +++ b/test/adaptive_storage_test.cpp @@ -21,8 +21,8 @@ template adaptive_storage prepare(std::size_t n = 1) { adaptive_storage s(n); s.increase(0); const auto tmax = std::numeric_limits::max(); - while (s.value(0) < 0.1 * tmax) { - s.add(0, s.value(0)); + while (s[0].value() < 0.1 * tmax) { + s.add(0, s[0].value()); } return s; } @@ -34,7 +34,7 @@ template <> adaptive_storage prepare(std::size_t n) { template <> adaptive_storage prepare(std::size_t n) { adaptive_storage s(n); - s.increase_by_weight(0, 1.0); + s.add(0, weight(1.0)); return s; } @@ -43,9 +43,9 @@ template <> adaptive_storage prepare(std::size_t n) { s.increase(0); auto tmax = static_cast(std::numeric_limits::max()); tmax *= 2.0; - while (s.value(0) < tmax) { - assert(s.value(0) != 0); - s.add(0, s.value(0)); + while (s[0].value() < tmax) { + assert(s[0].value() != 0); + s.add(0, s[0].value()); } return s; } @@ -126,8 +126,8 @@ template <> void serialization_impl() { template void equal_impl() { adaptive_storage a(std::size_t(1)); auto b = python::access::set_value(1, T(0)); - BOOST_TEST_EQ(a.value(0), 0.0); - BOOST_TEST_EQ(a.variance(0), 0.0); + BOOST_TEST_EQ(a[0].value(), 0.0); + BOOST_TEST_EQ(a[0].variance(), 0.0); BOOST_TEST(a == b); b.increase(0); BOOST_TEST(!(a == b)); @@ -144,8 +144,8 @@ template <> void equal_impl() { auto b = python::access::set_value(1, uint8_t(0)); auto c = python::access::set_value(2, uint8_t(0)); auto d = array_storage(std::size_t(1)); - BOOST_TEST_EQ(a.value(0), 0.0); - BOOST_TEST_EQ(a.variance(0), 0.0); + BOOST_TEST_EQ(a[0].value(), 0.0); + BOOST_TEST_EQ(a[0].variance(), 0.0); BOOST_TEST(a == b); BOOST_TEST(b == a); BOOST_TEST(a == d); @@ -171,22 +171,22 @@ template void increase_and_grow_impl() { adaptive_storage x(std::size_t(2)); x.increase(0); - n2.add(0, x.value(0)); - n2.add(0, x.value(0)); + n2.add(0, x[0].value()); + n2.add(0, x[0].value()); double v = tmax; ++v; - BOOST_TEST_EQ(n.value(0), v); - BOOST_TEST_EQ(n2.value(0), v); - BOOST_TEST_EQ(n.value(1), 0.0); - BOOST_TEST_EQ(n2.value(1), 0.0); + BOOST_TEST_EQ(n[0].value(), v); + BOOST_TEST_EQ(n2[0].value(), v); + BOOST_TEST_EQ(n[1].value(), 0.0); + BOOST_TEST_EQ(n2[1].value(), 0.0); } template <> void increase_and_grow_impl() { adaptive_storage s(std::size_t(2)); s.increase(0); - BOOST_TEST_EQ(s.value(0), 1.0); - BOOST_TEST_EQ(s.value(1), 0.0); + BOOST_TEST_EQ(s[0].value(), 1.0); + BOOST_TEST_EQ(s[1].value(), 0.0); } template void convert_array_storage_impl() { @@ -196,47 +196,47 @@ template void convert_array_storage_impl() { auto a = aref; a = s; - BOOST_TEST_EQ(a.value(0), 1.0); + BOOST_TEST_EQ(a[0].value(), 1.0); BOOST_TEST(a == s); a.increase(0); BOOST_TEST(!(a == s)); adaptive_storage b(s); - BOOST_TEST_EQ(b.value(0), 1.0); + BOOST_TEST_EQ(b[0].value(), 1.0); BOOST_TEST(b == s); b.increase(0); BOOST_TEST(!(b == s)); auto c = aref; - c.add(0, s.value(0)); - BOOST_TEST_EQ(c.value(0), 1.0); + c.add(0, s[0]); + BOOST_TEST_EQ(c[0].value(), 1.0); BOOST_TEST(c == s); BOOST_TEST(s == c); array_storage t(std::size_t(1)); t.increase(0); - while (t.value(0) < 1e20) - t.add(0, t.value(0)); + while (t[0] < 1e20) + t.add(0, t[0]); auto d = aref; d = t; BOOST_TEST(d == t); auto e = aref; e = s; - BOOST_TEST_EQ(e.value(0), 1.0); + BOOST_TEST_EQ(e[0].value(), 1.0); BOOST_TEST(e == s); e.increase(0); BOOST_TEST(!(e == s)); adaptive_storage f(s); - BOOST_TEST_EQ(f.value(0), 1.0); + BOOST_TEST_EQ(f[0].value(), 1.0); BOOST_TEST(f == s); f.increase(0); BOOST_TEST(!(f == s)); auto g = aref; - g.add(0, s.value(0)); - BOOST_TEST_EQ(g.value(0), 1.0); + g.add(0, s[0]); + BOOST_TEST_EQ(g[0].value(), 1.0); BOOST_TEST(g == s); BOOST_TEST(s == g); @@ -250,20 +250,20 @@ template void convert_array_storage_impl() { template <> void convert_array_storage_impl() { const auto aref = adaptive_storage(std::size_t(1)); - BOOST_TEST_EQ(aref.value(0), 0.0); + BOOST_TEST_EQ(aref[0].value(), 0.0); array_storage s(std::size_t(1)); s.increase(0); auto a = aref; a = s; - BOOST_TEST_EQ(a.value(0), 1.0); + BOOST_TEST_EQ(a[0].value(), 1.0); BOOST_TEST(a == s); a.increase(0); BOOST_TEST(!(a == s)); auto c = aref; - c.add(0, s.value(0)); - BOOST_TEST_EQ(c.value(0), 1.0); + c.add(0, s[0]); + BOOST_TEST_EQ(c[0].value(), 1.0); BOOST_TEST(c == s); BOOST_TEST(s == c); @@ -339,11 +339,11 @@ int main() { // only increase for mp_int auto a = boost::python::access::set_value(2, detail::mp_int(1)); - BOOST_TEST_EQ(a.value(0), 1.0); - BOOST_TEST_EQ(a.value(1), 0.0); + BOOST_TEST_EQ(a[0].value(), 1.0); + BOOST_TEST_EQ(a[1].value(), 0.0); a.increase(0); - BOOST_TEST_EQ(a.value(0), 2.0); - BOOST_TEST_EQ(a.value(1), 0.0); + BOOST_TEST_EQ(a[0].value(), 2.0); + BOOST_TEST_EQ(a[1].value(), 0.0); } // add_and_grow @@ -352,25 +352,25 @@ int main() { a.increase(0); double x = 1.0; adaptive_storage y(std::size_t(1)); - BOOST_TEST_EQ(y.value(0), 0.0); - a.add(0, y.value(0)); - BOOST_TEST_EQ(a.value(0), x); + BOOST_TEST_EQ(y[0].value(), 0.0); + a.add(0, y[0].value()); + BOOST_TEST_EQ(a[0].value(), x); for (unsigned i = 0; i < 80; ++i) { - a.add(0, a.value(0)); + a.add(0, a[0].value()); x += x; adaptive_storage b(std::size_t(1)); - b.add(0, a.value(0)); - BOOST_TEST_EQ(a.value(0), x); - BOOST_TEST_EQ(a.variance(0), x); - BOOST_TEST_EQ(b.value(0), x); - BOOST_TEST_EQ(b.variance(0), x); - b.increase_by_weight(0, 0.0); - BOOST_TEST_EQ(b.value(0), x); - BOOST_TEST_EQ(b.variance(0), x); + b.add(0, a[0].value()); + BOOST_TEST_EQ(a[0].value(), x); + BOOST_TEST_EQ(a[0].variance(), x); + BOOST_TEST_EQ(b[0].value(), x); + BOOST_TEST_EQ(b[0].variance(), x); + b.add(0, weight(0.0)); + BOOST_TEST_EQ(b[0].value(), x); + BOOST_TEST_EQ(b[0].variance(), x); adaptive_storage c(std::size_t(1)); - c.increase_by_weight(0, a.value(0)); - BOOST_TEST_EQ(c.value(0), x); - BOOST_TEST_EQ(c.variance(0), x * x); + c.add(0, weight(a[0].value())); + BOOST_TEST_EQ(c[0].value(), x); + BOOST_TEST_EQ(c[0].variance(), x * x); } } @@ -379,20 +379,20 @@ int main() { adaptive_storage a(std::size_t(2)); a.increase(0); a *= 3; - BOOST_TEST_EQ(a.value(0), 3); - BOOST_TEST_EQ(a.variance(0), 9); - BOOST_TEST_EQ(a.value(1), 0); - BOOST_TEST_EQ(a.variance(1), 0); - a.add(1, 2.0, 5.0); - BOOST_TEST_EQ(a.value(0), 3); - BOOST_TEST_EQ(a.variance(0), 9); - BOOST_TEST_EQ(a.value(1), 2); - BOOST_TEST_EQ(a.variance(1), 5); + BOOST_TEST_EQ(a[0].value(), 3); + BOOST_TEST_EQ(a[0].variance(), 9); + BOOST_TEST_EQ(a[1].value(), 0); + BOOST_TEST_EQ(a[1].variance(), 0); + a.add(1, adaptive_storage::bin_type(2, 5)); + BOOST_TEST_EQ(a[0].value(), 3); + BOOST_TEST_EQ(a[0].variance(), 9); + BOOST_TEST_EQ(a[1].value(), 2); + BOOST_TEST_EQ(a[1].variance(), 5); a *= 3; - BOOST_TEST_EQ(a.value(0), 9); - BOOST_TEST_EQ(a.variance(0), 81); - BOOST_TEST_EQ(a.value(1), 6); - BOOST_TEST_EQ(a.variance(1), 45); + BOOST_TEST_EQ(a[0].value(), 9); + BOOST_TEST_EQ(a[0].variance(), 81); + BOOST_TEST_EQ(a[1].value(), 6); + BOOST_TEST_EQ(a[1].variance(), 45); } // convert_array_storage diff --git a/test/array_storage_test.cpp b/test/array_storage_test.cpp index 6d5be44d..eb634b6f 100644 --- a/test/array_storage_test.cpp +++ b/test/array_storage_test.cpp @@ -22,7 +22,7 @@ int main() { { array_storage a(std::size_t(1)); BOOST_TEST_EQ(a.size(), 1u); - BOOST_TEST_EQ(a.value(0), 0u); + BOOST_TEST_EQ(a[0], 0); } // increase @@ -35,11 +35,11 @@ int main() { c.increase(0); d.increase(0); d.add(1, 5); - BOOST_TEST_EQ(a.value(0), 1u); - BOOST_TEST_EQ(b.value(0), 1u); - BOOST_TEST_EQ(c.value(0), 2u); - BOOST_TEST_EQ(d.value(0), 1u); - BOOST_TEST_EQ(d.value(1), 5u); + BOOST_TEST_EQ(a[0], 1); + BOOST_TEST_EQ(b[0], 1); + BOOST_TEST_EQ(c[0], 2); + BOOST_TEST_EQ(d[0], 1); + BOOST_TEST_EQ(d[1], 5); BOOST_TEST(a == a); BOOST_TEST(a == b); BOOST_TEST(!(a == c)); @@ -51,14 +51,14 @@ int main() { array_storage a(std::size_t(2)); a.increase(0); a *= 3; - BOOST_TEST_EQ(a.value(0), 3.0); - BOOST_TEST_EQ(a.value(1), 0.0); + BOOST_TEST_EQ(a[0], 3); + BOOST_TEST_EQ(a[1], 0); a.add(1, 2); - BOOST_TEST_EQ(a.value(0), 3.0); - BOOST_TEST_EQ(a.value(1), 2.0); + BOOST_TEST_EQ(a[0], 3); + BOOST_TEST_EQ(a[1], 2); a *= 3; - BOOST_TEST_EQ(a.value(0), 9.0); - BOOST_TEST_EQ(a.value(1), 6.0); + BOOST_TEST_EQ(a[0], 9); + BOOST_TEST_EQ(a[1], 6); } // copy @@ -69,13 +69,13 @@ int main() { BOOST_TEST(!(a == b)); b = a; BOOST_TEST(a == b); - BOOST_TEST_EQ(b.size(), 1u); - BOOST_TEST_EQ(b.value(0), 1u); + BOOST_TEST_EQ(b.size(), 1); + BOOST_TEST_EQ(b[0], 1); decltype(a) c(a); BOOST_TEST(a == c); - BOOST_TEST_EQ(c.size(), 1u); - BOOST_TEST_EQ(c.value(0), 1u); + BOOST_TEST_EQ(c.size(), 1); + BOOST_TEST_EQ(c[0], 1); array_storage d(std::size_t(1)); BOOST_TEST(!(a == d)); @@ -92,13 +92,13 @@ int main() { decltype(a) b; BOOST_TEST(!(a == b)); b = std::move(a); - BOOST_TEST_EQ(a.size(), 0u); - BOOST_TEST_EQ(b.size(), 1u); - BOOST_TEST_EQ(b.value(0), 1u); + BOOST_TEST_EQ(a.size(), 0); + BOOST_TEST_EQ(b.size(), 1); + BOOST_TEST_EQ(b[0], 1); decltype(a) c(std::move(b)); - BOOST_TEST_EQ(c.size(), 1u); - BOOST_TEST_EQ(c.value(0), 1u); - BOOST_TEST_EQ(b.size(), 0u); + BOOST_TEST_EQ(c.size(), 1); + BOOST_TEST_EQ(c[0], 1); + BOOST_TEST_EQ(b.size(), 0); } // with weight_counter @@ -106,12 +106,12 @@ int main() { array_storage> a(std::size_t(1)); a.increase(0); a.add(0, 1); - a.add(0, 1, 0); - BOOST_TEST_EQ(a.value(0), 3); - BOOST_TEST_EQ(a.variance(0), 2); - a.increase_by_weight(0, 2); - BOOST_TEST_EQ(a.value(0), 5); - BOOST_TEST_EQ(a.variance(0), 6); + a.add(0, weight_counter(1, 0)); + BOOST_TEST_EQ(a[0].value(), 3); + BOOST_TEST_EQ(a[0].variance(), 2); + a.add(0, weight(2)); + BOOST_TEST_EQ(a[0].value(), 5); + BOOST_TEST_EQ(a[0].variance(), 6); } return boost::report_errors(); diff --git a/test/histogram_test.cpp b/test/histogram_test.cpp index 20eac642..d46c6363 100644 --- a/test/histogram_test.cpp +++ b/test/histogram_test.cpp @@ -164,14 +164,14 @@ template void run_tests() { decltype(h) h2(std::move(h)); // static axes cannot shrink to zero BOOST_TEST_EQ(h.dim(), expected_moved_from_dim(Type(), 2)); - BOOST_TEST_EQ(h.sum(), 0); + BOOST_TEST_EQ(h.sum().value(), 0); BOOST_TEST_EQ(h.bincount(), 0); BOOST_TEST_EQ(h2, href); decltype(h) h3; h3 = std::move(h2); // static axes cannot shrink to zero BOOST_TEST_EQ(h2.dim(), expected_moved_from_dim(Type(), 2)); - BOOST_TEST_EQ(h2.sum(), 0); + BOOST_TEST_EQ(h2.sum().value(), 0); BOOST_TEST_EQ(h2.bincount(), 0); BOOST_TEST_EQ(h3, href); } @@ -248,21 +248,21 @@ template void run_tests() { BOOST_TEST_EQ(h.dim(), 1); BOOST_TEST_EQ(h.axis(0_c).size(), 2); BOOST_TEST_EQ(h.axis(0_c).shape(), 4); - BOOST_TEST_EQ(h.sum(), 4); + BOOST_TEST_EQ(h.sum().value(), 4); - BOOST_TEST_THROWS(h.value(-2), std::out_of_range); - BOOST_TEST_EQ(h.value(-1), 1); - BOOST_TEST_EQ(h.value(0), 2); - BOOST_TEST_EQ(h.value(1), 0); - BOOST_TEST_EQ(h.value(2), 1); - BOOST_TEST_THROWS(h.value(3), std::out_of_range); + BOOST_TEST_THROWS(h.bin(-2).value(), std::out_of_range); + BOOST_TEST_EQ(h.bin(-1).value(), 1); + BOOST_TEST_EQ(h.bin(0).value(), 2); + BOOST_TEST_EQ(h.bin(1).value(), 0); + BOOST_TEST_EQ(h.bin(2).value(), 1); + BOOST_TEST_THROWS(h.bin(3).value(), std::out_of_range); - BOOST_TEST_THROWS(h.variance(-2), std::out_of_range); - BOOST_TEST_EQ(h.variance(-1), 1); - BOOST_TEST_EQ(h.variance(0), 2); - BOOST_TEST_EQ(h.variance(1), 0); - BOOST_TEST_EQ(h.variance(2), 1); - BOOST_TEST_THROWS(h.variance(3), std::out_of_range); + BOOST_TEST_THROWS(h.bin(-2).variance(), std::out_of_range); + BOOST_TEST_EQ(h.bin(-1).variance(), 1); + BOOST_TEST_EQ(h.bin(0).variance(), 2); + BOOST_TEST_EQ(h.bin(1).variance(), 0); + BOOST_TEST_EQ(h.bin(2).variance(), 1); + BOOST_TEST_THROWS(h.bin(3).variance(), std::out_of_range); } // d1_2 @@ -277,17 +277,17 @@ template void run_tests() { BOOST_TEST_EQ(h.dim(), 1); BOOST_TEST_EQ(h.axis(0_c).size(), 2); BOOST_TEST_EQ(h.axis(0_c).shape(), 2); - BOOST_TEST_EQ(h.sum(), 2); + BOOST_TEST_EQ(h.sum().value(), 2); - BOOST_TEST_THROWS(h.value(-1), std::out_of_range); - BOOST_TEST_EQ(h.value(0), 2); - BOOST_TEST_EQ(h.value(1), 0); - BOOST_TEST_THROWS(h.value(2), std::out_of_range); + BOOST_TEST_THROWS(h.bin(-1).value(), std::out_of_range); + BOOST_TEST_EQ(h.bin(0).value(), 2); + BOOST_TEST_EQ(h.bin(1).value(), 0); + BOOST_TEST_THROWS(h.bin(2).value(), std::out_of_range); - BOOST_TEST_THROWS(h.variance(-1), std::out_of_range); - BOOST_TEST_EQ(h.variance(0), 2); - BOOST_TEST_EQ(h.variance(1), 0); - BOOST_TEST_THROWS(h.variance(2), std::out_of_range); + BOOST_TEST_THROWS(h.bin(-1).variance(), std::out_of_range); + BOOST_TEST_EQ(h.bin(0).variance(), 2); + BOOST_TEST_EQ(h.bin(1).variance(), 0); + BOOST_TEST_THROWS(h.bin(2).variance(), std::out_of_range); } // d1_3 @@ -302,17 +302,17 @@ template void run_tests() { BOOST_TEST_EQ(h.dim(), 1); BOOST_TEST_EQ(h.axis(0_c).size(), 2); BOOST_TEST_EQ(h.axis(0_c).shape(), 2); - BOOST_TEST_EQ(h.sum(), 2); + BOOST_TEST_EQ(h.sum().value(), 2); - BOOST_TEST_THROWS(h.value(-1), std::out_of_range); - BOOST_TEST_EQ(h.value(0), 1); - BOOST_TEST_EQ(h.value(1), 1); - BOOST_TEST_THROWS(h.value(2), std::out_of_range); + BOOST_TEST_THROWS(h.bin(-1).value(), std::out_of_range); + BOOST_TEST_EQ(h.bin(0).value(), 1); + BOOST_TEST_EQ(h.bin(1).value(), 1); + BOOST_TEST_THROWS(h.bin(2).value(), std::out_of_range); - BOOST_TEST_THROWS(h.variance(-1), std::out_of_range); - BOOST_TEST_EQ(h.variance(0), 1); - BOOST_TEST_EQ(h.variance(1), 1); - BOOST_TEST_THROWS(h.variance(2), std::out_of_range); + BOOST_TEST_THROWS(h.bin(-1).variance(), std::out_of_range); + BOOST_TEST_EQ(h.bin(0).variance(), 1); + BOOST_TEST_EQ(h.bin(1).variance(), 1); + BOOST_TEST_THROWS(h.bin(2).variance(), std::out_of_range); } // d1w @@ -325,17 +325,17 @@ template void run_tests() { h.fill(-2); h.fill(10, weight(5)); - BOOST_TEST_EQ(h.sum(), 8.5); + BOOST_TEST_EQ(h.sum().value(), 8.5); - BOOST_TEST_EQ(h.value(-1), 1); - BOOST_TEST_EQ(h.value(0), 1.5); - BOOST_TEST_EQ(h.value(1), 1); - BOOST_TEST_EQ(h.value(2), 5); + BOOST_TEST_EQ(h.bin(-1).value(), 1); + BOOST_TEST_EQ(h.bin(0).value(), 1.5); + BOOST_TEST_EQ(h.bin(1).value(), 1); + BOOST_TEST_EQ(h.bin(2).value(), 5); - BOOST_TEST_EQ(h.variance(-1), 1); - BOOST_TEST_EQ(h.variance(0), 1.25); - BOOST_TEST_EQ(h.variance(1), 1); - BOOST_TEST_EQ(h.variance(2), 25); + BOOST_TEST_EQ(h.bin(-1).variance(), 1); + BOOST_TEST_EQ(h.bin(0).variance(), 1.25); + BOOST_TEST_EQ(h.bin(1).variance(), 1); + BOOST_TEST_EQ(h.bin(2).variance(), 25); } // d2 @@ -353,39 +353,39 @@ template void run_tests() { BOOST_TEST_EQ(h.axis(0_c).shape(), 4); BOOST_TEST_EQ(h.axis(1_c).size(), 3); BOOST_TEST_EQ(h.axis(1_c).shape(), 3); - BOOST_TEST_EQ(h.sum(), 3); + BOOST_TEST_EQ(h.sum().value(), 3); - BOOST_TEST_EQ(h.value(-1, 0), 0.0); - BOOST_TEST_EQ(h.value(-1, 1), 1.0); - BOOST_TEST_EQ(h.value(-1, 2), 0.0); + BOOST_TEST_EQ(h.bin(-1, 0).value(), 0.0); + BOOST_TEST_EQ(h.bin(-1, 1).value(), 1.0); + BOOST_TEST_EQ(h.bin(-1, 2).value(), 0.0); - BOOST_TEST_EQ(h.value(0, 0), 1.0); - BOOST_TEST_EQ(h.value(0, 1), 1.0); - BOOST_TEST_EQ(h.value(0, 2), 0.0); + BOOST_TEST_EQ(h.bin(0, 0).value(), 1.0); + BOOST_TEST_EQ(h.bin(0, 1).value(), 1.0); + BOOST_TEST_EQ(h.bin(0, 2).value(), 0.0); - BOOST_TEST_EQ(h.value(1, 0), 0.0); - BOOST_TEST_EQ(h.value(1, 1), 0.0); - BOOST_TEST_EQ(h.value(1, 2), 0.0); + BOOST_TEST_EQ(h.bin(1, 0).value(), 0.0); + BOOST_TEST_EQ(h.bin(1, 1).value(), 0.0); + BOOST_TEST_EQ(h.bin(1, 2).value(), 0.0); - BOOST_TEST_EQ(h.value(2, 0), 0.0); - BOOST_TEST_EQ(h.value(2, 1), 0.0); - BOOST_TEST_EQ(h.value(2, 2), 0.0); + BOOST_TEST_EQ(h.bin(2, 0).value(), 0.0); + BOOST_TEST_EQ(h.bin(2, 1).value(), 0.0); + BOOST_TEST_EQ(h.bin(2, 2).value(), 0.0); - BOOST_TEST_EQ(h.variance(-1, 0), 0.0); - BOOST_TEST_EQ(h.variance(-1, 1), 1.0); - BOOST_TEST_EQ(h.variance(-1, 2), 0.0); + BOOST_TEST_EQ(h.bin(-1, 0).variance(), 0.0); + BOOST_TEST_EQ(h.bin(-1, 1).variance(), 1.0); + BOOST_TEST_EQ(h.bin(-1, 2).variance(), 0.0); - BOOST_TEST_EQ(h.variance(0, 0), 1.0); - BOOST_TEST_EQ(h.variance(0, 1), 1.0); - BOOST_TEST_EQ(h.variance(0, 2), 0.0); + BOOST_TEST_EQ(h.bin(0, 0).variance(), 1.0); + BOOST_TEST_EQ(h.bin(0, 1).variance(), 1.0); + BOOST_TEST_EQ(h.bin(0, 2).variance(), 0.0); - BOOST_TEST_EQ(h.variance(1, 0), 0.0); - BOOST_TEST_EQ(h.variance(1, 1), 0.0); - BOOST_TEST_EQ(h.variance(1, 2), 0.0); + BOOST_TEST_EQ(h.bin(1, 0).variance(), 0.0); + BOOST_TEST_EQ(h.bin(1, 1).variance(), 0.0); + BOOST_TEST_EQ(h.bin(1, 2).variance(), 0.0); - BOOST_TEST_EQ(h.variance(2, 0), 0.0); - BOOST_TEST_EQ(h.variance(2, 1), 0.0); - BOOST_TEST_EQ(h.variance(2, 2), 0.0); + BOOST_TEST_EQ(h.bin(2, 0).variance(), 0.0); + BOOST_TEST_EQ(h.bin(2, 1).variance(), 0.0); + BOOST_TEST_EQ(h.bin(2, 2).variance(), 0.0); } // d2w @@ -398,39 +398,39 @@ template void run_tests() { h.fill(weight(5), -1, -10); // is ignored h.fill(weight(7), -10, 0); // -> -1, 1 - BOOST_TEST_EQ(h.sum(), 18); + BOOST_TEST_EQ(h.sum().value(), 18); - BOOST_TEST_EQ(h.value(-1, 0), 0.0); - BOOST_TEST_EQ(h.value(-1, 1), 7.0); - BOOST_TEST_EQ(h.value(-1, 2), 0.0); + BOOST_TEST_EQ(h.bin(-1, 0).value(), 0.0); + BOOST_TEST_EQ(h.bin(-1, 1).value(), 7.0); + BOOST_TEST_EQ(h.bin(-1, 2).value(), 0.0); - BOOST_TEST_EQ(h.value(0, 0), 10.0); - BOOST_TEST_EQ(h.value(0, 1), 1.0); - BOOST_TEST_EQ(h.value(0, 2), 0.0); + BOOST_TEST_EQ(h.bin(0, 0).value(), 10.0); + BOOST_TEST_EQ(h.bin(0, 1).value(), 1.0); + BOOST_TEST_EQ(h.bin(0, 2).value(), 0.0); - BOOST_TEST_EQ(h.value(1, 0), 0.0); - BOOST_TEST_EQ(h.value(1, 1), 0.0); - BOOST_TEST_EQ(h.value(1, 2), 0.0); + BOOST_TEST_EQ(h.bin(1, 0).value(), 0.0); + BOOST_TEST_EQ(h.bin(1, 1).value(), 0.0); + BOOST_TEST_EQ(h.bin(1, 2).value(), 0.0); - BOOST_TEST_EQ(h.value(2, 0), 0.0); - BOOST_TEST_EQ(h.value(2, 1), 0.0); - BOOST_TEST_EQ(h.value(2, 2), 0.0); + BOOST_TEST_EQ(h.bin(2, 0).value(), 0.0); + BOOST_TEST_EQ(h.bin(2, 1).value(), 0.0); + BOOST_TEST_EQ(h.bin(2, 2).value(), 0.0); - BOOST_TEST_EQ(h.variance(-1, 0), 0.0); - BOOST_TEST_EQ(h.variance(-1, 1), 49.0); - BOOST_TEST_EQ(h.variance(-1, 2), 0.0); + BOOST_TEST_EQ(h.bin(-1, 0).variance(), 0.0); + BOOST_TEST_EQ(h.bin(-1, 1).variance(), 49.0); + BOOST_TEST_EQ(h.bin(-1, 2).variance(), 0.0); - BOOST_TEST_EQ(h.variance(0, 0), 100.0); - BOOST_TEST_EQ(h.variance(0, 1), 1.0); - BOOST_TEST_EQ(h.variance(0, 2), 0.0); + BOOST_TEST_EQ(h.bin(0, 0).variance(), 100.0); + BOOST_TEST_EQ(h.bin(0, 1).variance(), 1.0); + BOOST_TEST_EQ(h.bin(0, 2).variance(), 0.0); - BOOST_TEST_EQ(h.variance(1, 0), 0.0); - BOOST_TEST_EQ(h.variance(1, 1), 0.0); - BOOST_TEST_EQ(h.variance(1, 2), 0.0); + BOOST_TEST_EQ(h.bin(1, 0).variance(), 0.0); + BOOST_TEST_EQ(h.bin(1, 1).variance(), 0.0); + BOOST_TEST_EQ(h.bin(1, 2).variance(), 0.0); - BOOST_TEST_EQ(h.variance(2, 0), 0.0); - BOOST_TEST_EQ(h.variance(2, 1), 0.0); - BOOST_TEST_EQ(h.variance(2, 2), 0.0); + BOOST_TEST_EQ(h.bin(2, 0).variance(), 0.0); + BOOST_TEST_EQ(h.bin(2, 1).variance(), 0.0); + BOOST_TEST_EQ(h.bin(2, 2).variance(), 0.0); } // d3w @@ -449,7 +449,7 @@ template void run_tests() { for (auto i = 0; i < h.axis(0_c).size(); ++i) { for (auto j = 0; j < h.axis(1_c).size(); ++j) { for (auto k = 0; k < h.axis(2_c).size(); ++k) { - BOOST_TEST_EQ(h.value(i, j, k), i + j + k); + BOOST_TEST_EQ(h.bin(i, j, k).value(), i + j + k); } } } @@ -464,18 +464,18 @@ template void run_tests() { b.fill(1); auto c = a; c += b; - BOOST_TEST_EQ(c.value(-1), 0); - BOOST_TEST_EQ(c.value(0), 1); - BOOST_TEST_EQ(c.value(1), 0); - BOOST_TEST_EQ(c.value(2), 1); - BOOST_TEST_EQ(c.value(3), 0); + BOOST_TEST_EQ(c.bin(-1).value(), 0); + BOOST_TEST_EQ(c.bin(0).value(), 1); + BOOST_TEST_EQ(c.bin(1).value(), 0); + BOOST_TEST_EQ(c.bin(2).value(), 1); + BOOST_TEST_EQ(c.bin(3).value(), 0); auto d = a; d += b; - BOOST_TEST_EQ(d.value(-1), 0); - BOOST_TEST_EQ(d.value(0), 1); - BOOST_TEST_EQ(d.value(1), 0); - BOOST_TEST_EQ(d.value(2), 1); - BOOST_TEST_EQ(d.value(3), 0); + BOOST_TEST_EQ(d.bin(-1).value(), 0); + BOOST_TEST_EQ(d.bin(0).value(), 1); + BOOST_TEST_EQ(d.bin(1).value(), 0); + BOOST_TEST_EQ(d.bin(2).value(), 1); + BOOST_TEST_EQ(d.bin(3).value(), 0); } // add_2 @@ -484,25 +484,25 @@ template void run_tests() { auto b = make_histogram(Type(), axis::integer<>(0, 2)); a.fill(0); - BOOST_TEST_EQ(a.variance(0), 1); + BOOST_TEST_EQ(a.bin(0).variance(), 1); b.fill(1, weight(3)); - BOOST_TEST_EQ(b.variance(1), 9); + BOOST_TEST_EQ(b.bin(1).variance(), 9); auto c = a; c += b; - BOOST_TEST_EQ(c.value(-1), 0); - BOOST_TEST_EQ(c.value(0), 1); - BOOST_TEST_EQ(c.variance(0), 1); - BOOST_TEST_EQ(c.value(1), 3); - BOOST_TEST_EQ(c.variance(1), 9); - BOOST_TEST_EQ(c.value(2), 0); + BOOST_TEST_EQ(c.bin(-1).value(), 0); + BOOST_TEST_EQ(c.bin(0).value(), 1); + BOOST_TEST_EQ(c.bin(0).variance(), 1); + BOOST_TEST_EQ(c.bin(1).value(), 3); + BOOST_TEST_EQ(c.bin(1).variance(), 9); + BOOST_TEST_EQ(c.bin(2).value(), 0); auto d = a; d += b; - BOOST_TEST_EQ(d.value(-1), 0); - BOOST_TEST_EQ(d.value(0), 1); - BOOST_TEST_EQ(d.variance(0), 1); - BOOST_TEST_EQ(d.value(1), 3); - BOOST_TEST_EQ(d.variance(1), 9); - BOOST_TEST_EQ(d.value(2), 0); + BOOST_TEST_EQ(d.bin(-1).value(), 0); + BOOST_TEST_EQ(d.bin(0).value(), 1); + BOOST_TEST_EQ(d.bin(0).variance(), 1); + BOOST_TEST_EQ(d.bin(1).value(), 3); + BOOST_TEST_EQ(d.bin(1).variance(), 9); + BOOST_TEST_EQ(d.bin(2).value(), 0); } // add_3 @@ -515,18 +515,18 @@ template void run_tests() { b.fill(1); auto c = a; c += b; - BOOST_TEST_EQ(c.value(-1), 0); - BOOST_TEST_EQ(c.value(0), 1); - BOOST_TEST_EQ(c.value(1), 0); - BOOST_TEST_EQ(c.value(2), 1); - BOOST_TEST_EQ(c.value(3), 0); + BOOST_TEST_EQ(c.bin(-1), 0); + BOOST_TEST_EQ(c.bin(0), 1); + BOOST_TEST_EQ(c.bin(1), 0); + BOOST_TEST_EQ(c.bin(2), 1); + BOOST_TEST_EQ(c.bin(3), 0); auto d = a; d += b; - BOOST_TEST_EQ(d.value(-1), 0); - BOOST_TEST_EQ(d.value(0), 1); - BOOST_TEST_EQ(d.value(1), 0); - BOOST_TEST_EQ(d.value(2), 1); - BOOST_TEST_EQ(d.value(3), 0); + BOOST_TEST_EQ(d.bin(-1), 0); + BOOST_TEST_EQ(d.bin(0), 1); + BOOST_TEST_EQ(d.bin(1), 0); + BOOST_TEST_EQ(d.bin(2), 1); + BOOST_TEST_EQ(d.bin(3), 0); } // bad_add @@ -539,8 +539,8 @@ template void run_tests() { // bad_index { auto a = make_histogram(Type(), axis::integer<>(0, 2)); - BOOST_TEST_THROWS(a.value(5), std::out_of_range); - BOOST_TEST_THROWS(a.variance(5), std::out_of_range); + BOOST_TEST_THROWS(a.bin(5).value(), std::out_of_range); + BOOST_TEST_THROWS(a.bin(5).variance(), std::out_of_range); } // functional programming @@ -548,7 +548,7 @@ template void run_tests() { auto v = std::vector{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; auto h = make_histogram(Type(), axis::integer<>(0, 10)); std::for_each(v.begin(), v.end(), [&h](int x) { h.fill(weight(2.0), x); }); - BOOST_TEST_EQ(h.sum(), 20.0); + BOOST_TEST_EQ(h.sum().value(), 20.0); } // operators @@ -558,30 +558,30 @@ template void run_tests() { a.fill(0); b.fill(1); auto c = a + b; - BOOST_TEST_EQ(c.value(0), 1); - BOOST_TEST_EQ(c.value(1), 1); + BOOST_TEST_EQ(c.bin(0).value(), 1); + BOOST_TEST_EQ(c.bin(1).value(), 1); c += b; - BOOST_TEST_EQ(c.value(0), 1); - BOOST_TEST_EQ(c.value(1), 2); + BOOST_TEST_EQ(c.bin(0).value(), 1); + BOOST_TEST_EQ(c.bin(1).value(), 2); auto d = a + b + c; - BOOST_TEST_EQ(d.value(0), 2); - BOOST_TEST_EQ(d.value(1), 3); + BOOST_TEST_EQ(d.bin(0).value(), 2); + BOOST_TEST_EQ(d.bin(1).value(), 3); auto e = 3 * a; auto f = b * 2; - BOOST_TEST_EQ(e.value(0), 3); - BOOST_TEST_EQ(e.value(1), 0); - BOOST_TEST_EQ(f.value(0), 0); - BOOST_TEST_EQ(f.value(1), 2); + BOOST_TEST_EQ(e.bin(0).value(), 3); + BOOST_TEST_EQ(e.bin(1).value(), 0); + BOOST_TEST_EQ(f.bin(0).value(), 0); + BOOST_TEST_EQ(f.bin(1).value(), 2); auto r = a; r += b; r += e; - BOOST_TEST_EQ(r.value(0), 4); - BOOST_TEST_EQ(r.value(1), 1); + BOOST_TEST_EQ(r.bin(0).value(), 4); + BOOST_TEST_EQ(r.bin(1).value(), 1); BOOST_TEST_EQ(r, a + b + 3 * a); auto s = r / 4; r /= 4; - BOOST_TEST_EQ(r.value(0), 1); - BOOST_TEST_EQ(r.value(1), 0.25); + BOOST_TEST_EQ(r.bin(0).value(), 1); + BOOST_TEST_EQ(r.bin(1).value(), 0.25); BOOST_TEST_EQ(r, s); } @@ -630,11 +630,11 @@ template void run_tests() { Type(), axis::integer<>(0, 2, "", axis::uoflow::off)); a.fill(0); a.fill(1); - BOOST_TEST_EQ(a.value(0), 1); - BOOST_TEST_EQ(a.value(1), 1); + BOOST_TEST_EQ(a.bin(0).value(), 1); + BOOST_TEST_EQ(a.bin(1).value(), 1); a.reset(); - BOOST_TEST_EQ(a.value(0), 0); - BOOST_TEST_EQ(a.value(1), 0); + BOOST_TEST_EQ(a.bin(0).value(), 0); + BOOST_TEST_EQ(a.bin(1).value(), 0); } // reduce @@ -649,19 +649,19 @@ template void run_tests() { auto h1_0 = h1.reduce_to(0_c); BOOST_TEST_EQ(h1_0.dim(), 1); - BOOST_TEST_EQ(h1_0.sum(), 5); - BOOST_TEST_EQ(h1_0.value(0), 2); - BOOST_TEST_EQ(h1_0.value(1), 3); + BOOST_TEST_EQ(h1_0.sum().value(), 5); + BOOST_TEST_EQ(h1_0.bin(0).value(), 2); + BOOST_TEST_EQ(h1_0.bin(1).value(), 3); BOOST_TEST_EQ(h1_0.axis()[0].lower(), 0); BOOST_TEST_EQ(h1_0.axis()[1].lower(), 1); BOOST_TEST(axis_equal(Type(), h1_0.axis(), h1.axis(0_c))); auto h1_1 = h1.reduce_to(1_c); BOOST_TEST_EQ(h1_1.dim(), 1); - BOOST_TEST_EQ(h1_1.sum(), 5); - BOOST_TEST_EQ(h1_1.value(0), 2); - BOOST_TEST_EQ(h1_1.value(1), 2); - BOOST_TEST_EQ(h1_1.value(2), 1); + BOOST_TEST_EQ(h1_1.sum().value(), 5); + BOOST_TEST_EQ(h1_1.bin(0).value(), 2); + BOOST_TEST_EQ(h1_1.bin(1).value(), 2); + BOOST_TEST_EQ(h1_1.bin(2).value(), 1); BOOST_TEST(axis_equal(Type(), h1_1.axis(), h1.axis(1_c))); auto h2 = make_histogram(Type(), axis::integer<>(0, 2), @@ -675,52 +675,52 @@ template void run_tests() { auto h2_0 = h2.reduce_to(0_c); BOOST_TEST_EQ(h2_0.dim(), 1); - BOOST_TEST_EQ(h2_0.sum(), 5); - BOOST_TEST_EQ(h2_0.value(0), 4); - BOOST_TEST_EQ(h2_0.value(1), 1); + BOOST_TEST_EQ(h2_0.sum().value(), 5); + BOOST_TEST_EQ(h2_0.bin(0).value(), 4); + BOOST_TEST_EQ(h2_0.bin(1).value(), 1); BOOST_TEST(axis_equal(Type(), h2_0.axis(), axis::integer<>(0, 2))); auto h2_1 = h2.reduce_to(1_c); BOOST_TEST_EQ(h2_1.dim(), 1); - BOOST_TEST_EQ(h2_1.sum(), 5); - BOOST_TEST_EQ(h2_1.value(0), 3); - BOOST_TEST_EQ(h2_1.value(1), 2); + BOOST_TEST_EQ(h2_1.sum().value(), 5); + BOOST_TEST_EQ(h2_1.bin(0).value(), 3); + BOOST_TEST_EQ(h2_1.bin(1).value(), 2); BOOST_TEST(axis_equal(Type(), h2_1.axis(), axis::integer<>(0, 3))); auto h2_2 = h2.reduce_to(2_c); BOOST_TEST_EQ(h2_2.dim(), 1); - BOOST_TEST_EQ(h2_2.sum(), 5); - BOOST_TEST_EQ(h2_2.value(0), 2); - BOOST_TEST_EQ(h2_2.value(1), 1); - BOOST_TEST_EQ(h2_2.value(2), 2); + BOOST_TEST_EQ(h2_2.sum().value(), 5); + BOOST_TEST_EQ(h2_2.bin(0).value(), 2); + BOOST_TEST_EQ(h2_2.bin(1).value(), 1); + BOOST_TEST_EQ(h2_2.bin(2).value(), 2); BOOST_TEST(axis_equal(Type(), h2_2.axis(), axis::integer<>(0, 4))); auto h2_01 = h2.reduce_to(0_c, 1_c); BOOST_TEST_EQ(h2_01.dim(), 2); - BOOST_TEST_EQ(h2_01.sum(), 5); - BOOST_TEST_EQ(h2_01.value(0, 0), 2); - BOOST_TEST_EQ(h2_01.value(0, 1), 2); - BOOST_TEST_EQ(h2_01.value(1, 0), 1); + BOOST_TEST_EQ(h2_01.sum().value(), 5); + BOOST_TEST_EQ(h2_01.bin(0, 0).value(), 2); + BOOST_TEST_EQ(h2_01.bin(0, 1).value(), 2); + BOOST_TEST_EQ(h2_01.bin(1, 0).value(), 1); BOOST_TEST(axis_equal(Type(), h2_01.axis(0_c), axis::integer<>(0, 2))); BOOST_TEST(axis_equal(Type(), h2_01.axis(1_c), axis::integer<>(0, 3))); auto h2_02 = h2.reduce_to(0_c, 2_c); BOOST_TEST_EQ(h2_02.dim(), 2); - BOOST_TEST_EQ(h2_02.sum(), 5); - BOOST_TEST_EQ(h2_02.value(0, 0), 2); - BOOST_TEST_EQ(h2_02.value(0, 1), 1); - BOOST_TEST_EQ(h2_02.value(0, 2), 1); - BOOST_TEST_EQ(h2_02.value(1, 2), 1); + BOOST_TEST_EQ(h2_02.sum().value(), 5); + BOOST_TEST_EQ(h2_02.bin(0, 0).value(), 2); + BOOST_TEST_EQ(h2_02.bin(0, 1).value(), 1); + BOOST_TEST_EQ(h2_02.bin(0, 2).value(), 1); + BOOST_TEST_EQ(h2_02.bin(1, 2).value(), 1); BOOST_TEST(axis_equal(Type(), h2_02.axis(0_c), axis::integer<>(0, 2))); BOOST_TEST(axis_equal(Type(), h2_02.axis(1_c), axis::integer<>(0, 4))); auto h2_12 = h2.reduce_to(1_c, 2_c); BOOST_TEST_EQ(h2_12.dim(), 2); - BOOST_TEST_EQ(h2_12.sum(), 5); - BOOST_TEST_EQ(h2_12.value(0, 0), 1); - BOOST_TEST_EQ(h2_12.value(1, 0), 1); - BOOST_TEST_EQ(h2_12.value(1, 1), 1); - BOOST_TEST_EQ(h2_12.value(0, 2), 2); + BOOST_TEST_EQ(h2_12.sum().value(), 5); + BOOST_TEST_EQ(h2_12.bin(0, 0).value(), 1); + BOOST_TEST_EQ(h2_12.bin(1, 0).value(), 1); + BOOST_TEST_EQ(h2_12.bin(1, 1).value(), 1); + BOOST_TEST_EQ(h2_12.bin(0, 2).value(), 2); BOOST_TEST(axis_equal(Type(), h2_12.axis(0_c), axis::integer<>(0, 3))); BOOST_TEST(axis_equal(Type(), h2_12.axis(1_c), axis::integer<>(0, 4))); } @@ -746,9 +746,9 @@ template void run_tests() { BOOST_TEST_EQ(h.dim(), 1); BOOST_TEST(h.axis() == custom_axis(0, 3)); - BOOST_TEST_EQ(h.value(0), 1); - BOOST_TEST_EQ(h.value(1), 1); - BOOST_TEST_EQ(h.value(2), 0); + BOOST_TEST_EQ(h.bin(0).value(), 1); + BOOST_TEST_EQ(h.bin(1).value(), 1); + BOOST_TEST_EQ(h.bin(2).value(), 0); } // value iterator @@ -763,33 +763,33 @@ template void run_tests() { BOOST_TEST_EQ(it.idx(0), 0); BOOST_TEST_EQ(it.idx(1), 0); - BOOST_TEST_EQ(*it, 1); - BOOST_TEST_EQ(h.value(it.idx(0), it.idx(1)), *it); + BOOST_TEST_EQ(it->value(), 1); + BOOST_TEST_EQ(h.bin(it.idx(0), it.idx(1)).value(), it->value()); ++it; BOOST_TEST_EQ(it.idx(0), 1); BOOST_TEST_EQ(it.idx(1), 0); - BOOST_TEST_EQ(*it, 1); - BOOST_TEST_EQ(h.value(it.idx(0), it.idx(1)), *it); + BOOST_TEST_EQ(it->value(), 1); + BOOST_TEST_EQ(h.bin(it.idx(0), it.idx(1)).value(), it->value()); ++it; BOOST_TEST_EQ(it.idx(0), 2); BOOST_TEST_EQ(it.idx(1), 0); - BOOST_TEST_EQ(*it, 0); - BOOST_TEST_EQ(h.value(it.idx(0), it.idx(1)), *it); + BOOST_TEST_EQ(it->value(), 0); + BOOST_TEST_EQ(h.bin(it.idx(0), it.idx(1)).value(), it->value()); ++it; BOOST_TEST_EQ(it.idx(0), 0); BOOST_TEST_EQ(it.idx(1), 1); - BOOST_TEST_EQ(*it, 0); - BOOST_TEST_EQ(h.value(it.idx(0), it.idx(1)), *it); + BOOST_TEST_EQ(it->value(), 0); + BOOST_TEST_EQ(h.bin(it.idx(0), it.idx(1)).value(), it->value()); ++it; BOOST_TEST_EQ(it.idx(0), 1); BOOST_TEST_EQ(it.idx(1), 1); - BOOST_TEST_EQ(*it, 1); - BOOST_TEST_EQ(h.value(it.idx(0), it.idx(1)), *it); + BOOST_TEST_EQ(it->value(), 1); + BOOST_TEST_EQ(h.bin(it.idx(0), it.idx(1)).value(), it->value()); ++it; BOOST_TEST_EQ(it.idx(0), 2); BOOST_TEST_EQ(it.idx(1), 1); - BOOST_TEST_EQ(*it, 0); - BOOST_TEST_EQ(h.value(it.idx(0), it.idx(1)), *it); + BOOST_TEST_EQ(it->value(), 0); + BOOST_TEST_EQ(h.bin(it.idx(0), it.idx(1)).value(), it->value()); ++it; BOOST_TEST(it == h.end()); } @@ -860,9 +860,9 @@ int main() { h.fill(v.begin(), v.end()); auto i = std::vector(2); i = {0, 0}; - BOOST_TEST_EQ(h.value(i.begin(), i.end()), 1); + BOOST_TEST_EQ(h.bin(i.begin(), i.end()).value(), 1); i = {1, 1}; - BOOST_TEST_EQ(h.variance(i.begin(), i.end()), 1); + BOOST_TEST_EQ(h.bin(i.begin(), i.end()).variance(), 1); } // axis methods @@ -885,17 +885,17 @@ int main() { auto h1_0 = h1.reduce_to(0); BOOST_TEST_EQ(h1_0.dim(), 1); - BOOST_TEST_EQ(h1_0.sum(), 5); - BOOST_TEST_EQ(h1_0.value(0), 2); - BOOST_TEST_EQ(h1_0.value(1), 3); + BOOST_TEST_EQ(h1_0.sum().value(), 5); + BOOST_TEST_EQ(h1_0.bin(0).value(), 2); + BOOST_TEST_EQ(h1_0.bin(1).value(), 3); BOOST_TEST(axis_equal(dynamic_tag(), h1_0.axis(), h1.axis(0_c))); auto h1_1 = h1.reduce_to(1); BOOST_TEST_EQ(h1_1.dim(), 1); - BOOST_TEST_EQ(h1_1.sum(), 5); - BOOST_TEST_EQ(h1_1.value(0), 2); - BOOST_TEST_EQ(h1_1.value(1), 2); - BOOST_TEST_EQ(h1_1.value(2), 1); + BOOST_TEST_EQ(h1_1.sum().value(), 5); + BOOST_TEST_EQ(h1_1.bin(0).value(), 2); + BOOST_TEST_EQ(h1_1.bin(1).value(), 2); + BOOST_TEST_EQ(h1_1.bin(2).value(), 1); BOOST_TEST(axis_equal(dynamic_tag(), h1_1.axis(), h1.axis(1_c))); } diff --git a/test/meta_test.cpp b/test/meta_test.cpp index 54051d30..b6cb9cca 100644 --- a/test/meta_test.cpp +++ b/test/meta_test.cpp @@ -18,11 +18,11 @@ int main() { } struct no_variance_method { - using value_type = int; + using bin_type = int; }; + struct variance_method { - using value_type = int; - value_type variance(std::size_t) const; + struct bin_type { double variance() const; }; }; BOOST_TEST_EQ(typename has_variance_support::type(), diff --git a/test/python_suite_test.py b/test/python_suite_test.py index 38b50cba..0b3be014 100644 --- a/test/python_suite_test.py +++ b/test/python_suite_test.py @@ -419,9 +419,9 @@ class test_histogram(unittest.TestCase): self.assertEqual(h1.axis(0).shape, 5) for h in (h0, h1): - self.assertEqual(h.value(0), 2) - self.assertEqual(h.value(1), 1) - self.assertEqual(h.value(2), 3) + self.assertEqual(h[0], 2) + self.assertEqual(h[1], 1) + self.assertEqual(h[2], 3) with self.assertRaises(RuntimeError): h.value(0, 1) with self.assertRaises(RuntimeError): @@ -435,7 +435,7 @@ class test_histogram(unittest.TestCase): h.variance(0, foo=None) self.assertEqual(h1.value(-1), 1) - self.assertEqual(h1.value(3), 1) + self.assertEqual(h1[3], 1) def test_growth(self): h = histogram(integer(-1, 2)) @@ -448,10 +448,10 @@ class test_histogram(unittest.TestCase): for i in range(1000-256): h.fill(0) self.assertEqual(h.value(-1), 0) - self.assertEqual(h.value(0), 1) - self.assertEqual(h.value(1), 1000) - self.assertEqual(h.value(2), 2) - self.assertEqual(h.value(3), 0) + self.assertEqual(h[0], 1) + self.assertEqual(h[1], 1000) + self.assertEqual(h[2], 2) + self.assertEqual(h[3], 0) def test_fill_2d(self): for uoflow in (False, True): @@ -574,11 +574,11 @@ class test_histogram(unittest.TestCase): h.fill(-1) h.fill(2) self.assertEqual(h.value(-1), 1) - self.assertEqual(h.value(3), 1) + self.assertEqual(h[3], 1) with self.assertRaises(IndexError): h.value(-2) with self.assertRaises(IndexError): - h.value(4) + h[4] with self.assertRaises(IndexError): h.variance(-2) with self.assertRaises(IndexError): @@ -588,15 +588,15 @@ class test_histogram(unittest.TestCase): h = histogram(integer(0, 2)) h.fill(0) h += h - self.assertEqual(h.value(0), 2) + self.assertEqual(h[0], 2) self.assertEqual(h.variance(0), 2) - self.assertEqual(h.value(1), 0) + self.assertEqual(h[1], 0) h *= 2 - self.assertEqual(h.value(0), 4) + self.assertEqual(h[0], 4) self.assertEqual(h.variance(0), 8) - self.assertEqual(h.value(1), 0) - self.assertEqual((h + h).value(0), (2 * h).value(0)) - self.assertEqual((h + h).value(0), (h * 2).value(0)) + self.assertEqual(h[1], 0) + self.assertEqual((h + h)[0], (2 * h)[0]) + self.assertEqual((h + h)[0], (h * 2)[0]) self.assertNotEqual((h + h).variance(0), (2 * h).variance(0)) self.assertNotEqual((h + h).variance(0), (h * 2).variance(0)) h2 = histogram(regular(2, 0, 2)) @@ -825,9 +825,9 @@ class test_histogram(unittest.TestCase): a = histogram(integer(0, 3, uoflow=False)) a.fill(ar(-1, 0, 1, 2, 1)) a.fill((4, -1, 0, 1, 2)) - self.assertEqual(a.value(0), 2) - self.assertEqual(a.value(1), 3) - self.assertEqual(a.value(2), 2) + self.assertEqual(a[0], 2) + self.assertEqual(a[1], 3) + self.assertEqual(a[2], 2) with self.assertRaises(ValueError): a.fill(numpy.empty((2, 2))) @@ -850,9 +850,9 @@ class test_histogram(unittest.TestCase): a = histogram(integer(0, 3, uoflow=False)) a.fill(ar(0, 0, 1, 2, 1, 0, 2, 2)) - self.assertEqual(a.value(0), 3) - self.assertEqual(a.value(1), 2) - self.assertEqual(a.value(2), 3) + self.assertEqual(a[0], 3) + self.assertEqual(a[1], 2) + self.assertEqual(a[2], 3) @unittest.skipUnless(have_numpy, "requires build with numpy-support") @@ -864,18 +864,18 @@ class test_histogram(unittest.TestCase): a.fill(v, weight=w) a.fill((0, 1), weight=(2, 3)) self.assertEqual(a.value(-1), 2) - self.assertEqual(a.value(0), 5) - self.assertEqual(a.value(1), 7) - self.assertEqual(a.value(2), 5) + self.assertEqual(a[0], 5) + self.assertEqual(a[1], 7) + self.assertEqual(a[2], 5) self.assertEqual(a.variance(-1), 4) self.assertEqual(a.variance(0), 13) self.assertEqual(a.variance(1), 25) self.assertEqual(a.variance(2), 25) a.fill((1, 2), weight=1) a.fill(0, weight=(1, 2)) - self.assertEqual(a.value(0), 8) - self.assertEqual(a.value(1), 8) - self.assertEqual(a.value(2), 6) + self.assertEqual(a[0], 8) + self.assertEqual(a[1], 8) + self.assertEqual(a[2], 6) with self.assertRaises(RuntimeError): a.fill((1, 2), foo=(1, 1)) @@ -898,9 +898,9 @@ class test_histogram(unittest.TestCase): a = histogram(integer(0, 3, uoflow=False)) a.fill((0, 0, 1, 2)) a.fill((1, 0, 2, 2)) - self.assertEqual(a.value(0), 3) - self.assertEqual(a.value(1), 2) - self.assertEqual(a.value(2), 3) + self.assertEqual(a[0], 3) + self.assertEqual(a[1], 2) + self.assertEqual(a[2], 3) if __name__ == "__main__": unittest.main() diff --git a/test/weight_counter_test.cpp b/test/weight_counter_test.cpp index 9e22eff8..f66560bc 100644 --- a/test/weight_counter_test.cpp +++ b/test/weight_counter_test.cpp @@ -20,6 +20,7 @@ std::ostream &operator<<(std::ostream &os, const weight_counter &w) { int main() { using weight_counter = boost::histogram::weight_counter; + using boost::histogram::weight; weight_counter w(1); BOOST_TEST_EQ(w, weight_counter(1)); @@ -29,23 +30,23 @@ int main() { BOOST_TEST_NE(2, w); BOOST_TEST_NE(w, 2); - w.increase_by_weight(2); + w += weight(2); BOOST_TEST_EQ(w.value(), 3); BOOST_TEST_EQ(w.variance(), 5); // consistency: a weighted counter increased by weight 1 multiplied // by 2 must be the same as a weighted counter increased by weight 2 weight_counter u(0); - u.increase_by_weight(1); + u += weight(1); u *= 2; BOOST_TEST_EQ(u, weight_counter(2, 4)); weight_counter v(0); - v.increase_by_weight(2); + v += weight(2); BOOST_TEST_EQ(u, v); weight_counter x(0); - x.increase_by_count(2); + x += 2; BOOST_TEST_EQ(x, weight_counter(2, 2)); return boost::report_errors();