diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/histogram/examples/guide_histogram_projection.cpp | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/histogram/examples/guide_histogram_projection.cpp')
-rw-r--r-- | src/boost/libs/histogram/examples/guide_histogram_projection.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/boost/libs/histogram/examples/guide_histogram_projection.cpp b/src/boost/libs/histogram/examples/guide_histogram_projection.cpp new file mode 100644 index 00000000..54f45212 --- /dev/null +++ b/src/boost/libs/histogram/examples/guide_histogram_projection.cpp @@ -0,0 +1,58 @@ +// 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) + +//[ guide_histogram_projection + +#include <boost/histogram.hpp> +#include <cassert> +#include <iostream> +#include <sstream> + +int main() { + using namespace boost::histogram; + using namespace literals; // enables _c suffix + + // make a 2d histogram + auto h = make_histogram(axis::regular<>(3, -1.0, 1.0), axis::integer<>(0, 2)); + + h(-0.9, 0); + h(0.9, 1); + h(0.1, 0); + + auto hr0 = algorithm::project(h, 0_c); // keep only first axis + auto hr1 = algorithm::project(h, 1_c); // keep only second axis + + // reduce does not remove counts; returned histograms are summed over + // the removed axes, so h, hr0, and hr1 have same number of total counts; + // we compute the sum of counts with the sum algorithm + assert(algorithm::sum(h) == 3 && algorithm::sum(hr0) == 3 && algorithm::sum(hr1) == 3); + + std::ostringstream os1; + for (auto&& x : indexed(h)) + os1 << "(" << x.index(0) << ", " << x.index(1) << "): " << *x << "\n"; + std::cout << os1.str() << std::flush; + assert(os1.str() == "(0, 0): 1\n" + "(1, 0): 1\n" + "(2, 0): 0\n" + "(0, 1): 0\n" + "(1, 1): 0\n" + "(2, 1): 1\n"); + + std::ostringstream os2; + for (auto&& x : indexed(hr0)) os2 << "(" << x.index(0) << ", -): " << *x << "\n"; + std::cout << os2.str() << std::flush; + assert(os2.str() == "(0, -): 1\n" + "(1, -): 1\n" + "(2, -): 1\n"); + + std::ostringstream os3; + for (auto&& x : indexed(hr1)) os3 << "(- ," << x.index(0) << "): " << *x << "\n"; + std::cout << os3.str() << std::flush; + assert(os3.str() == "(- ,0): 2\n" + "(- ,1): 1\n"); +} + +//] |