Chris@16: /* boost random/geometric_distribution.hpp header file Chris@16: * Chris@16: * Copyright Jens Maurer 2000-2001 Chris@16: * Copyright Steven Watanabe 2011 Chris@16: * Distributed under the Boost Software License, Version 1.0. (See Chris@16: * accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org for most recent version including documentation. Chris@16: * Chris@101: * $Id$ Chris@16: * Chris@16: * Revision history Chris@16: * 2001-02-18 moved to individual header files Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP Chris@16: #define BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP Chris@16: Chris@16: #include // std::log Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: /** Chris@16: * An instantiation of the class template @c geometric_distribution models Chris@16: * a \random_distribution. The distribution produces positive Chris@16: * integers which are the number of bernoulli trials Chris@16: * with probability @c p required to get one that fails. Chris@16: * Chris@16: * For the geometric distribution, \f$p(i) = p(1-p)^{i}\f$. Chris@16: * Chris@16: * @xmlwarning Chris@16: * This distribution has been updated to match the C++ standard. Chris@16: * Its behavior has changed from the original Chris@16: * boost::geometric_distribution. A backwards compatible Chris@16: * wrapper is provided in namespace boost. Chris@16: * @endxmlwarning Chris@16: */ Chris@16: template Chris@16: class geometric_distribution Chris@16: { Chris@16: public: Chris@16: typedef RealType input_type; Chris@16: typedef IntType result_type; Chris@16: Chris@16: class param_type Chris@16: { Chris@16: public: Chris@16: Chris@16: typedef geometric_distribution distribution_type; Chris@16: Chris@16: /** Constructs the parameters with p. */ Chris@16: explicit param_type(RealType p_arg = RealType(0.5)) Chris@16: : _p(p_arg) Chris@16: { Chris@16: BOOST_ASSERT(RealType(0) < _p && _p < RealType(1)); Chris@16: } Chris@16: Chris@16: /** Returns the p parameter of the distribution. */ Chris@16: RealType p() const { return _p; } Chris@16: Chris@16: /** Writes the parameters to a std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@16: { Chris@16: os << parm._p; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the parameters from a std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@16: { Chris@16: double p_in; Chris@16: if(is >> p_in) { Chris@16: if(p_in > RealType(0) && p_in < RealType(1)) { Chris@16: parm._p = p_in; Chris@16: } else { Chris@16: is.setstate(std::ios_base::failbit); Chris@16: } Chris@16: } Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Returns true if the two sets of parameters are equal. */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) Chris@16: { return lhs._p == rhs._p; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are different. */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) Chris@16: Chris@16: Chris@16: private: Chris@16: RealType _p; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Contructs a new geometric_distribution with the paramter @c p. Chris@16: * Chris@16: * Requires: 0 < p < 1 Chris@16: */ Chris@101: explicit geometric_distribution(const RealType& p_arg = RealType(0.5)) Chris@101: : _p(p_arg) Chris@16: { Chris@16: BOOST_ASSERT(RealType(0) < _p && _p < RealType(1)); Chris@16: init(); Chris@16: } Chris@16: Chris@16: /** Constructs a new geometric_distribution from its parameters. */ Chris@16: explicit geometric_distribution(const param_type& parm) Chris@16: : _p(parm.p()) Chris@16: { Chris@16: init(); Chris@16: } Chris@16: Chris@16: // compiler-generated copy ctor and assignment operator are fine Chris@16: Chris@16: /** Returns: the distribution parameter @c p */ Chris@16: RealType p() const { return _p; } Chris@16: Chris@16: /** Returns the smallest value that the distribution can produce. */ Chris@16: IntType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return IntType(0); } Chris@16: Chris@16: /** Returns the largest value that the distribution can produce. */ Chris@16: IntType max BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@16: { return (std::numeric_limits::max)(); } Chris@16: Chris@16: /** Returns the parameters of the distribution. */ Chris@16: param_type param() const { return param_type(_p); } Chris@16: Chris@16: /** Sets the parameters of the distribution. */ Chris@16: void param(const param_type& parm) Chris@16: { Chris@16: _p = parm.p(); Chris@16: init(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Effects: Subsequent uses of the distribution do not depend Chris@16: * on values produced by any engine prior to invoking reset. Chris@16: */ Chris@16: void reset() { } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the Chris@16: * geometric_distribution. Chris@16: */ Chris@16: template Chris@16: result_type operator()(Engine& eng) const Chris@16: { Chris@16: using std::log; Chris@16: using std::floor; Chris@16: RealType x = RealType(1) - boost::uniform_01()(eng); Chris@16: return IntType(floor(log(x) / _log_1mp)); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the Chris@16: * geometric distribution with parameters specified by param. Chris@16: */ Chris@16: template Chris@16: result_type operator()(Engine& eng, const param_type& parm) const Chris@16: { return geometric_distribution(parm)(eng); } Chris@16: Chris@16: /** Writes the distribution to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, geometric_distribution, gd) Chris@16: { Chris@16: os << gd._p; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the distribution from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, geometric_distribution, gd) Chris@16: { Chris@16: param_type parm; Chris@16: if(is >> parm) { Chris@16: gd.param(parm); Chris@16: } Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true if the two distributions will produce identical Chris@16: * sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(geometric_distribution, lhs, rhs) Chris@16: { return lhs._p == rhs._p; } Chris@16: Chris@16: /** Chris@16: * Returns true if the two distributions may produce different Chris@16: * sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(geometric_distribution) Chris@16: Chris@16: private: Chris@16: Chris@16: /// \cond show_private Chris@16: Chris@16: void init() Chris@16: { Chris@16: using std::log; Chris@16: _log_1mp = log(1 - _p); Chris@16: } Chris@16: Chris@16: RealType _p; Chris@16: RealType _log_1mp; Chris@16: Chris@16: /// \endcond Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: Chris@16: /// \cond show_deprecated Chris@16: Chris@16: /** Chris@16: * Provided for backwards compatibility. This class is Chris@16: * deprecated. It provides the old behavior of geometric_distribution Chris@16: * with \f$p(i) = (1-p) p^{i-1}\f$. Chris@16: */ Chris@16: template Chris@16: class geometric_distribution Chris@16: { Chris@16: public: Chris@16: typedef RealType input_type; Chris@16: typedef IntType result_type; Chris@16: Chris@16: explicit geometric_distribution(RealType p_arg = RealType(0.5)) Chris@16: : _impl(1 - p_arg) {} Chris@16: Chris@16: RealType p() const { return 1 - _impl.p(); } Chris@16: Chris@16: void reset() {} Chris@16: Chris@16: template Chris@16: IntType operator()(Engine& eng) const { return _impl(eng) + IntType(1); } Chris@16: Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, geometric_distribution, gd) Chris@16: { Chris@16: os << gd.p(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, geometric_distribution, gd) Chris@16: { Chris@16: RealType val; Chris@16: if(is >> val) { Chris@16: typename impl_type::param_type impl_param(1 - val); Chris@16: gd._impl.param(impl_param); Chris@16: } Chris@16: return is; Chris@16: } Chris@16: Chris@16: private: Chris@16: typedef random::geometric_distribution impl_type; Chris@16: impl_type _impl; Chris@16: }; Chris@16: Chris@16: /// \endcond Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP