annotate DEPENDENCIES/generic/include/boost/random/geometric_distribution.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 /* boost random/geometric_distribution.hpp header file
Chris@16 2 *
Chris@16 3 * Copyright Jens Maurer 2000-2001
Chris@16 4 * Copyright Steven Watanabe 2011
Chris@16 5 * Distributed under the Boost Software License, Version 1.0. (See
Chris@16 6 * accompanying file LICENSE_1_0.txt or copy at
Chris@16 7 * http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8 *
Chris@16 9 * See http://www.boost.org for most recent version including documentation.
Chris@16 10 *
Chris@101 11 * $Id$
Chris@16 12 *
Chris@16 13 * Revision history
Chris@16 14 * 2001-02-18 moved to individual header files
Chris@16 15 */
Chris@16 16
Chris@16 17 #ifndef BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
Chris@16 18 #define BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
Chris@16 19
Chris@16 20 #include <boost/config/no_tr1/cmath.hpp> // std::log
Chris@16 21 #include <iosfwd>
Chris@16 22 #include <ios>
Chris@16 23 #include <boost/assert.hpp>
Chris@16 24 #include <boost/random/detail/config.hpp>
Chris@16 25 #include <boost/random/detail/operators.hpp>
Chris@16 26 #include <boost/random/uniform_01.hpp>
Chris@16 27
Chris@16 28 namespace boost {
Chris@16 29 namespace random {
Chris@16 30
Chris@16 31 /**
Chris@16 32 * An instantiation of the class template @c geometric_distribution models
Chris@16 33 * a \random_distribution. The distribution produces positive
Chris@16 34 * integers which are the number of bernoulli trials
Chris@16 35 * with probability @c p required to get one that fails.
Chris@16 36 *
Chris@16 37 * For the geometric distribution, \f$p(i) = p(1-p)^{i}\f$.
Chris@16 38 *
Chris@16 39 * @xmlwarning
Chris@16 40 * This distribution has been updated to match the C++ standard.
Chris@16 41 * Its behavior has changed from the original
Chris@16 42 * boost::geometric_distribution. A backwards compatible
Chris@16 43 * wrapper is provided in namespace boost.
Chris@16 44 * @endxmlwarning
Chris@16 45 */
Chris@16 46 template<class IntType = int, class RealType = double>
Chris@16 47 class geometric_distribution
Chris@16 48 {
Chris@16 49 public:
Chris@16 50 typedef RealType input_type;
Chris@16 51 typedef IntType result_type;
Chris@16 52
Chris@16 53 class param_type
Chris@16 54 {
Chris@16 55 public:
Chris@16 56
Chris@16 57 typedef geometric_distribution distribution_type;
Chris@16 58
Chris@16 59 /** Constructs the parameters with p. */
Chris@16 60 explicit param_type(RealType p_arg = RealType(0.5))
Chris@16 61 : _p(p_arg)
Chris@16 62 {
Chris@16 63 BOOST_ASSERT(RealType(0) < _p && _p < RealType(1));
Chris@16 64 }
Chris@16 65
Chris@16 66 /** Returns the p parameter of the distribution. */
Chris@16 67 RealType p() const { return _p; }
Chris@16 68
Chris@16 69 /** Writes the parameters to a std::ostream. */
Chris@16 70 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
Chris@16 71 {
Chris@16 72 os << parm._p;
Chris@16 73 return os;
Chris@16 74 }
Chris@16 75
Chris@16 76 /** Reads the parameters from a std::istream. */
Chris@16 77 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
Chris@16 78 {
Chris@16 79 double p_in;
Chris@16 80 if(is >> p_in) {
Chris@16 81 if(p_in > RealType(0) && p_in < RealType(1)) {
Chris@16 82 parm._p = p_in;
Chris@16 83 } else {
Chris@16 84 is.setstate(std::ios_base::failbit);
Chris@16 85 }
Chris@16 86 }
Chris@16 87 return is;
Chris@16 88 }
Chris@16 89
Chris@16 90 /** Returns true if the two sets of parameters are equal. */
Chris@16 91 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
Chris@16 92 { return lhs._p == rhs._p; }
Chris@16 93
Chris@16 94 /** Returns true if the two sets of parameters are different. */
Chris@16 95 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
Chris@16 96
Chris@16 97
Chris@16 98 private:
Chris@16 99 RealType _p;
Chris@16 100 };
Chris@16 101
Chris@16 102 /**
Chris@16 103 * Contructs a new geometric_distribution with the paramter @c p.
Chris@16 104 *
Chris@16 105 * Requires: 0 < p < 1
Chris@16 106 */
Chris@101 107 explicit geometric_distribution(const RealType& p_arg = RealType(0.5))
Chris@101 108 : _p(p_arg)
Chris@16 109 {
Chris@16 110 BOOST_ASSERT(RealType(0) < _p && _p < RealType(1));
Chris@16 111 init();
Chris@16 112 }
Chris@16 113
Chris@16 114 /** Constructs a new geometric_distribution from its parameters. */
Chris@16 115 explicit geometric_distribution(const param_type& parm)
Chris@16 116 : _p(parm.p())
Chris@16 117 {
Chris@16 118 init();
Chris@16 119 }
Chris@16 120
Chris@16 121 // compiler-generated copy ctor and assignment operator are fine
Chris@16 122
Chris@16 123 /** Returns: the distribution parameter @c p */
Chris@16 124 RealType p() const { return _p; }
Chris@16 125
Chris@16 126 /** Returns the smallest value that the distribution can produce. */
Chris@16 127 IntType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return IntType(0); }
Chris@16 128
Chris@16 129 /** Returns the largest value that the distribution can produce. */
Chris@16 130 IntType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
Chris@16 131 { return (std::numeric_limits<IntType>::max)(); }
Chris@16 132
Chris@16 133 /** Returns the parameters of the distribution. */
Chris@16 134 param_type param() const { return param_type(_p); }
Chris@16 135
Chris@16 136 /** Sets the parameters of the distribution. */
Chris@16 137 void param(const param_type& parm)
Chris@16 138 {
Chris@16 139 _p = parm.p();
Chris@16 140 init();
Chris@16 141 }
Chris@16 142
Chris@16 143 /**
Chris@16 144 * Effects: Subsequent uses of the distribution do not depend
Chris@16 145 * on values produced by any engine prior to invoking reset.
Chris@16 146 */
Chris@16 147 void reset() { }
Chris@16 148
Chris@16 149 /**
Chris@16 150 * Returns a random variate distributed according to the
Chris@16 151 * geometric_distribution.
Chris@16 152 */
Chris@16 153 template<class Engine>
Chris@16 154 result_type operator()(Engine& eng) const
Chris@16 155 {
Chris@16 156 using std::log;
Chris@16 157 using std::floor;
Chris@16 158 RealType x = RealType(1) - boost::uniform_01<RealType>()(eng);
Chris@16 159 return IntType(floor(log(x) / _log_1mp));
Chris@16 160 }
Chris@16 161
Chris@16 162 /**
Chris@16 163 * Returns a random variate distributed according to the
Chris@16 164 * geometric 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) const
Chris@16 168 { return geometric_distribution(parm)(eng); }
Chris@16 169
Chris@16 170 /** Writes the distribution to a @c std::ostream. */
Chris@16 171 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, geometric_distribution, gd)
Chris@16 172 {
Chris@16 173 os << gd._p;
Chris@16 174 return os;
Chris@16 175 }
Chris@16 176
Chris@16 177 /** Reads the distribution from a @c std::istream. */
Chris@16 178 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, geometric_distribution, gd)
Chris@16 179 {
Chris@16 180 param_type parm;
Chris@16 181 if(is >> parm) {
Chris@16 182 gd.param(parm);
Chris@16 183 }
Chris@16 184 return is;
Chris@16 185 }
Chris@16 186
Chris@16 187 /**
Chris@16 188 * Returns true if the two distributions will produce identical
Chris@16 189 * sequences of values given equal generators.
Chris@16 190 */
Chris@16 191 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(geometric_distribution, lhs, rhs)
Chris@16 192 { return lhs._p == rhs._p; }
Chris@16 193
Chris@16 194 /**
Chris@16 195 * Returns true if the two distributions may produce different
Chris@16 196 * sequences of values given equal generators.
Chris@16 197 */
Chris@16 198 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(geometric_distribution)
Chris@16 199
Chris@16 200 private:
Chris@16 201
Chris@16 202 /// \cond show_private
Chris@16 203
Chris@16 204 void init()
Chris@16 205 {
Chris@16 206 using std::log;
Chris@16 207 _log_1mp = log(1 - _p);
Chris@16 208 }
Chris@16 209
Chris@16 210 RealType _p;
Chris@16 211 RealType _log_1mp;
Chris@16 212
Chris@16 213 /// \endcond
Chris@16 214 };
Chris@16 215
Chris@16 216 } // namespace random
Chris@16 217
Chris@16 218 /// \cond show_deprecated
Chris@16 219
Chris@16 220 /**
Chris@16 221 * Provided for backwards compatibility. This class is
Chris@16 222 * deprecated. It provides the old behavior of geometric_distribution
Chris@16 223 * with \f$p(i) = (1-p) p^{i-1}\f$.
Chris@16 224 */
Chris@16 225 template<class IntType = int, class RealType = double>
Chris@16 226 class geometric_distribution
Chris@16 227 {
Chris@16 228 public:
Chris@16 229 typedef RealType input_type;
Chris@16 230 typedef IntType result_type;
Chris@16 231
Chris@16 232 explicit geometric_distribution(RealType p_arg = RealType(0.5))
Chris@16 233 : _impl(1 - p_arg) {}
Chris@16 234
Chris@16 235 RealType p() const { return 1 - _impl.p(); }
Chris@16 236
Chris@16 237 void reset() {}
Chris@16 238
Chris@16 239 template<class Engine>
Chris@16 240 IntType operator()(Engine& eng) const { return _impl(eng) + IntType(1); }
Chris@16 241
Chris@16 242 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, geometric_distribution, gd)
Chris@16 243 {
Chris@16 244 os << gd.p();
Chris@16 245 return os;
Chris@16 246 }
Chris@16 247
Chris@16 248 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, geometric_distribution, gd)
Chris@16 249 {
Chris@16 250 RealType val;
Chris@16 251 if(is >> val) {
Chris@16 252 typename impl_type::param_type impl_param(1 - val);
Chris@16 253 gd._impl.param(impl_param);
Chris@16 254 }
Chris@16 255 return is;
Chris@16 256 }
Chris@16 257
Chris@16 258 private:
Chris@16 259 typedef random::geometric_distribution<IntType, RealType> impl_type;
Chris@16 260 impl_type _impl;
Chris@16 261 };
Chris@16 262
Chris@16 263 /// \endcond
Chris@16 264
Chris@16 265 } // namespace boost
Chris@16 266
Chris@16 267 #endif // BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP