1 | /* boost random/cauchy_distribution.hpp header file |
2 | * |
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) |
7 | * |
8 | * See http://www.boost.org for most recent version including documentation. |
9 | * |
10 | * $Id$ |
11 | * |
12 | * Revision history |
13 | * 2001-02-18 moved to individual header files |
14 | */ |
15 | |
16 | #ifndef BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP |
17 | #define BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP |
18 | |
19 | #include <boost/config/no_tr1/cmath.hpp> |
20 | #include <iosfwd> |
21 | #include <istream> |
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> |
26 | |
27 | namespace boost { |
28 | namespace random { |
29 | |
30 | // Cauchy distribution: |
31 | |
32 | /** |
33 | * The cauchy distribution is a continuous distribution with two |
34 | * parameters, median and sigma. |
35 | * |
36 | * It has \f$\displaystyle p(x) = \frac{\sigma}{\pi(\sigma^2 + (x-m)^2)}\f$ |
37 | */ |
38 | template<class RealType = double> |
39 | class cauchy_distribution |
40 | { |
41 | public: |
42 | typedef RealType input_type; |
43 | typedef RealType result_type; |
44 | |
45 | class param_type |
46 | { |
47 | public: |
48 | |
49 | typedef cauchy_distribution distribution_type; |
50 | |
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) {} |
55 | |
56 | // backwards compatibility for Boost.Random |
57 | |
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; } |
62 | |
63 | // The new names in C++0x. |
64 | |
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; } |
69 | |
70 | /** Writes the parameters to a std::ostream. */ |
71 | BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) |
72 | { |
73 | os << parm._median << " " << parm._sigma; |
74 | return os; |
75 | } |
76 | |
77 | /** Reads the parameters from a std::istream. */ |
78 | BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) |
79 | { |
80 | is >> parm._median >> std::ws >> parm._sigma; |
81 | return is; |
82 | } |
83 | |
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; } |
87 | |
88 | /** Returns true if the two sets of parameters are different. */ |
89 | BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) |
90 | |
91 | private: |
92 | RealType _median; |
93 | RealType _sigma; |
94 | }; |
95 | |
96 | /** |
97 | * Constructs a \cauchy_distribution with the paramters @c median |
98 | * and @c sigma. |
99 | */ |
100 | explicit cauchy_distribution(RealType median_arg = RealType(0.0), |
101 | RealType sigma_arg = RealType(1.0)) |
102 | : _median(median_arg), _sigma(sigma_arg) { } |
103 | |
104 | /** |
105 | * Constructs a \cauchy_distribution from it's parameters. |
106 | */ |
107 | explicit cauchy_distribution(const param_type& parm) |
108 | : _median(parm.median()), _sigma(parm.sigma()) { } |
109 | |
110 | // compiler-generated copy ctor and assignment operator are fine |
111 | |
112 | // backwards compatibility for Boost.Random |
113 | |
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; } |
118 | |
119 | // The new names in C++0x |
120 | |
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; } |
125 | |
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)(); } |
129 | |
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)(); } |
133 | |
134 | param_type param() const { return param_type(_median, _sigma); } |
135 | |
136 | void param(const param_type& parm) |
137 | { |
138 | _median = parm.median(); |
139 | _sigma = parm.sigma(); |
140 | } |
141 | |
142 | /** |
143 | * Effects: Subsequent uses of the distribution do not depend |
144 | * on values produced by any engine prior to invoking reset. |
145 | */ |
146 | void reset() { } |
147 | |
148 | /** |
149 | * Returns: A random variate distributed according to the |
150 | * cauchy distribution. |
151 | */ |
152 | template<class Engine> |
153 | result_type operator()(Engine& eng) |
154 | { |
155 | // Can we have a boost::mathconst please? |
156 | const result_type pi = result_type(3.14159265358979323846); |
157 | using std::tan; |
158 | RealType val = uniform_01<RealType>()(eng)-result_type(0.5); |
159 | return _median + _sigma * tan(pi*val); |
160 | } |
161 | |
162 | /** |
163 | * Returns: A random variate distributed according to the |
164 | * cauchy distribution with parameters specified by param. |
165 | */ |
166 | template<class Engine> |
167 | result_type operator()(Engine& eng, const param_type& parm) |
168 | { |
169 | return cauchy_distribution(parm)(eng); |
170 | } |
171 | |
172 | /** |
173 | * Writes the distribution to a @c std::ostream. |
174 | */ |
175 | BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, cauchy_distribution, cd) |
176 | { |
177 | os << cd._median << " " << cd._sigma; |
178 | return os; |
179 | } |
180 | |
181 | /** |
182 | * Reads the distribution from a @c std::istream. |
183 | */ |
184 | BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, cauchy_distribution, cd) |
185 | { |
186 | is >> cd._median >> std::ws >> cd._sigma; |
187 | return is; |
188 | } |
189 | |
190 | /** |
191 | * Returns true if the two distributions will produce |
192 | * identical sequences of values, given equal generators. |
193 | */ |
194 | BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(cauchy_distribution, lhs, rhs) |
195 | { return lhs._median == rhs._median && lhs._sigma == rhs._sigma; } |
196 | |
197 | /** |
198 | * Returns true if the two distributions may produce |
199 | * different sequences of values, given equal generators. |
200 | */ |
201 | BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(cauchy_distribution) |
202 | |
203 | private: |
204 | RealType _median; |
205 | RealType _sigma; |
206 | }; |
207 | |
208 | } // namespace random |
209 | |
210 | using random::cauchy_distribution; |
211 | |
212 | } // namespace boost |
213 | |
214 | #endif // BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP |
215 | |