sl@0: /* boost random/triangle_distribution.hpp header file sl@0: * sl@0: * Copyright Jens Maurer 2000-2001 sl@0: * Distributed under the Boost Software License, Version 1.0. (See sl@0: * accompanying file LICENSE_1_0.txt or copy at sl@0: * http://www.boost.org/LICENSE_1_0.txt) sl@0: * sl@0: * See http://www.boost.org for most recent version including documentation. sl@0: * sl@0: * $Id: triangle_distribution.hpp,v 1.11 2004/07/27 03:43:32 dgregor Exp $ sl@0: * sl@0: * Revision history sl@0: * 2001-02-18 moved to individual header files sl@0: */ sl@0: sl@0: #ifndef BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP sl@0: #define BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: namespace boost { sl@0: sl@0: // triangle distribution, with a smallest, b most probable, and c largest sl@0: // value. sl@0: template sl@0: class triangle_distribution sl@0: { sl@0: public: sl@0: typedef RealType input_type; sl@0: typedef RealType result_type; sl@0: sl@0: explicit triangle_distribution(result_type a = result_type(0), sl@0: result_type b = result_type(0.5), sl@0: result_type c = result_type(1)) sl@0: : _a(a), _b(b), _c(c) sl@0: { sl@0: assert(_a <= _b && _b <= _c); sl@0: init(); sl@0: } sl@0: sl@0: // compiler-generated copy ctor and assignment operator are fine sl@0: result_type a() const { return _a; } sl@0: result_type b() const { return _b; } sl@0: result_type c() const { return _c; } sl@0: sl@0: void reset() { } sl@0: sl@0: template sl@0: result_type operator()(Engine& eng) sl@0: { sl@0: #ifndef BOOST_NO_STDC_NAMESPACE sl@0: using std::sqrt; sl@0: #endif sl@0: result_type u = eng(); sl@0: if( u <= q1 ) sl@0: return _a + p1*sqrt(u); sl@0: else sl@0: return _c - d3*sqrt(d2*u-d1); sl@0: } sl@0: sl@0: #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) sl@0: template sl@0: friend std::basic_ostream& sl@0: operator<<(std::basic_ostream& os, const triangle_distribution& td) sl@0: { sl@0: os << td._a << " " << td._b << " " << td._c; sl@0: return os; sl@0: } sl@0: sl@0: template sl@0: friend std::basic_istream& sl@0: operator>>(std::basic_istream& is, triangle_distribution& td) sl@0: { sl@0: is >> std::ws >> td._a >> std::ws >> td._b >> std::ws >> td._c; sl@0: td.init(); sl@0: return is; sl@0: } sl@0: #endif sl@0: sl@0: private: sl@0: void init() sl@0: { sl@0: #ifndef BOOST_NO_STDC_NAMESPACE sl@0: using std::sqrt; sl@0: #endif sl@0: d1 = _b - _a; sl@0: d2 = _c - _a; sl@0: d3 = sqrt(_c - _b); sl@0: q1 = d1 / d2; sl@0: p1 = sqrt(d1 * d2); sl@0: } sl@0: sl@0: result_type _a, _b, _c; sl@0: result_type d1, d2, d3, q1, p1; sl@0: }; sl@0: sl@0: } // namespace boost sl@0: sl@0: #endif // BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP