]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/histogram/examples/guide_histogram_operators.cpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / libs / histogram / examples / guide_histogram_operators.cpp
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
7 //[ guide_histogram_operators
8
9 #include <boost/histogram.hpp>
10 #include <cassert>
11 #include <vector>
12
13 int main() {
14 using namespace boost::histogram;
15
16 // make two histograms
17 auto h1 = make_histogram(axis::regular<>(2, -1.0, 1.0));
18 auto h2 = make_histogram(axis::regular<>(2, -1.0, 1.0));
19
20 h1(-0.5); // counts are: 1 0
21 h2(0.5); // counts are: 0 1
22
23 // add them
24 auto h3 = h1;
25 h3 += h2; // counts are: 1 1
26
27 // adding multiple histograms at once is likely to be optimized by the compiler so that
28 // superfluous temporaries avoided, but no guarantees are given; use this equivalent
29 // code when you want to make sure: h4 = h1; h4 += h2; h4 += h3;
30 auto h4 = h1 + h2 + h3; // counts are: 2 2
31
32 assert(h4.at(0) == 2 && h4.at(1) == 2);
33
34 // multiply by number, h4 *= 2 also works
35 auto h5 = h4 * 2; // counts are: 4 4
36
37 // divide by number; s4 /= 4 also works
38 auto h6 = h5 / 4; // counts are: 1 1
39
40 assert(h6.at(0) == 1 && h6.at(1) == 1);
41 assert(h6 != h5 && h5 == 4 * h6);
42
43 // note the special effect of multiplication on weight_storage
44 auto h = make_histogram_with(weight_storage(), axis::regular<>(2, -1.0, 1.0));
45 h(-0.5);
46
47 // counts are: 1 0
48 assert(h.at(0).value() == 1 && h.at(1).value() == 0);
49
50 auto h_sum = h + h;
51 auto h_mul = 2 * h;
52
53 // values are the same as expected...
54 assert(h_sum.at(0).value() == h_mul.at(0).value());
55 // ... but variances differ
56 assert(h_sum.at(0).variance() == 2 && h_mul.at(0).variance() == 4);
57
58 // equality operator checks variances, so histograms are not equal
59 assert(h_sum != h_mul);
60 }
61
62 //]