1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/random/poisson_distribution.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,99 @@
1.4 +/* boost random/poisson_distribution.hpp header file
1.5 + *
1.6 + * Copyright Jens Maurer 2002
1.7 + * Distributed under the Boost Software License, Version 1.0. (See
1.8 + * accompanying file LICENSE_1_0.txt or copy at
1.9 + * http://www.boost.org/LICENSE_1_0.txt)
1.10 + *
1.11 + * See http://www.boost.org for most recent version including documentation.
1.12 + *
1.13 + * $Id: poisson_distribution.hpp,v 1.15 2005/05/21 15:57:00 dgregor Exp $
1.14 + *
1.15 + */
1.16 +
1.17 +#ifndef BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
1.18 +#define BOOST_RANDOM_POISSON_DISTRIBUTION_HPP
1.19 +
1.20 +#include <cmath>
1.21 +#include <cassert>
1.22 +#include <iostream>
1.23 +#include <boost/limits.hpp>
1.24 +#include <boost/static_assert.hpp>
1.25 +
1.26 +namespace boost {
1.27 +
1.28 +// Knuth
1.29 +template<class IntType = int, class RealType = double>
1.30 +class poisson_distribution
1.31 +{
1.32 +public:
1.33 + typedef RealType input_type;
1.34 + typedef IntType result_type;
1.35 +
1.36 + explicit poisson_distribution(const RealType& mean = RealType(1))
1.37 + : _mean(mean)
1.38 + {
1.39 +#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
1.40 + // MSVC fails BOOST_STATIC_ASSERT with std::numeric_limits at class scope
1.41 + BOOST_STATIC_ASSERT(std::numeric_limits<IntType>::is_integer);
1.42 + BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
1.43 +#endif
1.44 +
1.45 + assert(mean > RealType(0));
1.46 + init();
1.47 + }
1.48 +
1.49 + // compiler-generated copy ctor and assignment operator are fine
1.50 +
1.51 + RealType mean() const { return _mean; }
1.52 + void reset() { }
1.53 +
1.54 + template<class Engine>
1.55 + result_type operator()(Engine& eng)
1.56 + {
1.57 + // TODO: This is O(_mean), but it should be O(log(_mean)) for large _mean
1.58 + RealType product = RealType(1);
1.59 + for(result_type m = 0; ; ++m) {
1.60 + product *= eng();
1.61 + if(product <= _exp_mean)
1.62 + return m;
1.63 + }
1.64 + }
1.65 +
1.66 +#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
1.67 + template<class CharT, class Traits>
1.68 + friend std::basic_ostream<CharT,Traits>&
1.69 + operator<<(std::basic_ostream<CharT,Traits>& os, const poisson_distribution& pd)
1.70 + {
1.71 + os << pd._mean;
1.72 + return os;
1.73 + }
1.74 +
1.75 + template<class CharT, class Traits>
1.76 + friend std::basic_istream<CharT,Traits>&
1.77 + operator>>(std::basic_istream<CharT,Traits>& is, poisson_distribution& pd)
1.78 + {
1.79 + is >> std::ws >> pd._mean;
1.80 + pd.init();
1.81 + return is;
1.82 + }
1.83 +#endif
1.84 +
1.85 +private:
1.86 + void init()
1.87 + {
1.88 +#ifndef BOOST_NO_STDC_NAMESPACE
1.89 + // allow for Koenig lookup
1.90 + using std::exp;
1.91 +#endif
1.92 + _exp_mean = exp(-_mean);
1.93 + }
1.94 +
1.95 + RealType _mean;
1.96 + // some precomputed data from the parameters
1.97 + RealType _exp_mean;
1.98 +};
1.99 +
1.100 +} // namespace boost
1.101 +
1.102 +#endif // BOOST_RANDOM_POISSON_DISTRIBUTION_HPP