Chris@16: /* boost random/triangle_distribution.hpp header file Chris@16: * Chris@16: * Copyright Jens Maurer 2000-2001 Chris@16: * Copyright Steven Watanabe 2011 Chris@16: * Distributed under the Boost Software License, Version 1.0. (See Chris@16: * accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org for most recent version including documentation. Chris@16: * Chris@101: * $Id$ Chris@16: * Chris@16: * Revision history Chris@16: * 2001-02-18 moved to individual header files Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP Chris@16: #define BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace random { Chris@16: Chris@16: /** Chris@16: * Instantiations of @c triangle_distribution model a \random_distribution. Chris@16: * A @c triangle_distribution has three parameters, @c a, @c b, and @c c, Chris@16: * which are the smallest, the most probable and the largest values of Chris@16: * the distribution respectively. Chris@16: */ Chris@16: template Chris@16: class triangle_distribution Chris@16: { Chris@16: public: Chris@16: typedef RealType input_type; Chris@16: typedef RealType result_type; Chris@16: Chris@16: class param_type Chris@16: { Chris@16: public: Chris@16: Chris@16: typedef triangle_distribution distribution_type; Chris@16: Chris@16: /** Constructs the parameters of a @c triangle_distribution. */ Chris@16: explicit param_type(RealType a_arg = RealType(0.0), Chris@16: RealType b_arg = RealType(0.5), Chris@16: RealType c_arg = RealType(1.0)) Chris@16: : _a(a_arg), _b(b_arg), _c(c_arg) Chris@16: { Chris@16: BOOST_ASSERT(_a <= _b && _b <= _c); Chris@16: } Chris@16: Chris@16: /** Returns the minimum value of the distribution. */ Chris@16: RealType a() const { return _a; } Chris@16: /** Returns the mode of the distribution. */ Chris@16: RealType b() const { return _b; } Chris@16: /** Returns the maximum value of the distribution. */ Chris@16: RealType c() const { return _c; } Chris@16: Chris@16: /** Writes the parameters to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm) Chris@16: { Chris@16: os << parm._a << " " << parm._b << " " << parm._c; Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the parameters from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm) Chris@16: { Chris@16: double a_in, b_in, c_in; Chris@16: if(is >> a_in >> std::ws >> b_in >> std::ws >> c_in) { Chris@16: if(a_in <= b_in && b_in <= c_in) { Chris@16: parm._a = a_in; Chris@16: parm._b = b_in; Chris@16: parm._c = c_in; Chris@16: } else { Chris@16: is.setstate(std::ios_base::failbit); Chris@16: } Chris@16: } Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Returns true if the two sets of parameters are equal. */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs) Chris@16: { return lhs._a == rhs._a && lhs._b == rhs._b && lhs._c == rhs._c; } Chris@16: Chris@16: /** Returns true if the two sets of parameters are different. */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type) Chris@16: Chris@16: private: Chris@16: RealType _a; Chris@16: RealType _b; Chris@16: RealType _c; Chris@16: }; Chris@16: Chris@16: /** Chris@16: * Constructs a @c triangle_distribution with the parameters Chris@16: * @c a, @c b, and @c c. Chris@16: * Chris@16: * Preconditions: a <= b <= c. Chris@16: */ Chris@16: explicit triangle_distribution(RealType a_arg = RealType(0.0), Chris@16: RealType b_arg = RealType(0.5), Chris@16: RealType c_arg = RealType(1.0)) Chris@16: : _a(a_arg), _b(b_arg), _c(c_arg) Chris@16: { Chris@16: BOOST_ASSERT(_a <= _b && _b <= _c); Chris@16: init(); Chris@16: } Chris@16: Chris@16: /** Constructs a @c triangle_distribution from its parameters. */ Chris@16: explicit triangle_distribution(const param_type& parm) Chris@16: : _a(parm.a()), _b(parm.b()), _c(parm.c()) Chris@16: { Chris@16: init(); Chris@16: } Chris@16: Chris@16: // compiler-generated copy ctor and assignment operator are fine Chris@16: Chris@16: /** Returns the @c a parameter of the distribution */ Chris@16: result_type a() const { return _a; } Chris@16: /** Returns the @c b parameter of the distribution */ Chris@16: result_type b() const { return _b; } Chris@16: /** Returns the @c c parameter of the distribution */ Chris@16: result_type c() const { return _c; } Chris@16: Chris@16: /** Returns the smallest value that the distribution can produce. */ Chris@16: RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _a; } Chris@16: /** Returns the largest value that the distribution can produce. */ Chris@16: RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return _c; } Chris@16: Chris@16: /** Returns the parameters of the distribution. */ Chris@16: param_type param() const { return param_type(_a, _b, _c); } Chris@16: /** Sets the parameters of the distribution. */ Chris@16: void param(const param_type& parm) Chris@16: { Chris@16: _a = parm.a(); Chris@16: _b = parm.b(); Chris@16: _c = parm.c(); Chris@16: init(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Effects: Subsequent uses of the distribution do not depend Chris@16: * on values produced by any engine prior to invoking reset. Chris@16: */ Chris@16: void reset() { } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the Chris@16: * triangle distribution. Chris@16: */ Chris@16: template Chris@16: result_type operator()(Engine& eng) Chris@16: { Chris@16: using std::sqrt; Chris@16: result_type u = uniform_01<>()(eng); Chris@16: if( u <= q1 ) Chris@16: return _a + p1*sqrt(u); Chris@16: else Chris@16: return _c - d3*sqrt(d2*u-d1); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns a random variate distributed according to the Chris@16: * triangle distribution with parameters specified by param. Chris@16: */ Chris@16: template Chris@16: result_type operator()(Engine& eng, const param_type& parm) Chris@16: { return triangle_distribution(parm)(eng); } Chris@16: Chris@16: /** Writes the distribution to a @c std::ostream. */ Chris@16: BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, triangle_distribution, td) Chris@16: { Chris@16: os << td.param(); Chris@16: return os; Chris@16: } Chris@16: Chris@16: /** Reads the distribution from a @c std::istream. */ Chris@16: BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, triangle_distribution, td) Chris@16: { Chris@16: param_type parm; Chris@16: if(is >> parm) { Chris@16: td.param(parm); Chris@16: } Chris@16: return is; Chris@16: } Chris@16: Chris@16: /** Chris@16: * Returns true if the two distributions will produce identical Chris@16: * sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(triangle_distribution, lhs, rhs) Chris@16: { return lhs._a == rhs._a && lhs._b == rhs._b && lhs._c == rhs._c; } Chris@16: Chris@16: /** Chris@16: * Returns true if the two distributions may produce different Chris@16: * sequences of values given equal generators. Chris@16: */ Chris@16: BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(triangle_distribution) Chris@16: Chris@16: private: Chris@16: /// \cond show_private Chris@16: void init() Chris@16: { Chris@16: using std::sqrt; Chris@16: d1 = _b - _a; Chris@16: d2 = _c - _a; Chris@16: d3 = sqrt(_c - _b); Chris@16: q1 = d1 / d2; Chris@16: p1 = sqrt(d1 * d2); Chris@16: } Chris@16: /// \endcond Chris@16: Chris@16: RealType _a, _b, _c; Chris@16: RealType d1, d2, d3, q1, p1; Chris@16: }; Chris@16: Chris@16: } // namespace random Chris@16: Chris@16: using random::triangle_distribution; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP