Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / histogram / examples / guide_axis_with_transform.cpp
1 // Copyright 2019 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_axis_with_transform
8
9 #include <boost/histogram/axis/regular.hpp>
10 #include <limits>
11
12 int main() {
13   using namespace boost::histogram;
14
15   // make a regular axis with a log transform over [10, 100), [100, 1000), [1000, 10000)
16   axis::regular<double, axis::transform::log> r_log{3, 10., 10000.};
17   // log transform:
18   // - useful when values vary dramatically in magnitude, like brightness of stars
19   // - edges are not exactly at 10, 100, 1000, because of finite floating point precision
20   // - values >= 0 but smaller than the starting value of the axis are mapped to -1
21   // - values < 0 are mapped to `size()`, because the result of std::log(value) is NaN
22   assert(r_log.index(10.1) == 0);
23   assert(r_log.index(100.1) == 1);
24   assert(r_log.index(1000.1) == 2);
25   assert(r_log.index(1) == -1);
26   assert(r_log.index(0) == -1);
27   assert(r_log.index(-1) == 3);
28
29   // make a regular axis with a sqrt transform over [4, 9), [9, 16), [16, 25)
30   axis::regular<double, axis::transform::sqrt> r_sqrt{3, 4., 25.};
31   // sqrt transform:
32   // - bin widths are more mildly increasing compared to log transform
33   // - axis starting at value == 0 is ok, sqrt(0) == 0 unlike log transform
34   // - values < 0 are mapped to `size()`, because the result of std::sqrt(value) is NaN
35   assert(r_sqrt.index(0) == -1);
36   assert(r_sqrt.index(4.1) == 0);
37   assert(r_sqrt.index(9.1) == 1);
38   assert(r_sqrt.index(16.1) == 2);
39   assert(r_sqrt.index(25.1) == 3);
40   assert(r_sqrt.index(-1) == 3);
41
42   // make a regular axis with a power transform x^1/3 over [1, 8), [8, 27), [27, 64)
43   using pow_trans = axis::transform::pow;
44   axis::regular<double, pow_trans> r_pow(pow_trans{1. / 3.}, 3, 1., 64.);
45   // pow transform:
46   // - generalization of the sqrt transform
47   // - starting the axis at value == 0 is ok for power p > 0, 0^p == 0 for p > 0
48   // - values < 0 are mapped to `size()` if power p is not a positive integer
49   assert(r_pow.index(0) == -1);
50   assert(r_pow.index(1.1) == 0);
51   assert(r_pow.index(8.1) == 1);
52   assert(r_pow.index(27.1) == 2);
53   assert(r_pow.index(64.1) == 3);
54   assert(r_pow.index(-1) == 3);
55 }
56
57 //]