/src/contrib/boost/random/exponential_distribution.hpp

http://pythonocc.googlecode.com/ · C++ Header · 86 lines · 51 code · 15 blank · 20 comment · 2 complexity · 76a9c1a2e52bda91a7c9deae57692289 MD5 · raw file

  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 60755 2010-03-22 00:45:06Z steven_watanabe $
  11. *
  12. * Revision history
  13. * 2001-02-18 moved to individual header files
  14. */
  15. #ifndef BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP
  16. #define BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP
  17. #include <boost/config/no_tr1/cmath.hpp>
  18. #include <cassert>
  19. #include <iostream>
  20. #include <boost/limits.hpp>
  21. #include <boost/static_assert.hpp>
  22. #include <boost/random/detail/config.hpp>
  23. namespace boost {
  24. /**
  25. * The exponential distribution has a single parameter lambda.
  26. *
  27. * It has \f$p(x) = \lambda e^{-\lambda x}\f$
  28. */
  29. template<class RealType = double>
  30. class exponential_distribution
  31. {
  32. public:
  33. typedef RealType input_type;
  34. typedef RealType result_type;
  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. explicit exponential_distribution(result_type lambda_arg = result_type(1))
  39. : _lambda(lambda_arg) { assert(_lambda > result_type(0)); }
  40. // compiler-generated copy ctor and assignment operator are fine
  41. result_type lambda() const { return _lambda; }
  42. void reset() { }
  43. template<class Engine>
  44. result_type operator()(Engine& eng)
  45. {
  46. #ifndef BOOST_NO_STDC_NAMESPACE
  47. using std::log;
  48. #endif
  49. return -result_type(1) / _lambda * log(result_type(1)-eng());
  50. }
  51. #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
  52. template<class CharT, class Traits>
  53. friend std::basic_ostream<CharT,Traits>&
  54. operator<<(std::basic_ostream<CharT,Traits>& os, const exponential_distribution& ed)
  55. {
  56. os << ed._lambda;
  57. return os;
  58. }
  59. template<class CharT, class Traits>
  60. friend std::basic_istream<CharT,Traits>&
  61. operator>>(std::basic_istream<CharT,Traits>& is, exponential_distribution& ed)
  62. {
  63. is >> std::ws >> ed._lambda;
  64. return is;
  65. }
  66. #endif
  67. private:
  68. result_type _lambda;
  69. };
  70. } // namespace boost
  71. #endif // BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP