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