information.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. //#include <boost/units/systems/information.hpp>
  10. /**
  11. \file
  12. \brief information.cpp
  13. \details
  14. Demonstrate information unit system.
  15. Output:
  16. @verbatim
  17. bytes= 1.25e+08 B
  18. bits= 8e+06 b
  19. nats= 4605.17 nat
  20. 1024 bytes in a kibi-byte
  21. 8.38861e+06 bits in a mebi-byte
  22. 0.000434294 hartleys in a milli-nat
  23. entropy in bits= 1 b
  24. entropy in nats= 0.693147 nat
  25. entropy in hartleys= 0.30103 Hart
  26. entropy in shannons= 1 Sh
  27. entropy in bytes= 0.125 B
  28. @endverbatim
  29. **/
  30. #include <cmath>
  31. #include <iostream>
  32. using std::cout;
  33. using std::endl;
  34. using std::log;
  35. #include <boost/units/quantity.hpp>
  36. #include <boost/units/io.hpp>
  37. #include <boost/units/conversion.hpp>
  38. namespace bu = boost::units;
  39. using bu::quantity;
  40. using bu::conversion_factor;
  41. // SI prefixes
  42. #include <boost/units/systems/si/prefixes.hpp>
  43. namespace si = boost::units::si;
  44. // information unit system
  45. #include <boost/units/systems/information.hpp>
  46. using namespace bu::information;
  47. // Define a function for the entropy of a bernoulli trial.
  48. // The formula is computed using natural log, so the units are in nats.
  49. // The user provides the desired return unit, the only restriction being that it
  50. // must be a unit of information. Conversion to the requested return unit is
  51. // accomplished automatically by the boost::units library.
  52. template <typename Sys>
  53. constexpr
  54. quantity<bu::unit<bu::information_dimension, Sys> >
  55. bernoulli_entropy(double p, const bu::unit<bu::information_dimension, Sys>&) {
  56. typedef bu::unit<bu::information_dimension, Sys> requested_unit;
  57. return quantity<requested_unit>((-(p*log(p) + (1-p)*log(1-p)))*nats);
  58. }
  59. int main(int argc, char** argv) {
  60. // a quantity of information (default in units of bytes)
  61. quantity<info> nbytes(1 * si::giga * bit);
  62. cout << "bytes= " << nbytes << endl;
  63. // a quantity of information, stored as bits
  64. quantity<hu::bit::info> nbits(1 * si::mega * byte);
  65. cout << "bits= " << nbits << endl;
  66. // a quantity of information, stored as nats
  67. quantity<hu::nat::info> nnats(2 * si::kilo * hartleys);
  68. cout << "nats= " << nnats << endl;
  69. // how many bytes are in a kibi-byte?
  70. cout << conversion_factor(kibi * byte, byte) << " bytes in a kibi-byte" << endl;
  71. // how many bits are in a mebi-byte?
  72. cout << conversion_factor(mebi * byte, bit) << " bits in a mebi-byte" << endl;
  73. // how many hartleys are in a milli-nat?
  74. cout << conversion_factor(si::milli * nat, hartley) << " hartleys in a milli-nat" << endl;
  75. // compute the entropy of a fair coin flip, in various units of information:
  76. cout << "entropy in bits= " << bernoulli_entropy(0.5, bits) << endl;
  77. cout << "entropy in nats= " << bernoulli_entropy(0.5, nats) << endl;
  78. cout << "entropy in hartleys= " << bernoulli_entropy(0.5, hartleys) << endl;
  79. cout << "entropy in shannons= " << bernoulli_entropy(0.5, shannons) << endl;
  80. cout << "entropy in bytes= " << bernoulli_entropy(0.5, bytes) << endl;
  81. return 0;
  82. }