]> git.donarmstrong.com Git - rsem.git/blob - boost/random/geometric_distribution.hpp
Added posterior standard deviation of counts as output if either '--calc-pme' or...
[rsem.git] / boost / random / geometric_distribution.hpp
1 /* boost random/geometric_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: geometric_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
16 #ifndef BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
18
19 #include <boost/config/no_tr1/cmath.hpp>          // std::log
20 #include <cassert>
21 #include <iostream>
22 #include <boost/random/detail/config.hpp>
23 #include <boost/random/uniform_01.hpp>
24
25 namespace boost {
26
27 #if defined(__GNUC__) && (__GNUC__ < 3)
28 // Special gcc workaround: gcc 2.95.x ignores using-declarations
29 // in template classes (confirmed by gcc author Martin v. Loewis)
30   using std::log;
31 #endif
32
33 /**
34  * An instantiation of the class template @c geometric_distribution models
35  * a \random_distribution.  The distribution produces positive
36  * integers which are the number of bernoulli trials
37  * with probability @c p required to get one that fails.
38  *
39  * For the geometric distribution, \f$p(i) = (1-p) p^{i-1}\f$.
40  */
41 template<class IntType = int, class RealType = double>
42 class geometric_distribution
43 {
44 public:
45   typedef RealType input_type;
46   typedef IntType result_type;
47
48   /**
49    * Contructs a new geometric_distribution with the paramter @c p.
50    *
51    * Requires: 0 < p < 1
52    */
53   explicit geometric_distribution(const RealType& p = RealType(0.5))
54     : _p(p)
55   {
56     assert(RealType(0) < _p && _p < RealType(1));
57     init();
58   }
59
60   // compiler-generated copy ctor and assignment operator are fine
61
62   /**
63    * Returns: the distribution parameter @c p
64    */
65   RealType p() const { return _p; }
66   void reset() { }
67
68   template<class Engine>
69   result_type operator()(Engine& eng)
70   {
71 #ifndef BOOST_NO_STDC_NAMESPACE
72     using std::log;
73     using std::floor;
74 #endif
75     return IntType(floor(log(RealType(1)-eng()) / _log_p)) + IntType(1);
76   }
77
78 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
79   template<class CharT, class Traits>
80   friend std::basic_ostream<CharT,Traits>&
81   operator<<(std::basic_ostream<CharT,Traits>& os, const geometric_distribution& gd)
82   {
83     os << gd._p;
84     return os;
85   }
86
87   template<class CharT, class Traits>
88   friend std::basic_istream<CharT,Traits>&
89   operator>>(std::basic_istream<CharT,Traits>& is, geometric_distribution& gd)
90   {
91     is >> std::ws >> gd._p;
92     gd.init();
93     return is;
94   }
95 #endif
96
97 private:
98
99   /// \cond hide_private_functions
100
101   void init()
102   {
103 #ifndef BOOST_NO_STDC_NAMESPACE
104     using std::log;
105 #endif
106     _log_p = log(_p);
107   }
108
109   /// \endcond
110
111   RealType _p;
112   RealType _log_p;
113 };
114
115 } // namespace boost
116
117 #endif // BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
118