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

Private GIT Repository
15cecec1da76dbc645432c554f3a2b71fb6809b8
[canny.git] / stc / exp / ml_stc_linux_make_v1.0 / include / boost / random / normal_distribution.hpp
1 /* boost random/normal_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: normal_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_NORMAL_DISTRIBUTION_HPP\r
17 #define BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP\r
18 \r
19 #include <boost/config/no_tr1/cmath.hpp>\r
20 #include <cassert>\r
21 #include <iostream>\r
22 #include <boost/limits.hpp>\r
23 #include <boost/static_assert.hpp>\r
24 #include <boost/random/detail/config.hpp>\r
25 \r
26 namespace boost {\r
27 \r
28 // deterministic Box-Muller method, uses trigonometric functions\r
29 template<class RealType = double>\r
30 class normal_distribution\r
31 {\r
32 public:\r
33   typedef RealType input_type;\r
34   typedef RealType result_type;\r
35 \r
36 #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)\r
37     BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);\r
38 #endif\r
39 \r
40   explicit normal_distribution(const result_type& mean_arg = result_type(0),\r
41                                const result_type& sigma_arg = result_type(1))\r
42     : _mean(mean_arg), _sigma(sigma_arg), _valid(false)\r
43   {\r
44     assert(_sigma >= result_type(0));\r
45   }\r
46 \r
47   // compiler-generated copy constructor is NOT fine, need to purge cache\r
48   normal_distribution(const normal_distribution& other)\r
49     : _mean(other._mean), _sigma(other._sigma), _valid(false)\r
50   {\r
51   }\r
52 \r
53   // compiler-generated copy ctor and assignment operator are fine\r
54 \r
55   RealType mean() const { return _mean; }\r
56   RealType sigma() const { return _sigma; }\r
57 \r
58   void reset() { _valid = false; }\r
59 \r
60   template<class Engine>\r
61   result_type operator()(Engine& eng)\r
62   {\r
63 #ifndef BOOST_NO_STDC_NAMESPACE\r
64     // allow for Koenig lookup\r
65     using std::sqrt; using std::log; using std::sin; using std::cos;\r
66 #endif\r
67     if(!_valid) {\r
68       _r1 = eng();\r
69       _r2 = eng();\r
70       _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));\r
71       _valid = true;\r
72     } else {\r
73       _valid = false;\r
74     }\r
75     // Can we have a boost::mathconst please?\r
76     const result_type pi = result_type(3.14159265358979323846);\r
77     \r
78     return _cached_rho * (_valid ?\r
79                           cos(result_type(2)*pi*_r1) :\r
80                           sin(result_type(2)*pi*_r1))\r
81       * _sigma + _mean;\r
82   }\r
83 \r
84 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS\r
85   template<class CharT, class Traits>\r
86   friend std::basic_ostream<CharT,Traits>&\r
87   operator<<(std::basic_ostream<CharT,Traits>& os, const normal_distribution& nd)\r
88   {\r
89     os << nd._mean << " " << nd._sigma << " "\r
90        << nd._valid << " " << nd._cached_rho << " " << nd._r1;\r
91     return os;\r
92   }\r
93 \r
94   template<class CharT, class Traits>\r
95   friend std::basic_istream<CharT,Traits>&\r
96   operator>>(std::basic_istream<CharT,Traits>& is, normal_distribution& nd)\r
97   {\r
98     is >> std::ws >> nd._mean >> std::ws >> nd._sigma\r
99        >> std::ws >> nd._valid >> std::ws >> nd._cached_rho\r
100        >> std::ws >> nd._r1;\r
101     return is;\r
102   }\r
103 #endif\r
104 private:\r
105   result_type _mean, _sigma;\r
106   result_type _r1, _r2, _cached_rho;\r
107   bool _valid;\r
108 };\r
109 \r
110 } // namespace boost\r
111 \r
112 #endif // BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP\r