]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/histogram/examples/guide_histogram_operators.cpp
1 // Copyright 2015-2018 Hans Dembinski
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)
7 //[ guide_histogram_operators
9 #include <boost/histogram.hpp>
14 using namespace boost::histogram
;
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));
20 h1(-0.5); // counts are: 1 0
21 h2(0.5); // counts are: 0 1
25 h3
+= h2
; // counts are: 1 1
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
32 assert(h4
.at(0) == 2 && h4
.at(1) == 2);
34 // multiply by number, h4 *= 2 also works
35 auto h5
= h4
* 2; // counts are: 4 4
37 // divide by number; s4 /= 4 also works
38 auto h6
= h5
/ 4; // counts are: 1 1
40 assert(h6
.at(0) == 1 && h6
.at(1) == 1);
41 assert(h6
!= h5
&& h5
== 4 * h6
);
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));
48 assert(h
.at(0).value() == 1 && h
.at(1).value() == 0);
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);
58 // equality operator checks variances, so histograms are not equal
59 assert(h_sum
!= h_mul
);