]> git.donarmstrong.com Git - rsem.git/blob - boost/random/triangle_distribution.hpp
Added posterior standard deviation of counts as output if either '--calc-pme' or...
[rsem.git] / boost / random / triangle_distribution.hpp
1 /* boost random/triangle_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: triangle_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_TRIANGLE_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
18
19 #include <boost/config/no_tr1/cmath.hpp>
20 #include <cassert>
21 #include <boost/random/detail/config.hpp>
22 #include <boost/random/uniform_01.hpp>
23
24 namespace boost {
25
26 /**
27  * Instantiations of @c triangle_distribution model a \random_distribution.
28  * A @c triangle_distribution has three parameters, @c a, @c b, and @c c,
29  * which are the smallest, the most probable and the largest values of
30  * the distribution respectively.
31  */
32 template<class RealType = double>
33 class triangle_distribution
34 {
35 public:
36   typedef RealType input_type;
37   typedef RealType result_type;
38
39   /**
40    * Constructs a @c triangle_distribution with the parameters
41    * @c a, @c b, and @c c.
42    *
43    * Preconditions: a <= b <= c.
44    */
45   explicit triangle_distribution(result_type a_arg = result_type(0),
46                                  result_type b_arg = result_type(0.5),
47                                  result_type c_arg = result_type(1))
48     : _a(a_arg), _b(b_arg), _c(c_arg)
49   {
50     assert(_a <= _b && _b <= _c);
51     init();
52   }
53
54   // compiler-generated copy ctor and assignment operator are fine
55
56   /** Returns the @c a parameter of the distribution */
57   result_type a() const { return _a; }
58   /** Returns the @c b parameter of the distribution */
59   result_type b() const { return _b; }
60   /** Returns the @c c parameter of the distribution */
61   result_type c() const { return _c; }
62
63   void reset() { }
64
65   template<class Engine>
66   result_type operator()(Engine& eng)
67   {
68 #ifndef BOOST_NO_STDC_NAMESPACE
69     using std::sqrt;
70 #endif
71     result_type u = eng();
72     if( u <= q1 )
73       return _a + p1*sqrt(u);
74     else
75       return _c - d3*sqrt(d2*u-d1);
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 triangle_distribution& td)
82   {
83     os << td._a << " " << td._b << " " << td._c;
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, triangle_distribution& td)
90   {
91     is >> std::ws >> td._a >> std::ws >> td._b >> std::ws >> td._c;
92     td.init();
93     return is;
94   }
95 #endif
96
97 private:
98   /// \cond hide_private_members
99   void init()
100   {
101 #ifndef BOOST_NO_STDC_NAMESPACE
102     using std::sqrt;
103 #endif
104     d1 = _b - _a;
105     d2 = _c - _a;
106     d3 = sqrt(_c - _b);
107     q1 = d1 / d2;
108     p1 = sqrt(d1 * d2);
109   }
110   /// \endcond
111
112   result_type _a, _b, _c;
113   result_type d1, d2, d3, q1, p1;
114 };
115
116 } // namespace boost
117
118 #endif // BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP