]> git.donarmstrong.com Git - rsem.git/blob - boost/random/normal_distribution.hpp
Added posterior standard deviation of counts as output if either '--calc-pme' or...
[rsem.git] / boost / random / normal_distribution.hpp
1 /* boost random/normal_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: normal_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_NORMAL_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
18
19 #include <boost/config/no_tr1/cmath.hpp>
20 #include <cassert>
21 #include <iostream>
22 #include <boost/limits.hpp>
23 #include <boost/static_assert.hpp>
24 #include <boost/random/detail/config.hpp>
25
26 namespace boost {
27
28 /**
29  * Instantiations of class template normal_distribution model a
30  * \random_distribution. Such a distribution produces random numbers
31  * @c x distributed with probability density function
32  * \f$p(x) = \frac{1}{\sqrt{2\pi\sigma}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}\f$,
33  * where mean and sigma are the parameters of the distribution.
34  */
35 // deterministic Box-Muller method, uses trigonometric functions
36 template<class RealType = double>
37 class normal_distribution
38 {
39 public:
40   typedef RealType input_type;
41   typedef RealType result_type;
42
43 #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
44     BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
45 #endif
46
47   /**
48    * Constructs a normal_distribution object. @c mean and @c sigma are
49    * the parameters for the distribution.
50    *
51    * Requires: sigma > 0
52    */
53   explicit normal_distribution(const result_type& mean_arg = result_type(0),
54                                const result_type& sigma_arg = result_type(1))
55     : _mean(mean_arg), _sigma(sigma_arg), _valid(false)
56   {
57     assert(_sigma >= result_type(0));
58   }
59
60   // compiler-generated copy constructor is NOT fine, need to purge cache
61   normal_distribution(const normal_distribution& other)
62     : _mean(other._mean), _sigma(other._sigma), _valid(false)
63   {
64   }
65
66   // compiler-generated copy ctor and assignment operator are fine
67
68   /**
69    * Returns: The "mean" parameter of the distribution.
70    */
71   RealType mean() const { return _mean; }
72   /**
73    * Returns: The "sigma" parameter of the distribution.
74    */
75   RealType sigma() const { return _sigma; }
76
77   void reset() { _valid = false; }
78
79   template<class Engine>
80   result_type operator()(Engine& eng)
81   {
82 #ifndef BOOST_NO_STDC_NAMESPACE
83     // allow for Koenig lookup
84     using std::sqrt; using std::log; using std::sin; using std::cos;
85 #endif
86     if(!_valid) {
87       _r1 = eng();
88       _r2 = eng();
89       _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));
90       _valid = true;
91     } else {
92       _valid = false;
93     }
94     // Can we have a boost::mathconst please?
95     const result_type pi = result_type(3.14159265358979323846);
96     
97     return _cached_rho * (_valid ?
98                           cos(result_type(2)*pi*_r1) :
99                           sin(result_type(2)*pi*_r1))
100       * _sigma + _mean;
101   }
102
103 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
104   template<class CharT, class Traits>
105   friend std::basic_ostream<CharT,Traits>&
106   operator<<(std::basic_ostream<CharT,Traits>& os, const normal_distribution& nd)
107   {
108     os << nd._mean << " " << nd._sigma << " "
109        << nd._valid << " " << nd._cached_rho << " " << nd._r1;
110     return os;
111   }
112
113   template<class CharT, class Traits>
114   friend std::basic_istream<CharT,Traits>&
115   operator>>(std::basic_istream<CharT,Traits>& is, normal_distribution& nd)
116   {
117     is >> std::ws >> nd._mean >> std::ws >> nd._sigma
118        >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
119        >> std::ws >> nd._r1;
120     return is;
121   }
122 #endif
123 private:
124   result_type _mean, _sigma;
125   result_type _r1, _r2, _cached_rho;
126   bool _valid;
127 };
128
129 } // namespace boost
130
131 #endif // BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP