/usr/include/boost/histogram/algorithm
Edit: /usr/include/boost/histogram/algorithm/project.hpp (3672B)
// Copyright 2015-2018 Hans Dembinski
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_HISTOGRAM_ALGORITHM_PROJECT_HPP
#define BOOST_HISTOGRAM_ALGORITHM_PROJECT_HPP
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace boost {
namespace histogram {
namespace algorithm {
/**
Returns a lower-dimensional histogram, summing over removed axes.
Arguments are the source histogram and compile-time numbers, the remaining indices of
the axes. Returns a new histogram which only contains the subset of axes. The source
histogram is summed over the removed axes.
*/
template
auto project(const histogram& h, std::integral_constant, Ns...) {
using LN = mp11::mp_list, Ns...>;
static_assert(mp11::mp_is_set::value, "indices must be unique");
const auto& old_axes = unsafe_access::axes(h);
auto axes = detail::static_if>(
[&](const auto& old_axes) {
return std::make_tuple(std::get(old_axes), std::get(old_axes)...);
},
[&](const auto& old_axes) {
return std::decay_t({old_axes[N], old_axes[Ns::value]...});
},
old_axes);
const auto& old_storage = unsafe_access::storage(h);
using A2 = decltype(axes);
auto result = histogram(std::move(axes), detail::make_default(old_storage));
auto idx = detail::make_stack_buffer(unsafe_access::axes(result));
for (auto&& x : indexed(h, coverage::all)) {
auto i = idx.begin();
mp11::mp_for_each([&i, &x](auto J) { *i++ = x.index(J); });
result.at(idx) += *x;
}
return result;
}
/**
Returns a lower-dimensional histogram, summing over removed axes.
This version accepts a source histogram and an iterable range containing the remaining
indices.
*/
template >
auto project(const histogram& h, const Iterable& c) {
using namespace boost::mp11;
const auto& old_axes = unsafe_access::axes(h);
// axes is always std::vector<...>, even if A is tuple
auto axes = detail::make_empty_dynamic_axes(old_axes);
axes.reserve(c.size());
auto seen = detail::make_stack_buffer(old_axes, false);
for (auto d : c) {
if (static_cast(d) >= h.rank())
BOOST_THROW_EXCEPTION(std::invalid_argument("invalid axis index"));
if (seen[d]) BOOST_THROW_EXCEPTION(std::invalid_argument("indices are not unique"));
seen[d] = true;
axes.emplace_back(detail::axis_get(old_axes, d));
}
const auto& old_storage = unsafe_access::storage(h);
auto result =
histogram(std::move(axes), detail::make_default(old_storage));
auto idx = detail::make_stack_buffer(unsafe_access::axes(result));
for (auto&& x : indexed(h, coverage::all)) {
auto i = idx.begin();
for (auto d : c) *i++ = x.index(d);
result.at(idx) += *x;
}
return result;
}
} // namespace algorithm
} // namespace histogram
} // namespace boost
#endif