1 /* boost random/normal_distribution.hpp header file
3 * Copyright Jens Maurer 2000-2001
4 * Copyright Steven Watanabe 2010-2011
5 * Distributed under the Boost Software License, Version 1.0. (See
6 * accompanying file LICENSE_1_0.txt or copy at
7 * http://www.boost.org/LICENSE_1_0.txt)
9 * See http://www.boost.org for most recent version including documentation.
11 * $Id: normal_distribution.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
14 * 2001-02-18 moved to individual header files
17 #ifndef BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
18 #define BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
20 #include <boost/config/no_tr1/cmath.hpp>
23 #include <boost/assert.hpp>
24 #include <boost/limits.hpp>
25 #include <boost/static_assert.hpp>
26 #include <boost/random/detail/config.hpp>
27 #include <boost/random/detail/operators.hpp>
28 #include <boost/random/uniform_01.hpp>
33 // deterministic Box-Muller method, uses trigonometric functions
36 * Instantiations of class template normal_distribution model a
37 * \random_distribution. Such a distribution produces random numbers
38 * @c x distributed with probability density function
39 * \f$\displaystyle p(x) =
40 * \frac{1}{\sqrt{2\pi\sigma}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}
42 * where mean and sigma are the parameters of the distribution.
44 template<class RealType = double>
45 class normal_distribution
48 typedef RealType input_type;
49 typedef RealType result_type;
53 typedef normal_distribution distribution_type;
56 * Constructs a @c param_type with a given mean and
59 * Requires: sigma >= 0
61 explicit param_type(RealType mean_arg = RealType(0.0),
62 RealType sigma_arg = RealType(1.0))
67 /** Returns the mean of the distribution. */
68 RealType mean() const { return _mean; }
70 /** Returns the standand deviation of the distribution. */
71 RealType sigma() const { return _sigma; }
73 /** Writes a @c param_type to a @c std::ostream. */
74 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
75 { os << parm._mean << " " << parm._sigma ; return os; }
77 /** Reads a @c param_type from a @c std::istream. */
78 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
79 { is >> parm._mean >> std::ws >> parm._sigma; return is; }
81 /** Returns true if the two sets of parameters are the same. */
82 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
83 { return lhs._mean == rhs._mean && lhs._sigma == rhs._sigma; }
85 /** Returns true if the two sets of parameters are the different. */
86 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
94 * Constructs a @c normal_distribution object. @c mean and @c sigma are
95 * the parameters for the distribution.
97 * Requires: sigma >= 0
99 explicit normal_distribution(const RealType& mean_arg = RealType(0.0),
100 const RealType& sigma_arg = RealType(1.0))
101 : _mean(mean_arg), _sigma(sigma_arg),
102 _r1(0), _r2(0), _cached_rho(0), _valid(false)
104 BOOST_ASSERT(_sigma >= RealType(0));
108 * Constructs a @c normal_distribution object from its parameters.
110 explicit normal_distribution(const param_type& parm)
111 : _mean(parm.mean()), _sigma(parm.sigma()),
112 _r1(0), _r2(0), _cached_rho(0), _valid(false)
115 /** Returns the mean of the distribution. */
116 RealType mean() const { return _mean; }
117 /** Returns the standard deviation of the distribution. */
118 RealType sigma() const { return _sigma; }
120 /** Returns the smallest value that the distribution can produce. */
121 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
122 { return -std::numeric_limits<RealType>::infinity(); }
123 /** Returns the largest value that the distribution can produce. */
124 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
125 { return std::numeric_limits<RealType>::infinity(); }
127 /** Returns the parameters of the distribution. */
128 param_type param() const { return param_type(_mean, _sigma); }
129 /** Sets the parameters of the distribution. */
130 void param(const param_type& parm)
133 _sigma = parm.sigma();
138 * Effects: Subsequent uses of the distribution do not depend
139 * on values produced by any engine prior to invoking reset.
141 void reset() { _valid = false; }
143 /** Returns a normal variate. */
144 template<class Engine>
145 result_type operator()(Engine& eng)
153 _r1 = boost::uniform_01<RealType>()(eng);
154 _r2 = boost::uniform_01<RealType>()(eng);
155 _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));
160 // Can we have a boost::mathconst please?
161 const result_type pi = result_type(3.14159265358979323846);
163 return _cached_rho * (_valid ?
164 cos(result_type(2)*pi*_r1) :
165 sin(result_type(2)*pi*_r1))
169 /** Returns a normal variate with parameters specified by @c param. */
171 result_type operator()(URNG& urng, const param_type& parm)
173 return normal_distribution(parm)(urng);
176 /** Writes a @c normal_distribution to a @c std::ostream. */
177 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, normal_distribution, nd)
179 os << nd._mean << " " << nd._sigma << " "
180 << nd._valid << " " << nd._cached_rho << " " << nd._r1;
184 /** Reads a @c normal_distribution from a @c std::istream. */
185 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, normal_distribution, nd)
187 is >> std::ws >> nd._mean >> std::ws >> nd._sigma
188 >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
189 >> std::ws >> nd._r1;
194 * Returns true if the two instances of @c normal_distribution will
195 * return identical sequences of values given equal generators.
197 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(normal_distribution, lhs, rhs)
199 return lhs._mean == rhs._mean && lhs._sigma == rhs._sigma
200 && lhs._valid == rhs._valid
201 && (!lhs._valid || (lhs._r1 == rhs._r1 && lhs._r2 == rhs._r2));
205 * Returns true if the two instances of @c normal_distribution will
206 * return different sequences of values given equal generators.
208 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(normal_distribution)
211 RealType _mean, _sigma;
212 RealType _r1, _r2, _cached_rho;
217 } // namespace random
219 using random::normal_distribution;
223 #endif // BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP