• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
2 // unit/quantity manipulation and conversion
3 //
4 // Copyright (C) 2014 Erik Erlandson
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 
10 //#include <boost/units/systems/information.hpp>
11 
12 /**
13 \file
14 
15 \brief information.cpp
16 
17 \details
18 Demonstrate information unit system.
19 
20 Output:
21 @verbatim
22 bytes= 1.25e+08 B
23 bits= 8e+06 b
24 nats= 4605.17 nat
25 1024 bytes in a kibi-byte
26 8.38861e+06 bits in a mebi-byte
27 0.000434294 hartleys in a milli-nat
28 entropy in bits= 1 b
29 entropy in nats= 0.693147 nat
30 entropy in hartleys= 0.30103 Hart
31 entropy in shannons= 1 Sh
32 entropy in bytes= 0.125 B
33 @endverbatim
34 **/
35 
36 #include <cmath>
37 #include <iostream>
38 using std::cout;
39 using std::endl;
40 using std::log;
41 
42 #include <boost/units/quantity.hpp>
43 #include <boost/units/io.hpp>
44 #include <boost/units/conversion.hpp>
45 namespace bu = boost::units;
46 using bu::quantity;
47 using bu::conversion_factor;
48 
49 // SI prefixes
50 #include <boost/units/systems/si/prefixes.hpp>
51 namespace si = boost::units::si;
52 
53 // information unit system
54 #include <boost/units/systems/information.hpp>
55 using namespace bu::information;
56 
57 // Define a function for the entropy of a bernoulli trial.
58 // The formula is computed using natural log, so the units are in nats.
59 // The user provides the desired return unit, the only restriction being that it
60 // must be a unit of information.  Conversion to the requested return unit is
61 // accomplished automatically by the boost::units library.
62 template <typename Sys>
63 constexpr
64 quantity<bu::unit<bu::information_dimension, Sys> >
bernoulli_entropy(double p,const bu::unit<bu::information_dimension,Sys> &)65 bernoulli_entropy(double p, const bu::unit<bu::information_dimension, Sys>&) {
66     typedef bu::unit<bu::information_dimension, Sys> requested_unit;
67     return quantity<requested_unit>((-(p*log(p) + (1-p)*log(1-p)))*nats);
68 }
69 
main(int argc,char ** argv)70 int main(int argc, char** argv) {
71     // a quantity of information (default in units of bytes)
72     quantity<info> nbytes(1 * si::giga * bit);
73     cout << "bytes= " << nbytes << endl;
74 
75     // a quantity of information, stored as bits
76     quantity<hu::bit::info> nbits(1 * si::mega * byte);
77     cout << "bits= " << nbits << endl;
78 
79     // a quantity of information, stored as nats
80     quantity<hu::nat::info> nnats(2 * si::kilo * hartleys);
81     cout << "nats= " << nnats << endl;
82 
83     // how many bytes are in a kibi-byte?
84     cout << conversion_factor(kibi * byte, byte) << " bytes in a kibi-byte" << endl;
85 
86     // how many bits are in a mebi-byte?
87     cout << conversion_factor(mebi * byte, bit) << " bits in a mebi-byte" << endl;
88 
89     // how many hartleys are in a milli-nat?
90     cout << conversion_factor(si::milli * nat, hartley) << " hartleys in a milli-nat" << endl;
91 
92     // compute the entropy of a fair coin flip, in various units of information:
93     cout << "entropy in bits= " << bernoulli_entropy(0.5, bits) << endl;
94     cout << "entropy in nats= " << bernoulli_entropy(0.5, nats) << endl;
95     cout << "entropy in hartleys= " << bernoulli_entropy(0.5, hartleys) << endl;
96     cout << "entropy in shannons= " << bernoulli_entropy(0.5, shannons) << endl;
97     cout << "entropy in bytes= " << bernoulli_entropy(0.5, bytes) << endl;
98 
99     return 0;
100 }
101