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 60755 2010-03-22 00:45:06Z 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>
21 #include <boost/limits.hpp>
22 #include <boost/static_assert.hpp>
23 #include <boost/random/detail/config.hpp>
27 #if defined(__GNUC__) && (__GNUC__ < 3)
28 // Special gcc workaround: gcc 2.95.x ignores using-declarations
29 // in template classes (confirmed by gcc author Martin v. Loewis)
33 // Cauchy distribution:
36 * The cauchy distribution is a continuous distribution with two
37 * parameters, sigma and median.
39 * It has \f$p(x) = \frac{\sigma}{\pi(\sigma^2 + (x-m)^2)}\f$
41 template<class RealType = double>
42 class cauchy_distribution
45 typedef RealType input_type;
46 typedef RealType result_type;
48 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
49 BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
53 * Constructs a \cauchy_distribution with the paramters @c median
56 explicit cauchy_distribution(result_type median_arg = result_type(0),
57 result_type sigma_arg = result_type(1))
58 : _median(median_arg), _sigma(sigma_arg) { }
60 // compiler-generated copy ctor and assignment operator are fine
63 * Returns: the "median" parameter of the distribution
65 result_type median() const { return _median; }
67 * Returns: the "sigma" parameter of the distribution
69 result_type sigma() const { return _sigma; }
71 * Effects: Subsequent uses of the distribution do not depend
72 * on values produced by any engine prior to invoking reset.
77 * Returns: A random variate distributed according to the
78 * cauchy distribution.
80 template<class Engine>
81 result_type operator()(Engine& eng)
83 // Can we have a boost::mathconst please?
84 const result_type pi = result_type(3.14159265358979323846);
85 #ifndef BOOST_NO_STDC_NAMESPACE
88 return _median + _sigma * tan(pi*(eng()-result_type(0.5)));
91 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
93 * Writes the parameters of the distribution to a @c std::ostream.
95 template<class CharT, class Traits>
96 friend std::basic_ostream<CharT,Traits>&
97 operator<<(std::basic_ostream<CharT,Traits>& os, const cauchy_distribution& cd)
99 os << cd._median << " " << cd._sigma;
104 * Reads the parameters of the distribution from a @c std::istream.
106 template<class CharT, class Traits>
107 friend std::basic_istream<CharT,Traits>&
108 operator>>(std::basic_istream<CharT,Traits>& is, cauchy_distribution& cd)
110 is >> std::ws >> cd._median >> std::ws >> cd._sigma;
116 result_type _median, _sigma;
121 #endif // BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP