1 | /* boost random/triangle_distribution.hpp header file
|
---|
2 | *
|
---|
3 | * Copyright Jens Maurer 2000-2001
|
---|
4 | * Distributed under the Boost Software License, Version 1.0. (See
|
---|
5 | * accompanying file LICENSE_1_0.txt or copy at
|
---|
6 | * http://www.boost.org/LICENSE_1_0.txt)
|
---|
7 | *
|
---|
8 | * See http://www.boost.org for most recent version including documentation.
|
---|
9 | *
|
---|
10 | * $Id: triangle_distribution.hpp,v 1.11 2004/07/27 03:43:32 dgregor Exp $
|
---|
11 | *
|
---|
12 | * Revision history
|
---|
13 | * 2001-02-18 moved to individual header files
|
---|
14 | */
|
---|
15 |
|
---|
16 | #ifndef BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
|
---|
17 | #define BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
|
---|
18 |
|
---|
19 | #include <cmath>
|
---|
20 | #include <cassert>
|
---|
21 | #include <boost/random/uniform_01.hpp>
|
---|
22 |
|
---|
23 | namespace boost {
|
---|
24 |
|
---|
25 | // triangle distribution, with a smallest, b most probable, and c largest
|
---|
26 | // value.
|
---|
27 | template<class RealType = double>
|
---|
28 | class triangle_distribution
|
---|
29 | {
|
---|
30 | public:
|
---|
31 | typedef RealType input_type;
|
---|
32 | typedef RealType result_type;
|
---|
33 |
|
---|
34 | explicit triangle_distribution(result_type a = result_type(0),
|
---|
35 | result_type b = result_type(0.5),
|
---|
36 | result_type c = result_type(1))
|
---|
37 | : _a(a), _b(b), _c(c)
|
---|
38 | {
|
---|
39 | assert(_a <= _b && _b <= _c);
|
---|
40 | init();
|
---|
41 | }
|
---|
42 |
|
---|
43 | // compiler-generated copy ctor and assignment operator are fine
|
---|
44 | result_type a() const { return _a; }
|
---|
45 | result_type b() const { return _b; }
|
---|
46 | result_type c() const { return _c; }
|
---|
47 |
|
---|
48 | void reset() { }
|
---|
49 |
|
---|
50 | template<class Engine>
|
---|
51 | result_type operator()(Engine& eng)
|
---|
52 | {
|
---|
53 | #ifndef BOOST_NO_STDC_NAMESPACE
|
---|
54 | using std::sqrt;
|
---|
55 | #endif
|
---|
56 | result_type u = eng();
|
---|
57 | if( u <= q1 )
|
---|
58 | return _a + p1*sqrt(u);
|
---|
59 | else
|
---|
60 | return _c - d3*sqrt(d2*u-d1);
|
---|
61 | }
|
---|
62 |
|
---|
63 | #if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
|
---|
64 | template<class CharT, class Traits>
|
---|
65 | friend std::basic_ostream<CharT,Traits>&
|
---|
66 | operator<<(std::basic_ostream<CharT,Traits>& os, const triangle_distribution& td)
|
---|
67 | {
|
---|
68 | os << td._a << " " << td._b << " " << td._c;
|
---|
69 | return os;
|
---|
70 | }
|
---|
71 |
|
---|
72 | template<class CharT, class Traits>
|
---|
73 | friend std::basic_istream<CharT,Traits>&
|
---|
74 | operator>>(std::basic_istream<CharT,Traits>& is, triangle_distribution& td)
|
---|
75 | {
|
---|
76 | is >> std::ws >> td._a >> std::ws >> td._b >> std::ws >> td._c;
|
---|
77 | td.init();
|
---|
78 | return is;
|
---|
79 | }
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | private:
|
---|
83 | void init()
|
---|
84 | {
|
---|
85 | #ifndef BOOST_NO_STDC_NAMESPACE
|
---|
86 | using std::sqrt;
|
---|
87 | #endif
|
---|
88 | d1 = _b - _a;
|
---|
89 | d2 = _c - _a;
|
---|
90 | d3 = sqrt(_c - _b);
|
---|
91 | q1 = d1 / d2;
|
---|
92 | p1 = sqrt(d1 * d2);
|
---|
93 | }
|
---|
94 |
|
---|
95 | result_type _a, _b, _c;
|
---|
96 | result_type d1, d2, d3, q1, p1;
|
---|
97 | };
|
---|
98 |
|
---|
99 | } // namespace boost
|
---|
100 |
|
---|
101 | #endif // BOOST_RANDOM_TRIANGLE_DISTRIBUTION_HPP
|
---|