Chris@16: /* boost random/exponential_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_EXPONENTIAL_DISTRIBUTION_HPP Chris@16: #define BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP Chris@16: Chris@16: #include 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: * The exponential distribution is a model of \random_distribution with Chris@16: * a single parameter lambda. Chris@16: * Chris@16: * It has \f$\displaystyle p(x) = \lambda e^{-\lambda x}\f$ Chris@16: */ Chris@16: template Chris@16: class exponential_distribution Chris@16: { Chris@16: public: Chris@16: typedef RealType input_type; Chris@16: typedef RealType result_type; Chris@16: Chris@16: class param_type Chris@16: { Chris@16: public: Chris@16: Chris@16: typedef exponential_distribution distribution_type; Chris@16: Chris@16: /** Chris@16: * Constructs parameters with a given lambda. Chris@16: * Chris@16: * Requires: lambda > 0 Chris@16: */ Chris@16: param_type(RealType lambda_arg = RealType(1.0)) Chris@16: : _lambda(lambda_arg) { BOOST_ASSERT(_lambda > RealType(0)); } Chris@16: Chris@16: /** Returns the lambda parameter of the distribution. */ Chris@16: RealType lambda() const { return _lambda; } Chris@16: Chris@16: /** Writes the parameters to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@16: { Chris@16: os << parm._lambda; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the parameters from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@16: { Chris@16: is >> parm._lambda; 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._lambda == rhs._lambda; } 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: private: Chris@16: RealType _lambda; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Constructs an exponential_distribution with a given lambda. Chris@16: * Chris@16: * Requires: lambda > 0 Chris@16: */ Chris@16: explicit exponential_distribution(RealType lambda_arg = RealType(1.0)) Chris@16: : _lambda(lambda_arg) { BOOST_ASSERT(_lambda > RealType(0)); } Chris@16: Chris@16: /** Chris@16: * Constructs an exponential_distribution from its parameters Chris@16: */ Chris@16: explicit exponential_distribution(const param_type& parm) Chris@16: : _lambda(parm.lambda()) {} Chris@16: Chris@16: // compiler-generated copy ctor and assignment operator are fine Chris@16: Chris@16: /** Returns the lambda parameter of the distribution. */ Chris@16: RealType lambda() const { return _lambda; } Chris@16: Chris@16: /** Returns the smallest value that the distribution can produce. */ Chris@16: RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@16: { return RealType(0); } Chris@16: /** Returns the largest value that the distribution can produce. */ Chris@16: RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@16: { return (std::numeric_limits::infinity)(); } Chris@16: Chris@16: /** Returns the parameters of the distribution. */ Chris@16: param_type param() const { return param_type(_lambda); } Chris@16: /** Sets the parameters of the distribution. */ Chris@16: void param(const param_type& parm) { _lambda = parm.lambda(); } 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: * exponential distribution. Chris@16: */ Chris@16: template Chris@16: result_type operator()(Engine& eng) const Chris@16: { Chris@16: using std::log; Chris@16: return -result_type(1) / Chris@16: _lambda * log(result_type(1)-uniform_01()(eng)); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the exponential Chris@16: * 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: { Chris@16: return exponential_distribution(parm)(eng); Chris@16: } Chris@16: Chris@16: /** Writes the distribution to a std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, exponential_distribution, ed) Chris@16: { Chris@16: os << ed._lambda; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the distribution from a std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, exponential_distribution, ed) Chris@16: { Chris@16: is >> ed._lambda; Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true iff the two distributions will produce identical Chris@16: * sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(exponential_distribution, lhs, rhs) Chris@16: { return lhs._lambda == rhs._lambda; } Chris@16: Chris@16: /** Chris@16: * Returns true iff the two distributions will produce different Chris@16: * sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(exponential_distribution) Chris@16: Chris@16: private: Chris@16: result_type _lambda; Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: Chris@16: using random::exponential_distribution; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP