PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 14#ifndef BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
 15#define BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
 16
 17#include <boost/config/no_tr1/cmath.hpp>
 18#include <cassert>
 19#include <iostream>
 20#include <boost/limits.hpp>
 21#include <boost/static_assert.hpp>
 22#include <boost/random/detail/config.hpp>
 23
 24namespace boost {
 25
 26// Knuth
 27
 28/**
 29 * An instantiation of the class template @c poisson_distribution is a
 30 * model of \random_distribution.  The poisson distribution has
 31 * \f$p(i) = \frac{e^{-\lambda}\lambda^i}{i!}\f$
 32 */
 33template<class IntType = int, class RealType = double>
 34class poisson_distribution
 35{
 36public:
 37  typedef RealType input_type;
 38  typedef IntType result_type;
 39
 40  /**
 41   * Constructs a @c poisson_distribution with the parameter @c mean.
 42   *
 43   * Requires: mean > 0
 44   */
 45  explicit poisson_distribution(const RealType& mean_arg = RealType(1))
 46    : _mean(mean_arg)
 47  {
 48#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
 49    // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
 50    BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
 51    BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
 52#endif
 53
 54    assert(_mean > RealType(0));
 55    init();
 56  }
 57
 58  // compiler-generated copy ctor and assignment operator are fine
 59
 60  /**
 61   * Returns: the "mean" parameter of the distribution.
 62   */
 63  RealType mean() const { return _mean; }
 64  void reset() { }
 65
 66  template<class Engine>
 67  result_type operator()(Engine& eng)
 68  {
 69    // TODO: This is O(_mean), but it should be O(log(_mean)) for large _mean
 70    RealType product = RealType(1);
 71    for(result_type m = 0; ; ++m) {
 72      product *= eng();
 73      if(product <= _exp_mean)
 74        return m;
 75    }
 76  }
 77
 78#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
 79  template<class CharT, class Traits>
 80  friend std::basic_ostream<CharT,Traits>&
 81  operator<<(std::basic_ostream<CharT,Traits>& os, const poisson_distribution& pd)
 82  {
 83    os << pd._mean;
 84    return os;
 85  }
 86
 87  template<class CharT, class Traits>
 88  friend std::basic_istream<CharT,Traits>&
 89  operator>>(std::basic_istream<CharT,Traits>& is, poisson_distribution& pd)
 90  {
 91    is >> std::ws >> pd._mean;
 92    pd.init();
 93    return is;
 94  }
 95#endif
 96
 97private:
 98  /// \cond hide_private_members
 99  void init()
100  {
101#ifndef BOOST_NO_STDC_NAMESPACE
102    // allow for Koenig lookup
103    using std::exp;
104#endif
105    _exp_mean = exp(-_mean);
106  }
107  /// \endcond
108
109  RealType _mean;
110  // some precomputed data from the parameters
111  RealType _exp_mean;
112};
113
114} // namespace boost
115
116#endif // BOOST_RANDOM_POISSON_DISTRIBUTION_HPP