1 /* boost random/cauchy_distribution.hpp header file
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)
8 * See http://www.boost.org for most recent version including documentation.
10 * $Id: cauchy_distribution.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
13 * 2001-02-18 moved to individual header files
16 #ifndef BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
17 #define BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
19 #include <boost/config/no_tr1/cmath.hpp>
22 #include <boost/limits.hpp>
23 #include <boost/random/detail/config.hpp>
24 #include <boost/random/detail/operators.hpp>
25 #include <boost/random/uniform_01.hpp>
30 // Cauchy distribution:
33 * The cauchy distribution is a continuous distribution with two
34 * parameters, median and sigma.
36 * It has \f$\displaystyle p(x) = \frac{\sigma}{\pi(\sigma^2 + (x-m)^2)}\f$
38 template<class RealType = double>
39 class cauchy_distribution
42 typedef RealType input_type;
43 typedef RealType result_type;
49 typedef cauchy_distribution distribution_type;
51 /** Constructs the parameters of the cauchy distribution. */
52 explicit param_type(RealType median_arg = RealType(0.0),
53 RealType sigma_arg = RealType(1.0))
54 : _median(median_arg), _sigma(sigma_arg) {}
56 // backwards compatibility for Boost.Random
58 /** Returns the median of the distribution. */
59 RealType median() const { return _median; }
60 /** Returns the sigma parameter of the distribution. */
61 RealType sigma() const { return _sigma; }
63 // The new names in C++0x.
65 /** Returns the median of the distribution. */
66 RealType a() const { return _median; }
67 /** Returns the sigma parameter of the distribution. */
68 RealType b() const { return _sigma; }
70 /** Writes the parameters to a std::ostream. */
71 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
73 os << parm._median << " " << parm._sigma;
77 /** Reads the parameters from a std::istream. */
78 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
80 is >> parm._median >> std::ws >> parm._sigma;
84 /** Returns true if the two sets of parameters are equal. */
85 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
86 { return lhs._median == rhs._median && lhs._sigma == rhs._sigma; }
88 /** Returns true if the two sets of parameters are different. */
89 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
97 * Constructs a \cauchy_distribution with the paramters @c median
100 explicit cauchy_distribution(RealType median_arg = RealType(0.0),
101 RealType sigma_arg = RealType(1.0))
102 : _median(median_arg), _sigma(sigma_arg) { }
105 * Constructs a \cauchy_distribution from it's parameters.
107 explicit cauchy_distribution(const param_type& parm)
108 : _median(parm.median()), _sigma(parm.sigma()) { }
110 // compiler-generated copy ctor and assignment operator are fine
112 // backwards compatibility for Boost.Random
114 /** Returns: the "median" parameter of the distribution */
115 RealType median() const { return _median; }
116 /** Returns: the "sigma" parameter of the distribution */
117 RealType sigma() const { return _sigma; }
119 // The new names in C++0x
121 /** Returns: the "median" parameter of the distribution */
122 RealType a() const { return _median; }
123 /** Returns: the "sigma" parameter of the distribution */
124 RealType b() const { return _sigma; }
126 /** Returns the smallest value that the distribution can produce. */
127 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
128 { return -(std::numeric_limits<RealType>::infinity)(); }
130 /** Returns the largest value that the distribution can produce. */
131 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
132 { return (std::numeric_limits<RealType>::infinity)(); }
134 param_type param() const { return param_type(_median, _sigma); }
136 void param(const param_type& parm)
138 _median = parm.median();
139 _sigma = parm.sigma();
143 * Effects: Subsequent uses of the distribution do not depend
144 * on values produced by any engine prior to invoking reset.
149 * Returns: A random variate distributed according to the
150 * cauchy distribution.
152 template<class Engine>
153 result_type operator()(Engine& eng)
155 // Can we have a boost::mathconst please?
156 const result_type pi = result_type(3.14159265358979323846);
158 RealType val = uniform_01<RealType>()(eng)-result_type(0.5);
159 return _median + _sigma * tan(pi*val);
163 * Returns: A random variate distributed according to the
164 * cauchy distribution with parameters specified by param.
166 template<class Engine>
167 result_type operator()(Engine& eng, const param_type& parm)
169 return cauchy_distribution(parm)(eng);
173 * Writes the distribution to a @c std::ostream.
175 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, cauchy_distribution, cd)
177 os << cd._median << " " << cd._sigma;
182 * Reads the distribution from a @c std::istream.
184 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, cauchy_distribution, cd)
186 is >> cd._median >> std::ws >> cd._sigma;
191 * Returns true if the two distributions will produce
192 * identical sequences of values, given equal generators.
194 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(cauchy_distribution, lhs, rhs)
195 { return lhs._median == rhs._median && lhs._sigma == rhs._sigma; }
198 * Returns true if the two distributions may produce
199 * different sequences of values, given equal generators.
201 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(cauchy_distribution)
208 } // namespace random
210 using random::cauchy_distribution;
214 #endif // BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP