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