diff --git a/include/boost/histogram/algorithm/reduce.hpp b/include/boost/histogram/algorithm/reduce.hpp index c342d6aa..8d007529 100644 --- a/include/boost/histogram/algorithm/reduce.hpp +++ b/include/boost/histogram/algorithm/reduce.hpp @@ -405,6 +405,9 @@ Histogram reduce(const Histogram& hist, const Iterable& options) { auto result = Histogram(std::move(axes), detail::make_default(unsafe_access::storage(hist))); + for (auto& o : opts) + o.reduced_end = (o.end.index - o.begin.index) / static_cast(o.merge); + auto idx = detail::make_stack_buffer(unsafe_access::axes(result)); for (auto&& x : indexed(hist, coverage::all)) { auto i = idx.begin(); @@ -417,14 +420,12 @@ Histogram reduce(const Histogram& hist, const Iterable& options) { *i = -1; if (!o->use_underflow_bin) skip = true; } else { - if (*i >= 0) + if (*i < 0) + *i = o->reduced_end; + else if (o->merge > 1) *i /= static_cast(o->merge); - else - *i = o->end.index; - const auto reduced_axis_end = - (o->end.index - o->begin.index) / static_cast(o->merge); - if (*i >= reduced_axis_end) { - *i = reduced_axis_end; + if (*i >= o->reduced_end) { + *i = o->reduced_end; if (!o->use_overflow_bin) skip = true; } } diff --git a/include/boost/histogram/detail/reduce_command.hpp b/include/boost/histogram/detail/reduce_command.hpp index 3fb67b50..037b28f1 100644 --- a/include/boost/histogram/detail/reduce_command.hpp +++ b/include/boost/histogram/detail/reduce_command.hpp @@ -33,6 +33,7 @@ struct reduce_command { unsigned merge = 0; // default value indicates unset option bool crop = false; // for internal use by the reduce algorithm + axis::index_type reduced_end = 0; // (end - begin) / merge bool is_ordered = true; bool use_underflow_bin = true; bool use_overflow_bin = true;