annotate DEPENDENCIES/generic/include/boost/random/triangle_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/triangle_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_TRIANGLE_DISTRIBUTION_HPP
Chris@16 18 #define BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
Chris@16 19
Chris@16 20 #include <boost/config/no_tr1/cmath.hpp>
Chris@16 21 #include <iosfwd>
Chris@16 22 #include <ios>
Chris@16 23 #include <istream>
Chris@16 24 #include <boost/assert.hpp>
Chris@16 25 #include <boost/random/detail/config.hpp>
Chris@16 26 #include <boost/random/detail/operators.hpp>
Chris@16 27 #include <boost/random/uniform_01.hpp>
Chris@16 28
Chris@16 29 namespace boost {
Chris@16 30 namespace random {
Chris@16 31
Chris@16 32 /**
Chris@16 33 * Instantiations of @c triangle_distribution model a \random_distribution.
Chris@16 34 * A @c triangle_distribution has three parameters, @c a, @c b, and @c c,
Chris@16 35 * which are the smallest, the most probable and the largest values of
Chris@16 36 * the distribution respectively.
Chris@16 37 */
Chris@16 38 template<class RealType = double>
Chris@16 39 class triangle_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 triangle_distribution distribution_type;
Chris@16 50
Chris@16 51 /** Constructs the parameters of a @c triangle_distribution. */
Chris@16 52 explicit param_type(RealType a_arg = RealType(0.0),
Chris@16 53 RealType b_arg = RealType(0.5),
Chris@16 54 RealType c_arg = RealType(1.0))
Chris@16 55 : _a(a_arg), _b(b_arg), _c(c_arg)
Chris@16 56 {
Chris@16 57 BOOST_ASSERT(_a <= _b && _b <= _c);
Chris@16 58 }
Chris@16 59
Chris@16 60 /** Returns the minimum value of the distribution. */
Chris@16 61 RealType a() const { return _a; }
Chris@16 62 /** Returns the mode of the distribution. */
Chris@16 63 RealType b() const { return _b; }
Chris@16 64 /** Returns the maximum value of the distribution. */
Chris@16 65 RealType c() const { return _c; }
Chris@16 66
Chris@16 67 /** Writes the parameters to a @c std::ostream. */
Chris@16 68 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
Chris@16 69 {
Chris@16 70 os << parm._a << " " << parm._b << " " << parm._c;
Chris@16 71 return os;
Chris@16 72 }
Chris@16 73
Chris@16 74 /** Reads the parameters from a @c std::istream. */
Chris@16 75 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
Chris@16 76 {
Chris@16 77 double a_in, b_in, c_in;
Chris@16 78 if(is >> a_in >> std::ws >> b_in >> std::ws >> c_in) {
Chris@16 79 if(a_in <= b_in && b_in <= c_in) {
Chris@16 80 parm._a = a_in;
Chris@16 81 parm._b = b_in;
Chris@16 82 parm._c = c_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._a == rhs._a && lhs._b == rhs._b && lhs._c == rhs._c; }
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 private:
Chris@16 98 RealType _a;
Chris@16 99 RealType _b;
Chris@16 100 RealType _c;
Chris@16 101 };
Chris@16 102
Chris@16 103 /**
Chris@16 104 * Constructs a @c triangle_distribution with the parameters
Chris@16 105 * @c a, @c b, and @c c.
Chris@16 106 *
Chris@16 107 * Preconditions: a <= b <= c.
Chris@16 108 */
Chris@16 109 explicit triangle_distribution(RealType a_arg = RealType(0.0),
Chris@16 110 RealType b_arg = RealType(0.5),
Chris@16 111 RealType c_arg = RealType(1.0))
Chris@16 112 : _a(a_arg), _b(b_arg), _c(c_arg)
Chris@16 113 {
Chris@16 114 BOOST_ASSERT(_a <= _b && _b <= _c);
Chris@16 115 init();
Chris@16 116 }
Chris@16 117
Chris@16 118 /** Constructs a @c triangle_distribution from its parameters. */
Chris@16 119 explicit triangle_distribution(const param_type& parm)
Chris@16 120 : _a(parm.a()), _b(parm.b()), _c(parm.c())
Chris@16 121 {
Chris@16 122 init();
Chris@16 123 }
Chris@16 124
Chris@16 125 // compiler-generated copy ctor and assignment operator are fine
Chris@16 126
Chris@16 127 /** Returns the @c a parameter of the distribution */
Chris@16 128 result_type a() const { return _a; }
Chris@16 129 /** Returns the @c b parameter of the distribution */
Chris@16 130 result_type b() const { return _b; }
Chris@16 131 /** Returns the @c c parameter of the distribution */
Chris@16 132 result_type c() const { return _c; }
Chris@16 133
Chris@16 134 /** Returns the smallest value that the distribution can produce. */
Chris@16 135 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _a; }
Chris@16 136 /** Returns the largest value that the distribution can produce. */
Chris@16 137 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _c; }
Chris@16 138
Chris@16 139 /** Returns the parameters of the distribution. */
Chris@16 140 param_type param() const { return param_type(_a, _b, _c); }
Chris@16 141 /** Sets the parameters of the distribution. */
Chris@16 142 void param(const param_type& parm)
Chris@16 143 {
Chris@16 144 _a = parm.a();
Chris@16 145 _b = parm.b();
Chris@16 146 _c = parm.c();
Chris@16 147 init();
Chris@16 148 }
Chris@16 149
Chris@16 150 /**
Chris@16 151 * Effects: Subsequent uses of the distribution do not depend
Chris@16 152 * on values produced by any engine prior to invoking reset.
Chris@16 153 */
Chris@16 154 void reset() { }
Chris@16 155
Chris@16 156 /**
Chris@16 157 * Returns a random variate distributed according to the
Chris@16 158 * triangle distribution.
Chris@16 159 */
Chris@16 160 template<class Engine>
Chris@16 161 result_type operator()(Engine& eng)
Chris@16 162 {
Chris@16 163 using std::sqrt;
Chris@16 164 result_type u = uniform_01<>()(eng);
Chris@16 165 if( u <= q1 )
Chris@16 166 return _a + p1*sqrt(u);
Chris@16 167 else
Chris@16 168 return _c - d3*sqrt(d2*u-d1);
Chris@16 169 }
Chris@16 170
Chris@16 171 /**
Chris@16 172 * Returns a random variate distributed according to the
Chris@16 173 * triangle distribution with parameters specified by param.
Chris@16 174 */
Chris@16 175 template<class Engine>
Chris@16 176 result_type operator()(Engine& eng, const param_type& parm)
Chris@16 177 { return triangle_distribution(parm)(eng); }
Chris@16 178
Chris@16 179 /** Writes the distribution to a @c std::ostream. */
Chris@16 180 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, triangle_distribution, td)
Chris@16 181 {
Chris@16 182 os << td.param();
Chris@16 183 return os;
Chris@16 184 }
Chris@16 185
Chris@16 186 /** Reads the distribution from a @c std::istream. */
Chris@16 187 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, triangle_distribution, td)
Chris@16 188 {
Chris@16 189 param_type parm;
Chris@16 190 if(is >> parm) {
Chris@16 191 td.param(parm);
Chris@16 192 }
Chris@16 193 return is;
Chris@16 194 }
Chris@16 195
Chris@16 196 /**
Chris@16 197 * Returns true if the two distributions will produce identical
Chris@16 198 * sequences of values given equal generators.
Chris@16 199 */
Chris@16 200 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(triangle_distribution, lhs, rhs)
Chris@16 201 { return lhs._a == rhs._a && lhs._b == rhs._b && lhs._c == rhs._c; }
Chris@16 202
Chris@16 203 /**
Chris@16 204 * Returns true if the two distributions may produce different
Chris@16 205 * sequences of values given equal generators.
Chris@16 206 */
Chris@16 207 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(triangle_distribution)
Chris@16 208
Chris@16 209 private:
Chris@16 210 /// \cond show_private
Chris@16 211 void init()
Chris@16 212 {
Chris@16 213 using std::sqrt;
Chris@16 214 d1 = _b - _a;
Chris@16 215 d2 = _c - _a;
Chris@16 216 d3 = sqrt(_c - _b);
Chris@16 217 q1 = d1 / d2;
Chris@16 218 p1 = sqrt(d1 * d2);
Chris@16 219 }
Chris@16 220 /// \endcond
Chris@16 221
Chris@16 222 RealType _a, _b, _c;
Chris@16 223 RealType d1, d2, d3, q1, p1;
Chris@16 224 };
Chris@16 225
Chris@16 226 } // namespace random
Chris@16 227
Chris@16 228 using random::triangle_distribution;
Chris@16 229
Chris@16 230 } // namespace boost
Chris@16 231
Chris@16 232 #endif // BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP