normal_distribution.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H
  9. #define _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H
  10. #include <__config>
  11. #include <__random/uniform_real_distribution.h>
  12. #include <cmath>
  13. #include <iosfwd>
  14. #include <limits>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_PUSH_MACROS
  19. #include <__undef_macros>
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. template<class _RealType = double>
  22. class _LIBCPP_TEMPLATE_VIS normal_distribution
  23. {
  24. public:
  25. // types
  26. typedef _RealType result_type;
  27. class _LIBCPP_TEMPLATE_VIS param_type
  28. {
  29. result_type __mean_;
  30. result_type __stddev_;
  31. public:
  32. typedef normal_distribution distribution_type;
  33. _LIBCPP_INLINE_VISIBILITY
  34. explicit param_type(result_type __mean = 0, result_type __stddev = 1)
  35. : __mean_(__mean), __stddev_(__stddev) {}
  36. _LIBCPP_INLINE_VISIBILITY
  37. result_type mean() const {return __mean_;}
  38. _LIBCPP_INLINE_VISIBILITY
  39. result_type stddev() const {return __stddev_;}
  40. friend _LIBCPP_INLINE_VISIBILITY
  41. bool operator==(const param_type& __x, const param_type& __y)
  42. {return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;}
  43. friend _LIBCPP_INLINE_VISIBILITY
  44. bool operator!=(const param_type& __x, const param_type& __y)
  45. {return !(__x == __y);}
  46. };
  47. private:
  48. param_type __p_;
  49. result_type _V_;
  50. bool _V_hot_;
  51. public:
  52. // constructors and reset functions
  53. #ifndef _LIBCPP_CXX03_LANG
  54. _LIBCPP_INLINE_VISIBILITY
  55. normal_distribution() : normal_distribution(0) {}
  56. _LIBCPP_INLINE_VISIBILITY
  57. explicit normal_distribution(result_type __mean, result_type __stddev = 1)
  58. : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
  59. #else
  60. _LIBCPP_INLINE_VISIBILITY
  61. explicit normal_distribution(result_type __mean = 0,
  62. result_type __stddev = 1)
  63. : __p_(param_type(__mean, __stddev)), _V_hot_(false) {}
  64. #endif
  65. _LIBCPP_INLINE_VISIBILITY
  66. explicit normal_distribution(const param_type& __p)
  67. : __p_(__p), _V_hot_(false) {}
  68. _LIBCPP_INLINE_VISIBILITY
  69. void reset() {_V_hot_ = false;}
  70. // generating functions
  71. template<class _URNG>
  72. _LIBCPP_INLINE_VISIBILITY
  73. result_type operator()(_URNG& __g)
  74. {return (*this)(__g, __p_);}
  75. template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
  76. // property functions
  77. _LIBCPP_INLINE_VISIBILITY
  78. result_type mean() const {return __p_.mean();}
  79. _LIBCPP_INLINE_VISIBILITY
  80. result_type stddev() const {return __p_.stddev();}
  81. _LIBCPP_INLINE_VISIBILITY
  82. param_type param() const {return __p_;}
  83. _LIBCPP_INLINE_VISIBILITY
  84. void param(const param_type& __p) {__p_ = __p;}
  85. _LIBCPP_INLINE_VISIBILITY
  86. result_type min() const {return -numeric_limits<result_type>::infinity();}
  87. _LIBCPP_INLINE_VISIBILITY
  88. result_type max() const {return numeric_limits<result_type>::infinity();}
  89. friend _LIBCPP_INLINE_VISIBILITY
  90. bool operator==(const normal_distribution& __x,
  91. const normal_distribution& __y)
  92. {return __x.__p_ == __y.__p_ && __x._V_hot_ == __y._V_hot_ &&
  93. (!__x._V_hot_ || __x._V_ == __y._V_);}
  94. friend _LIBCPP_INLINE_VISIBILITY
  95. bool operator!=(const normal_distribution& __x,
  96. const normal_distribution& __y)
  97. {return !(__x == __y);}
  98. template <class _CharT, class _Traits, class _RT>
  99. friend
  100. basic_ostream<_CharT, _Traits>&
  101. operator<<(basic_ostream<_CharT, _Traits>& __os,
  102. const normal_distribution<_RT>& __x);
  103. template <class _CharT, class _Traits, class _RT>
  104. friend
  105. basic_istream<_CharT, _Traits>&
  106. operator>>(basic_istream<_CharT, _Traits>& __is,
  107. normal_distribution<_RT>& __x);
  108. };
  109. template <class _RealType>
  110. template<class _URNG>
  111. _RealType
  112. normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  113. {
  114. result_type _Up;
  115. if (_V_hot_)
  116. {
  117. _V_hot_ = false;
  118. _Up = _V_;
  119. }
  120. else
  121. {
  122. uniform_real_distribution<result_type> _Uni(-1, 1);
  123. result_type __u;
  124. result_type __v;
  125. result_type __s;
  126. do
  127. {
  128. __u = _Uni(__g);
  129. __v = _Uni(__g);
  130. __s = __u * __u + __v * __v;
  131. } while (__s > 1 || __s == 0);
  132. result_type _Fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
  133. _V_ = __v * _Fp;
  134. _V_hot_ = true;
  135. _Up = __u * _Fp;
  136. }
  137. return _Up * __p.stddev() + __p.mean();
  138. }
  139. template <class _CharT, class _Traits, class _RT>
  140. basic_ostream<_CharT, _Traits>&
  141. operator<<(basic_ostream<_CharT, _Traits>& __os,
  142. const normal_distribution<_RT>& __x)
  143. {
  144. __save_flags<_CharT, _Traits> __lx(__os);
  145. typedef basic_ostream<_CharT, _Traits> _OStream;
  146. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
  147. _OStream::scientific);
  148. _CharT __sp = __os.widen(' ');
  149. __os.fill(__sp);
  150. __os << __x.mean() << __sp << __x.stddev() << __sp << __x._V_hot_;
  151. if (__x._V_hot_)
  152. __os << __sp << __x._V_;
  153. return __os;
  154. }
  155. template <class _CharT, class _Traits, class _RT>
  156. basic_istream<_CharT, _Traits>&
  157. operator>>(basic_istream<_CharT, _Traits>& __is,
  158. normal_distribution<_RT>& __x)
  159. {
  160. typedef normal_distribution<_RT> _Eng;
  161. typedef typename _Eng::result_type result_type;
  162. typedef typename _Eng::param_type param_type;
  163. __save_flags<_CharT, _Traits> __lx(__is);
  164. typedef basic_istream<_CharT, _Traits> _Istream;
  165. __is.flags(_Istream::dec | _Istream::skipws);
  166. result_type __mean;
  167. result_type __stddev;
  168. result_type _Vp = 0;
  169. bool _V_hot = false;
  170. __is >> __mean >> __stddev >> _V_hot;
  171. if (_V_hot)
  172. __is >> _Vp;
  173. if (!__is.fail())
  174. {
  175. __x.param(param_type(__mean, __stddev));
  176. __x._V_hot_ = _V_hot;
  177. __x._V_ = _Vp;
  178. }
  179. return __is;
  180. }
  181. _LIBCPP_END_NAMESPACE_STD
  182. _LIBCPP_POP_MACROS
  183. #endif // _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H