/src/contrib/boost/random/poisson_distribution.hpp

http://pythonocc.googlecode.com/ · C++ Header · 116 lines · 67 code · 16 blank · 33 comment · 2 complexity · f177ef8cc15de3adbd2a42ea0320af9c MD5 · raw file

  1. /* boost random/poisson_distribution.hpp header file
  2. *
  3. * Copyright Jens Maurer 2002
  4. * Distributed under the Boost Software License, Version 1.0. (See
  5. * accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org for most recent version including documentation.
  9. *
  10. * $Id: poisson_distribution.hpp 60755 2010-03-22 00:45:06Z steven_watanabe $
  11. *
  12. */
  13. #ifndef BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
  14. #define BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
  15. #include <boost/config/no_tr1/cmath.hpp>
  16. #include <cassert>
  17. #include <iostream>
  18. #include <boost/limits.hpp>
  19. #include <boost/static_assert.hpp>
  20. #include <boost/random/detail/config.hpp>
  21. namespace boost {
  22. // Knuth
  23. /**
  24. * An instantiation of the class template @c poisson_distribution is a
  25. * model of \random_distribution. The poisson distribution has
  26. * \f$p(i) = \frac{e^{-\lambda}\lambda^i}{i!}\f$
  27. */
  28. template<class IntType = int, class RealType = double>
  29. class poisson_distribution
  30. {
  31. public:
  32. typedef RealType input_type;
  33. typedef IntType result_type;
  34. /**
  35. * Constructs a @c poisson_distribution with the parameter @c mean.
  36. *
  37. * Requires: mean > 0
  38. */
  39. explicit poisson_distribution(const RealType& mean_arg = RealType(1))
  40. : _mean(mean_arg)
  41. {
  42. #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
  43. // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
  44. BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
  45. BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
  46. #endif
  47. assert(_mean > RealType(0));
  48. init();
  49. }
  50. // compiler-generated copy ctor and assignment operator are fine
  51. /**
  52. * Returns: the "mean" parameter of the distribution.
  53. */
  54. RealType mean() const { return _mean; }
  55. void reset() { }
  56. template<class Engine>
  57. result_type operator()(Engine& eng)
  58. {
  59. // TODO: This is O(_mean), but it should be O(log(_mean)) for large _mean
  60. RealType product = RealType(1);
  61. for(result_type m = 0; ; ++m) {
  62. product *= eng();
  63. if(product <= _exp_mean)
  64. return m;
  65. }
  66. }
  67. #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
  68. template<class CharT, class Traits>
  69. friend std::basic_ostream<CharT,Traits>&
  70. operator<<(std::basic_ostream<CharT,Traits>& os, const poisson_distribution& pd)
  71. {
  72. os << pd._mean;
  73. return os;
  74. }
  75. template<class CharT, class Traits>
  76. friend std::basic_istream<CharT,Traits>&
  77. operator>>(std::basic_istream<CharT,Traits>& is, poisson_distribution& pd)
  78. {
  79. is >> std::ws >> pd._mean;
  80. pd.init();
  81. return is;
  82. }
  83. #endif
  84. private:
  85. /// \cond hide_private_members
  86. void init()
  87. {
  88. #ifndef BOOST_NO_STDC_NAMESPACE
  89. // allow for Koenig lookup
  90. using std::exp;
  91. #endif
  92. _exp_mean = exp(-_mean);
  93. }
  94. /// \endcond
  95. RealType _mean;
  96. // some precomputed data from the parameters
  97. RealType _exp_mean;
  98. };
  99. } // namespace boost
  100. #endif // BOOST_RANDOM_POISSON_DISTRIBUTION_HPP