Chris@16: /* boost random/cauchy_distribution.hpp header file Chris@16: * Chris@16: * Copyright Jens Maurer 2000-2001 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@101: * $Id$ 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_CAUCHY_DISTRIBUTION_HPP Chris@16: #define BOOST_RANDOM_CAUCHY_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: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: // Cauchy distribution: Chris@16: Chris@16: /** Chris@16: * The cauchy distribution is a continuous distribution with two Chris@16: * parameters, median and sigma. Chris@16: * Chris@16: * It has \f$\displaystyle p(x) = \frac{\sigma}{\pi(\sigma^2 + (x-m)^2)}\f$ Chris@16: */ Chris@16: template Chris@16: class cauchy_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: { Chris@16: public: Chris@16: Chris@16: typedef cauchy_distribution distribution_type; Chris@16: Chris@16: /** Constructs the parameters of the cauchy distribution. */ Chris@16: explicit param_type(RealType median_arg = RealType(0.0), Chris@16: RealType sigma_arg = RealType(1.0)) Chris@16: : _median(median_arg), _sigma(sigma_arg) {} Chris@16: Chris@16: // backwards compatibility for Boost.Random Chris@16: Chris@16: /** Returns the median of the distribution. */ Chris@16: RealType median() const { return _median; } Chris@16: /** Returns the sigma parameter of the distribution. */ Chris@16: RealType sigma() const { return _sigma; } Chris@16: Chris@16: // The new names in C++0x. Chris@16: Chris@16: /** Returns the median of the distribution. */ Chris@16: RealType a() const { return _median; } Chris@16: /** Returns the sigma parameter of the distribution. */ Chris@16: RealType b() const { return _sigma; } Chris@16: Chris@16: /** Writes the parameters to a std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@16: { Chris@16: os << parm._median << " " << parm._sigma; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the parameters from a std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@16: { Chris@16: is >> parm._median >> std::ws >> parm._sigma; Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Returns true if the two sets of parameters are equal. */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) Chris@16: { return lhs._median == rhs._median && lhs._sigma == rhs._sigma; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are different. */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) Chris@16: Chris@16: private: Chris@16: RealType _median; Chris@16: RealType _sigma; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Constructs a \cauchy_distribution with the paramters @c median Chris@16: * and @c sigma. Chris@16: */ Chris@16: explicit cauchy_distribution(RealType median_arg = RealType(0.0), Chris@16: RealType sigma_arg = RealType(1.0)) Chris@16: : _median(median_arg), _sigma(sigma_arg) { } Chris@16: Chris@16: /** Chris@16: * Constructs a \cauchy_distribution from it's parameters. Chris@16: */ Chris@16: explicit cauchy_distribution(const param_type& parm) Chris@16: : _median(parm.median()), _sigma(parm.sigma()) { } Chris@16: Chris@16: // compiler-generated copy ctor and assignment operator are fine Chris@16: Chris@16: // backwards compatibility for Boost.Random Chris@16: Chris@16: /** Returns: the "median" parameter of the distribution */ Chris@16: RealType median() const { return _median; } Chris@16: /** Returns: the "sigma" parameter of the distribution */ Chris@16: RealType sigma() const { return _sigma; } Chris@16: Chris@16: // The new names in C++0x Chris@16: Chris@16: /** Returns: the "median" parameter of the distribution */ Chris@16: RealType a() const { return _median; } Chris@16: /** Returns: the "sigma" parameter of the distribution */ Chris@16: RealType b() 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: 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: param_type param() const { return param_type(_median, _sigma); } Chris@16: Chris@16: void param(const param_type& parm) Chris@16: { Chris@16: _median = parm.median(); Chris@16: _sigma = parm.sigma(); 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() { } Chris@16: Chris@16: /** Chris@16: * Returns: A random variate distributed according to the Chris@16: * cauchy distribution. Chris@16: */ Chris@16: template Chris@16: result_type operator()(Engine& eng) Chris@16: { Chris@16: // Can we have a boost::mathconst please? Chris@16: const result_type pi = result_type(3.14159265358979323846); Chris@16: using std::tan; Chris@16: RealType val = uniform_01()(eng)-result_type(0.5); Chris@16: return _median + _sigma * tan(pi*val); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns: A random variate distributed according to the Chris@16: * cauchy distribution with parameters specified by param. Chris@16: */ Chris@16: template Chris@16: result_type operator()(Engine& eng, const param_type& parm) Chris@16: { Chris@16: return cauchy_distribution(parm)(eng); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Writes the distribution to a @c std::ostream. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, cauchy_distribution, cd) Chris@16: { Chris@16: os << cd._median << " " << cd._sigma; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Reads the distribution from a @c std::istream. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, cauchy_distribution, cd) Chris@16: { Chris@16: is >> cd._median >> std::ws >> cd._sigma; Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true if the two distributions will produce Chris@16: * identical sequences of values, given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(cauchy_distribution, lhs, rhs) Chris@16: { return lhs._median == rhs._median && lhs._sigma == rhs._sigma; } Chris@16: Chris@16: /** Chris@16: * Returns true if the two distributions may produce Chris@16: * different sequences of values, given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(cauchy_distribution) Chris@16: Chris@16: private: Chris@16: RealType _median; Chris@16: RealType _sigma; Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: Chris@16: using random::cauchy_distribution; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP