Chris@16: /* boost random/normal_distribution.hpp header file Chris@16: * Chris@16: * Copyright Jens Maurer 2000-2001 Chris@16: * Copyright Steven Watanabe 2010-2011 Chris@16: * Distributed under the Boost Software License, Version 1.0. (See Chris@16: * accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org for most recent version including documentation. Chris@16: * Chris@16: * $Id: normal_distribution.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $ Chris@16: * Chris@16: * Revision history Chris@16: * 2001-02-18 moved to individual header files Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP Chris@16: #define BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: // deterministic Box-Muller method, uses trigonometric functions Chris@16: Chris@16: /** Chris@16: * Instantiations of class template normal_distribution model a Chris@16: * \random_distribution. Such a distribution produces random numbers Chris@16: * @c x distributed with probability density function Chris@16: * \f$\displaystyle p(x) = Chris@16: * \frac{1}{\sqrt{2\pi\sigma}} e^{-\frac{(x-\mu)^2}{2\sigma^2}} Chris@16: * \f$, Chris@16: * where mean and sigma are the parameters of the distribution. Chris@16: */ Chris@16: template Chris@16: class normal_distribution Chris@16: { Chris@16: public: Chris@16: typedef RealType input_type; Chris@16: typedef RealType result_type; Chris@16: Chris@16: class param_type { Chris@16: public: Chris@16: typedef normal_distribution distribution_type; Chris@16: Chris@16: /** Chris@16: * Constructs a @c param_type with a given mean and Chris@16: * standard deviation. Chris@16: * Chris@16: * Requires: sigma >= 0 Chris@16: */ Chris@16: explicit param_type(RealType mean_arg = RealType(0.0), Chris@16: RealType sigma_arg = RealType(1.0)) Chris@16: : _mean(mean_arg), Chris@16: _sigma(sigma_arg) Chris@16: {} Chris@16: Chris@16: /** Returns the mean of the distribution. */ Chris@16: RealType mean() const { return _mean; } Chris@16: Chris@16: /** Returns the standand deviation of the distribution. */ Chris@16: RealType sigma() const { return _sigma; } Chris@16: Chris@16: /** Writes a @c param_type to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@16: { os << parm._mean << " " << parm._sigma ; return os; } Chris@16: Chris@16: /** Reads a @c param_type from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@16: { is >> parm._mean >> std::ws >> parm._sigma; return is; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are the same. */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) Chris@16: { return lhs._mean == rhs._mean && lhs._sigma == rhs._sigma; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are the different. */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) Chris@16: Chris@16: private: Chris@16: RealType _mean; Chris@16: RealType _sigma; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Constructs a @c normal_distribution object. @c mean and @c sigma are Chris@16: * the parameters for the distribution. Chris@16: * Chris@16: * Requires: sigma >= 0 Chris@16: */ Chris@16: explicit normal_distribution(const RealType& mean_arg = RealType(0.0), Chris@16: const RealType& sigma_arg = RealType(1.0)) Chris@16: : _mean(mean_arg), _sigma(sigma_arg), Chris@16: _r1(0), _r2(0), _cached_rho(0), _valid(false) Chris@16: { Chris@16: BOOST_ASSERT(_sigma >= RealType(0)); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Constructs a @c normal_distribution object from its parameters. Chris@16: */ Chris@16: explicit normal_distribution(const param_type& parm) Chris@16: : _mean(parm.mean()), _sigma(parm.sigma()), Chris@16: _r1(0), _r2(0), _cached_rho(0), _valid(false) Chris@16: {} Chris@16: Chris@16: /** Returns the mean of the distribution. */ Chris@16: RealType mean() const { return _mean; } Chris@16: /** Returns the standard deviation of the distribution. */ Chris@16: RealType sigma() const { return _sigma; } Chris@16: Chris@16: /** Returns the smallest value that the distribution can produce. */ Chris@16: RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@16: { return -std::numeric_limits::infinity(); } Chris@16: /** Returns the largest value that the distribution can produce. */ Chris@16: RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const Chris@16: { return std::numeric_limits::infinity(); } Chris@16: Chris@16: /** Returns the parameters of the distribution. */ Chris@16: param_type param() const { return param_type(_mean, _sigma); } Chris@16: /** Sets the parameters of the distribution. */ Chris@16: void param(const param_type& parm) Chris@16: { Chris@16: _mean = parm.mean(); Chris@16: _sigma = parm.sigma(); Chris@16: _valid = false; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Effects: Subsequent uses of the distribution do not depend Chris@16: * on values produced by any engine prior to invoking reset. Chris@16: */ Chris@16: void reset() { _valid = false; } Chris@16: Chris@16: /** Returns a normal variate. */ Chris@16: template Chris@16: result_type operator()(Engine& eng) Chris@16: { Chris@16: using std::sqrt; Chris@16: using std::log; Chris@16: using std::sin; Chris@16: using std::cos; Chris@16: Chris@16: if(!_valid) { Chris@16: _r1 = boost::uniform_01()(eng); Chris@16: _r2 = boost::uniform_01()(eng); Chris@16: _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2)); Chris@16: _valid = true; Chris@16: } else { Chris@16: _valid = false; Chris@16: } Chris@16: // Can we have a boost::mathconst please? Chris@16: const result_type pi = result_type(3.14159265358979323846); Chris@16: Chris@16: return _cached_rho * (_valid ? Chris@16: cos(result_type(2)*pi*_r1) : Chris@16: sin(result_type(2)*pi*_r1)) Chris@16: * _sigma + _mean; Chris@16: } Chris@16: Chris@16: /** Returns a normal variate with parameters specified by @c param. */ Chris@16: template Chris@16: result_type operator()(URNG& urng, const param_type& parm) Chris@16: { Chris@16: return normal_distribution(parm)(urng); Chris@16: } Chris@16: Chris@16: /** Writes a @c normal_distribution to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, normal_distribution, nd) Chris@16: { Chris@16: os << nd._mean << " " << nd._sigma << " " Chris@16: << nd._valid << " " << nd._cached_rho << " " << nd._r1; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads a @c normal_distribution from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, normal_distribution, nd) Chris@16: { Chris@16: is >> std::ws >> nd._mean >> std::ws >> nd._sigma Chris@16: >> std::ws >> nd._valid >> std::ws >> nd._cached_rho Chris@16: >> std::ws >> nd._r1; Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true if the two instances of @c normal_distribution will Chris@16: * return identical sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(normal_distribution, lhs, rhs) Chris@16: { Chris@16: return lhs._mean == rhs._mean && lhs._sigma == rhs._sigma Chris@16: && lhs._valid == rhs._valid Chris@16: && (!lhs._valid || (lhs._r1 == rhs._r1 && lhs._r2 == rhs._r2)); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true if the two instances of @c normal_distribution will Chris@16: * return different sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(normal_distribution) Chris@16: Chris@16: private: Chris@16: RealType _mean, _sigma; Chris@16: RealType _r1, _r2, _cached_rho; Chris@16: bool _valid; Chris@16: Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: Chris@16: using random::normal_distribution; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP