/src/contrib/boost/random/normal_distribution.hpp

http://pythonocc.googlecode.com/ · C++ Header · 131 lines · 77 code · 16 blank · 38 comment · 4 complexity · 38558782e1747b4a9f3c5751745517b9 MD5 · raw file

  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 60755 2010-03-22 00:45:06Z steven_watanabe $
  11. *
  12. * Revision history
  13. * 2001-02-18 moved to individual header files
  14. */
  15. #ifndef BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
  16. #define BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP
  17. #include <boost/config/no_tr1/cmath.hpp>
  18. #include <cassert>
  19. #include <iostream>
  20. #include <boost/limits.hpp>
  21. #include <boost/static_assert.hpp>
  22. #include <boost/random/detail/config.hpp>
  23. namespace boost {
  24. /**
  25. * Instantiations of class template normal_distribution model a
  26. * \random_distribution. Such a distribution produces random numbers
  27. * @c x distributed with probability density function
  28. * \f$p(x) = \frac{1}{\sqrt{2\pi\sigma}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}\f$,
  29. * where mean and sigma are the parameters of the distribution.
  30. */
  31. // deterministic Box-Muller method, uses trigonometric functions
  32. template<class RealType = double>
  33. class normal_distribution
  34. {
  35. public:
  36. typedef RealType input_type;
  37. typedef RealType result_type;
  38. #if !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) && !(defined(BOOST_MSVC) && BOOST_MSVC <= 1300)
  39. BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
  40. #endif
  41. /**
  42. * Constructs a normal_distribution object. @c mean and @c sigma are
  43. * the parameters for the distribution.
  44. *
  45. * Requires: sigma > 0
  46. */
  47. explicit normal_distribution(const result_type& mean_arg = result_type(0),
  48. const result_type& sigma_arg = result_type(1))
  49. : _mean(mean_arg), _sigma(sigma_arg), _valid(false)
  50. {
  51. assert(_sigma >= result_type(0));
  52. }
  53. // compiler-generated copy constructor is NOT fine, need to purge cache
  54. normal_distribution(const normal_distribution& other)
  55. : _mean(other._mean), _sigma(other._sigma), _valid(false)
  56. {
  57. }
  58. // compiler-generated copy ctor and assignment operator are fine
  59. /**
  60. * Returns: The "mean" parameter of the distribution.
  61. */
  62. RealType mean() const { return _mean; }
  63. /**
  64. * Returns: The "sigma" parameter of the distribution.
  65. */
  66. RealType sigma() const { return _sigma; }
  67. void reset() { _valid = false; }
  68. template<class Engine>
  69. result_type operator()(Engine& eng)
  70. {
  71. #ifndef BOOST_NO_STDC_NAMESPACE
  72. // allow for Koenig lookup
  73. using std::sqrt; using std::log; using std::sin; using std::cos;
  74. #endif
  75. if(!_valid) {
  76. _r1 = eng();
  77. _r2 = eng();
  78. _cached_rho = sqrt(-result_type(2) * log(result_type(1)-_r2));
  79. _valid = true;
  80. } else {
  81. _valid = false;
  82. }
  83. // Can we have a boost::mathconst please?
  84. const result_type pi = result_type(3.14159265358979323846);
  85. return _cached_rho * (_valid ?
  86. cos(result_type(2)*pi*_r1) :
  87. sin(result_type(2)*pi*_r1))
  88. * _sigma + _mean;
  89. }
  90. #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
  91. template<class CharT, class Traits>
  92. friend std::basic_ostream<CharT,Traits>&
  93. operator<<(std::basic_ostream<CharT,Traits>& os, const normal_distribution& nd)
  94. {
  95. os << nd._mean << " " << nd._sigma << " "
  96. << nd._valid << " " << nd._cached_rho << " " << nd._r1;
  97. return os;
  98. }
  99. template<class CharT, class Traits>
  100. friend std::basic_istream<CharT,Traits>&
  101. operator>>(std::basic_istream<CharT,Traits>& is, normal_distribution& nd)
  102. {
  103. is >> std::ws >> nd._mean >> std::ws >> nd._sigma
  104. >> std::ws >> nd._valid >> std::ws >> nd._cached_rho
  105. >> std::ws >> nd._r1;
  106. return is;
  107. }
  108. #endif
  109. private:
  110. result_type _mean, _sigma;
  111. result_type _r1, _r2, _cached_rho;
  112. bool _valid;
  113. };
  114. } // namespace boost
  115. #endif // BOOST_RANDOM_NORMAL_DISTRIBUTION_HPP