]> AND Private Git Repository - canny.git/blob - stc/exp/ml_stc_linux_make_v1.0/include/boost/random/triangle_distribution.hpp
Logo AND Algorithmique Numérique Distribuée

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