1 /* boost random/triangle_distribution.hpp header file
3 * Copyright Jens Maurer 2000-2001
4 * Copyright Steven Watanabe 2011
5 * Distributed under the Boost Software License, Version 1.0. (See
6 * accompanying file LICENSE_1_0.txt or copy at
7 * http://www.boost.org/LICENSE_1_0.txt)
9 * See http://www.boost.org for most recent version including documentation.
11 * $Id: triangle_distribution.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
14 * 2001-02-18 moved to individual header files
17 #ifndef BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
18 #define BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
20 #include <boost/config/no_tr1/cmath.hpp>
24 #include <boost/assert.hpp>
25 #include <boost/random/detail/config.hpp>
26 #include <boost/random/detail/operators.hpp>
27 #include <boost/random/uniform_01.hpp>
33 * Instantiations of @c triangle_distribution model a \random_distribution.
34 * A @c triangle_distribution has three parameters, @c a, @c b, and @c c,
35 * which are the smallest, the most probable and the largest values of
36 * the distribution respectively.
38 template<class RealType = double>
39 class triangle_distribution
42 typedef RealType input_type;
43 typedef RealType result_type;
49 typedef triangle_distribution distribution_type;
51 /** Constructs the parameters of a @c triangle_distribution. */
52 explicit param_type(RealType a_arg = RealType(0.0),
53 RealType b_arg = RealType(0.5),
54 RealType c_arg = RealType(1.0))
55 : _a(a_arg), _b(b_arg), _c(c_arg)
57 BOOST_ASSERT(_a <= _b && _b <= _c);
60 /** Returns the minimum value of the distribution. */
61 RealType a() const { return _a; }
62 /** Returns the mode of the distribution. */
63 RealType b() const { return _b; }
64 /** Returns the maximum value of the distribution. */
65 RealType c() const { return _c; }
67 /** Writes the parameters to a @c std::ostream. */
68 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
70 os << parm._a << " " << parm._b << " " << parm._c;
74 /** Reads the parameters from a @c std::istream. */
75 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
77 double a_in, b_in, c_in;
78 if(is >> a_in >> std::ws >> b_in >> std::ws >> c_in) {
79 if(a_in <= b_in && b_in <= c_in) {
84 is.setstate(std::ios_base::failbit);
90 /** Returns true if the two sets of parameters are equal. */
91 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
92 { return lhs._a == rhs._a && lhs._b == rhs._b && lhs._c == rhs._c; }
94 /** Returns true if the two sets of parameters are different. */
95 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
104 * Constructs a @c triangle_distribution with the parameters
105 * @c a, @c b, and @c c.
107 * Preconditions: a <= b <= c.
109 explicit triangle_distribution(RealType a_arg = RealType(0.0),
110 RealType b_arg = RealType(0.5),
111 RealType c_arg = RealType(1.0))
112 : _a(a_arg), _b(b_arg), _c(c_arg)
114 BOOST_ASSERT(_a <= _b && _b <= _c);
118 /** Constructs a @c triangle_distribution from its parameters. */
119 explicit triangle_distribution(const param_type& parm)
120 : _a(parm.a()), _b(parm.b()), _c(parm.c())
125 // compiler-generated copy ctor and assignment operator are fine
127 /** Returns the @c a parameter of the distribution */
128 result_type a() const { return _a; }
129 /** Returns the @c b parameter of the distribution */
130 result_type b() const { return _b; }
131 /** Returns the @c c parameter of the distribution */
132 result_type c() const { return _c; }
134 /** Returns the smallest value that the distribution can produce. */
135 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _a; }
136 /** Returns the largest value that the distribution can produce. */
137 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _c; }
139 /** Returns the parameters of the distribution. */
140 param_type param() const { return param_type(_a, _b, _c); }
141 /** Sets the parameters of the distribution. */
142 void param(const param_type& parm)
151 * Effects: Subsequent uses of the distribution do not depend
152 * on values produced by any engine prior to invoking reset.
157 * Returns a random variate distributed according to the
158 * triangle distribution.
160 template<class Engine>
161 result_type operator()(Engine& eng)
164 result_type u = uniform_01<>()(eng);
166 return _a + p1*sqrt(u);
168 return _c - d3*sqrt(d2*u-d1);
172 * Returns a random variate distributed according to the
173 * triangle distribution with parameters specified by param.
175 template<class Engine>
176 result_type operator()(Engine& eng, const param_type& parm)
177 { return triangle_distribution(parm)(eng); }
179 /** Writes the distribution to a @c std::ostream. */
180 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, triangle_distribution, td)
186 /** Reads the distribution from a @c std::istream. */
187 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, triangle_distribution, td)
197 * Returns true if the two distributions will produce identical
198 * sequences of values given equal generators.
200 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(triangle_distribution, lhs, rhs)
201 { return lhs._a == rhs._a && lhs._b == rhs._b && lhs._c == rhs._c; }
204 * Returns true if the two distributions may produce different
205 * sequences of values given equal generators.
207 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(triangle_distribution)
210 /// \cond show_private
223 RealType d1, d2, d3, q1, p1;
226 } // namespace random
228 using random::triangle_distribution;
232 #endif // BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP