os/ossrv/ossrv_pub/boost_apis/boost/random/poisson_distribution.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* boost random/poisson_distribution.hpp header file
sl@0
     2
 *
sl@0
     3
 * Copyright Jens Maurer 2002
sl@0
     4
 * Distributed under the Boost Software License, Version 1.0. (See
sl@0
     5
 * accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
 * http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
 *
sl@0
     8
 * See http://www.boost.org for most recent version including documentation.
sl@0
     9
 *
sl@0
    10
 * $Id: poisson_distribution.hpp,v 1.15 2005/05/21 15:57:00 dgregor Exp $
sl@0
    11
 *
sl@0
    12
 */
sl@0
    13
sl@0
    14
#ifndef BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
sl@0
    15
#define BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
sl@0
    16
sl@0
    17
#include <cmath>
sl@0
    18
#include <cassert>
sl@0
    19
#include <iostream>
sl@0
    20
#include <boost/limits.hpp>
sl@0
    21
#include <boost/static_assert.hpp>
sl@0
    22
sl@0
    23
namespace boost {
sl@0
    24
sl@0
    25
// Knuth
sl@0
    26
template<class IntType = int, class RealType = double>
sl@0
    27
class poisson_distribution
sl@0
    28
{
sl@0
    29
public:
sl@0
    30
  typedef RealType input_type;
sl@0
    31
  typedef IntType result_type;
sl@0
    32
sl@0
    33
  explicit poisson_distribution(const RealType& mean = RealType(1))
sl@0
    34
    : _mean(mean)
sl@0
    35
  {
sl@0
    36
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
sl@0
    37
    // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
sl@0
    38
    BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
sl@0
    39
    BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
sl@0
    40
#endif
sl@0
    41
sl@0
    42
    assert(mean > RealType(0));
sl@0
    43
    init();
sl@0
    44
  }
sl@0
    45
sl@0
    46
  // compiler-generated copy ctor and assignment operator are fine
sl@0
    47
sl@0
    48
  RealType mean() const { return _mean; }
sl@0
    49
  void reset() { }
sl@0
    50
sl@0
    51
  template<class Engine>
sl@0
    52
  result_type operator()(Engine& eng)
sl@0
    53
  {
sl@0
    54
    // TODO: This is O(_mean), but it should be O(log(_mean)) for large _mean
sl@0
    55
    RealType product = RealType(1);
sl@0
    56
    for(result_type m = 0; ; ++m) {
sl@0
    57
      product *= eng();
sl@0
    58
      if(product <= _exp_mean)
sl@0
    59
        return m;
sl@0
    60
    }
sl@0
    61
  }
sl@0
    62
sl@0
    63
#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
sl@0
    64
  template<class CharT, class Traits>
sl@0
    65
  friend std::basic_ostream<CharT,Traits>&
sl@0
    66
  operator<<(std::basic_ostream<CharT,Traits>& os, const poisson_distribution& pd)
sl@0
    67
  {
sl@0
    68
    os << pd._mean;
sl@0
    69
    return os;
sl@0
    70
  }
sl@0
    71
sl@0
    72
  template<class CharT, class Traits>
sl@0
    73
  friend std::basic_istream<CharT,Traits>&
sl@0
    74
  operator>>(std::basic_istream<CharT,Traits>& is, poisson_distribution& pd)
sl@0
    75
  {
sl@0
    76
    is >> std::ws >> pd._mean;
sl@0
    77
    pd.init();
sl@0
    78
    return is;
sl@0
    79
  }
sl@0
    80
#endif
sl@0
    81
sl@0
    82
private:
sl@0
    83
  void init()
sl@0
    84
  {
sl@0
    85
#ifndef BOOST_NO_STDC_NAMESPACE
sl@0
    86
    // allow for Koenig lookup
sl@0
    87
    using std::exp;
sl@0
    88
#endif
sl@0
    89
    _exp_mean = exp(-_mean);
sl@0
    90
  }
sl@0
    91
sl@0
    92
  RealType _mean;
sl@0
    93
  // some precomputed data from the parameters
sl@0
    94
  RealType _exp_mean;
sl@0
    95
};
sl@0
    96
sl@0
    97
} // namespace boost
sl@0
    98
sl@0
    99
#endif // BOOST_RANDOM_POISSON_DISTRIBUTION_HPP