| 1 | /* boost random/exponential_distribution.hpp header file | 
|---|
| 2 |  * | 
|---|
| 3 |  * Copyright Jens Maurer 2000-2001 | 
|---|
| 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: exponential_distribution.hpp,v 1.16 2004/07/27 03:43:32 dgregor Exp $ | 
|---|
| 11 |  * | 
|---|
| 12 |  * Revision history | 
|---|
| 13 |  *  2001-02-18  moved to individual header files | 
|---|
| 14 |  */ | 
|---|
| 15 |  | 
|---|
| 16 | #ifndef BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP | 
|---|
| 17 | #define BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP | 
|---|
| 18 |  | 
|---|
| 19 | #include <cmath> | 
|---|
| 20 | #include <cassert> | 
|---|
| 21 | #include <iostream> | 
|---|
| 22 | #include <boost/limits.hpp> | 
|---|
| 23 | #include <boost/static_assert.hpp> | 
|---|
| 24 |  | 
|---|
| 25 | namespace boost { | 
|---|
| 26 |  | 
|---|
| 27 | // exponential distribution: p(x) = lambda * exp(-lambda * x) | 
|---|
| 28 | template<class RealType = double> | 
|---|
| 29 | class exponential_distribution | 
|---|
| 30 | { | 
|---|
| 31 | public: | 
|---|
| 32 |   typedef RealType input_type; | 
|---|
| 33 |   typedef RealType result_type; | 
|---|
| 34 |  | 
|---|
| 35 | #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300) | 
|---|
| 36 |   BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer); | 
|---|
| 37 | #endif | 
|---|
| 38 |  | 
|---|
| 39 |   explicit exponential_distribution(result_type lambda = result_type(1)) | 
|---|
| 40 |     : _lambda(lambda) { assert(lambda > result_type(0)); } | 
|---|
| 41 |  | 
|---|
| 42 |   // compiler-generated copy ctor and assignment operator are fine | 
|---|
| 43 |  | 
|---|
| 44 |   result_type lambda() const { return _lambda; } | 
|---|
| 45 |  | 
|---|
| 46 |   void reset() { } | 
|---|
| 47 |  | 
|---|
| 48 |   template<class Engine> | 
|---|
| 49 |   result_type operator()(Engine& eng) | 
|---|
| 50 |   {  | 
|---|
| 51 | #ifndef BOOST_NO_STDC_NAMESPACE | 
|---|
| 52 |     using std::log; | 
|---|
| 53 | #endif | 
|---|
| 54 |     return -result_type(1) / _lambda * log(result_type(1)-eng()); | 
|---|
| 55 |   } | 
|---|
| 56 |  | 
|---|
| 57 | #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) | 
|---|
| 58 |   template<class CharT, class Traits> | 
|---|
| 59 |   friend std::basic_ostream<CharT,Traits>& | 
|---|
| 60 |   operator<<(std::basic_ostream<CharT,Traits>& os, const exponential_distribution& ed) | 
|---|
| 61 |   { | 
|---|
| 62 |     os << ed._lambda; | 
|---|
| 63 |     return os; | 
|---|
| 64 |   } | 
|---|
| 65 |  | 
|---|
| 66 |   template<class CharT, class Traits> | 
|---|
| 67 |   friend std::basic_istream<CharT,Traits>& | 
|---|
| 68 |   operator>>(std::basic_istream<CharT,Traits>& is, exponential_distribution& ed) | 
|---|
| 69 |   { | 
|---|
| 70 |     is >> std::ws >> ed._lambda; | 
|---|
| 71 |     return is; | 
|---|
| 72 |   } | 
|---|
| 73 | #endif | 
|---|
| 74 |  | 
|---|
| 75 | private: | 
|---|
| 76 |   result_type _lambda; | 
|---|
| 77 | }; | 
|---|
| 78 |  | 
|---|
| 79 | } // namespace boost | 
|---|
| 80 |  | 
|---|
| 81 | #endif // BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP | 
|---|