os/ossrv/ossrv_pub/boost_apis/boost/random/exponential_distribution.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* boost random/exponential_distribution.hpp header file
sl@0
     2
 *
sl@0
     3
 * Copyright Jens Maurer 2000-2001
sl@0
     4
 * Distributed under the Boost Software License, Version 1.0. (See
sl@0
     5
 * accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
 * http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
 *
sl@0
     8
 * See http://www.boost.org for most recent version including documentation.
sl@0
     9
 *
sl@0
    10
 * $Id: exponential_distribution.hpp,v 1.16 2004/07/27 03:43:32 dgregor Exp $
sl@0
    11
 *
sl@0
    12
 * Revision history
sl@0
    13
 *  2001-02-18  moved to individual header files
sl@0
    14
 */
sl@0
    15
sl@0
    16
#ifndef BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP
sl@0
    17
#define BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP
sl@0
    18
sl@0
    19
#include <cmath>
sl@0
    20
#include <cassert>
sl@0
    21
#include <iostream>
sl@0
    22
#include <boost/limits.hpp>
sl@0
    23
#include <boost/static_assert.hpp>
sl@0
    24
sl@0
    25
namespace boost {
sl@0
    26
sl@0
    27
// exponential distribution: p(x) = lambda * exp(-lambda * x)
sl@0
    28
template<class RealType = double>
sl@0
    29
class exponential_distribution
sl@0
    30
{
sl@0
    31
public:
sl@0
    32
  typedef RealType input_type;
sl@0
    33
  typedef RealType result_type;
sl@0
    34
sl@0
    35
#if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
sl@0
    36
  BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
sl@0
    37
#endif
sl@0
    38
sl@0
    39
  explicit exponential_distribution(result_type lambda = result_type(1))
sl@0
    40
    : _lambda(lambda) { assert(lambda > result_type(0)); }
sl@0
    41
sl@0
    42
  // compiler-generated copy ctor and assignment operator are fine
sl@0
    43
sl@0
    44
  result_type lambda() const { return _lambda; }
sl@0
    45
sl@0
    46
  void reset() { }
sl@0
    47
sl@0
    48
  template<class Engine>
sl@0
    49
  result_type operator()(Engine& eng)
sl@0
    50
  { 
sl@0
    51
#ifndef BOOST_NO_STDC_NAMESPACE
sl@0
    52
    using std::log;
sl@0
    53
#endif
sl@0
    54
    return -result_type(1) / _lambda * log(result_type(1)-eng());
sl@0
    55
  }
sl@0
    56
sl@0
    57
#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
sl@0
    58
  template<class CharT, class Traits>
sl@0
    59
  friend std::basic_ostream<CharT,Traits>&
sl@0
    60
  operator<<(std::basic_ostream<CharT,Traits>& os, const exponential_distribution& ed)
sl@0
    61
  {
sl@0
    62
    os << ed._lambda;
sl@0
    63
    return os;
sl@0
    64
  }
sl@0
    65
sl@0
    66
  template<class CharT, class Traits>
sl@0
    67
  friend std::basic_istream<CharT,Traits>&
sl@0
    68
  operator>>(std::basic_istream<CharT,Traits>& is, exponential_distribution& ed)
sl@0
    69
  {
sl@0
    70
    is >> std::ws >> ed._lambda;
sl@0
    71
    return is;
sl@0
    72
  }
sl@0
    73
#endif
sl@0
    74
sl@0
    75
private:
sl@0
    76
  result_type _lambda;
sl@0
    77
};
sl@0
    78
sl@0
    79
} // namespace boost
sl@0
    80
sl@0
    81
#endif // BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP