normal_distribution.h 6.5 KB

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