cauchy_distribution.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #ifndef BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
  16. #define BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP
  17. #include <boost/config/no_tr1/cmath.hpp>
  18. #include <iosfwd>
  19. #include <istream>
  20. #include <boost/limits.hpp>
  21. #include <boost/random/detail/config.hpp>
  22. #include <boost/random/detail/operators.hpp>
  23. #include <boost/random/uniform_01.hpp>
  24. namespace boost {
  25. namespace random {
  26. // Cauchy distribution:
  27. /**
  28. * The cauchy distribution is a continuous distribution with two
  29. * parameters, median and sigma.
  30. *
  31. * It has \f$\displaystyle p(x) = \frac{\sigma}{\pi(\sigma^2 + (x-m)^2)}\f$
  32. */
  33. template<class RealType = double>
  34. class cauchy_distribution
  35. {
  36. public:
  37. typedef RealType input_type;
  38. typedef RealType result_type;
  39. class param_type
  40. {
  41. public:
  42. typedef cauchy_distribution distribution_type;
  43. /** Constructs the parameters of the cauchy distribution. */
  44. explicit param_type(RealType median_arg = RealType(0.0),
  45. RealType sigma_arg = RealType(1.0))
  46. : _median(median_arg), _sigma(sigma_arg) {}
  47. // backwards compatibility for Boost.Random
  48. /** Returns the median of the distribution. */
  49. RealType median() const { return _median; }
  50. /** Returns the sigma parameter of the distribution. */
  51. RealType sigma() const { return _sigma; }
  52. // The new names in C++0x.
  53. /** Returns the median of the distribution. */
  54. RealType a() const { return _median; }
  55. /** Returns the sigma parameter of the distribution. */
  56. RealType b() const { return _sigma; }
  57. /** Writes the parameters to a std::ostream. */
  58. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
  59. {
  60. os << parm._median << " " << parm._sigma;
  61. return os;
  62. }
  63. /** Reads the parameters from a std::istream. */
  64. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
  65. {
  66. is >> parm._median >> std::ws >> parm._sigma;
  67. return is;
  68. }
  69. /** Returns true if the two sets of parameters are equal. */
  70. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
  71. { return lhs._median == rhs._median && lhs._sigma == rhs._sigma; }
  72. /** Returns true if the two sets of parameters are different. */
  73. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
  74. private:
  75. RealType _median;
  76. RealType _sigma;
  77. };
  78. /**
  79. * Constructs a \cauchy_distribution with the paramters @c median
  80. * and @c sigma.
  81. */
  82. explicit cauchy_distribution(RealType median_arg = RealType(0.0),
  83. RealType sigma_arg = RealType(1.0))
  84. : _median(median_arg), _sigma(sigma_arg) { }
  85. /**
  86. * Constructs a \cauchy_distribution from it's parameters.
  87. */
  88. explicit cauchy_distribution(const param_type& parm)
  89. : _median(parm.median()), _sigma(parm.sigma()) { }
  90. // compiler-generated copy ctor and assignment operator are fine
  91. // backwards compatibility for Boost.Random
  92. /** Returns: the "median" parameter of the distribution */
  93. RealType median() const { return _median; }
  94. /** Returns: the "sigma" parameter of the distribution */
  95. RealType sigma() const { return _sigma; }
  96. // The new names in C++0x
  97. /** Returns: the "median" parameter of the distribution */
  98. RealType a() const { return _median; }
  99. /** Returns: the "sigma" parameter of the distribution */
  100. RealType b() const { return _sigma; }
  101. /** Returns the smallest value that the distribution can produce. */
  102. RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
  103. { return -(std::numeric_limits<RealType>::infinity)(); }
  104. /** Returns the largest value that the distribution can produce. */
  105. RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
  106. { return (std::numeric_limits<RealType>::infinity)(); }
  107. param_type param() const { return param_type(_median, _sigma); }
  108. void param(const param_type& parm)
  109. {
  110. _median = parm.median();
  111. _sigma = parm.sigma();
  112. }
  113. /**
  114. * Effects: Subsequent uses of the distribution do not depend
  115. * on values produced by any engine prior to invoking reset.
  116. */
  117. void reset() { }
  118. /**
  119. * Returns: A random variate distributed according to the
  120. * cauchy distribution.
  121. */
  122. template<class Engine>
  123. result_type operator()(Engine& eng)
  124. {
  125. // Can we have a boost::mathconst please?
  126. const result_type pi = result_type(3.14159265358979323846);
  127. using std::tan;
  128. RealType val = uniform_01<RealType>()(eng)-result_type(0.5);
  129. return _median + _sigma * tan(pi*val);
  130. }
  131. /**
  132. * Returns: A random variate distributed according to the
  133. * cauchy distribution with parameters specified by param.
  134. */
  135. template<class Engine>
  136. result_type operator()(Engine& eng, const param_type& parm)
  137. {
  138. return cauchy_distribution(parm)(eng);
  139. }
  140. /**
  141. * Writes the distribution to a @c std::ostream.
  142. */
  143. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, cauchy_distribution, cd)
  144. {
  145. os << cd._median << " " << cd._sigma;
  146. return os;
  147. }
  148. /**
  149. * Reads the distribution from a @c std::istream.
  150. */
  151. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, cauchy_distribution, cd)
  152. {
  153. is >> cd._median >> std::ws >> cd._sigma;
  154. return is;
  155. }
  156. /**
  157. * Returns true if the two distributions will produce
  158. * identical sequences of values, given equal generators.
  159. */
  160. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(cauchy_distribution, lhs, rhs)
  161. { return lhs._median == rhs._median && lhs._sigma == rhs._sigma; }
  162. /**
  163. * Returns true if the two distributions may produce
  164. * different sequences of values, given equal generators.
  165. */
  166. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(cauchy_distribution)
  167. private:
  168. RealType _median;
  169. RealType _sigma;
  170. };
  171. } // namespace random
  172. using random::cauchy_distribution;
  173. } // namespace boost
  174. #endif // BOOST_RANDOM_CAUCHY_DISTRIBUTION_HPP