normal_distribution.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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>
  77. _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
  78. // property functions
  79. _LIBCPP_INLINE_VISIBILITY
  80. result_type mean() const {return __p_.mean();}
  81. _LIBCPP_INLINE_VISIBILITY
  82. result_type stddev() const {return __p_.stddev();}
  83. _LIBCPP_INLINE_VISIBILITY
  84. param_type param() const {return __p_;}
  85. _LIBCPP_INLINE_VISIBILITY
  86. void param(const param_type& __p) {__p_ = __p;}
  87. _LIBCPP_INLINE_VISIBILITY
  88. result_type min() const {return -numeric_limits<result_type>::infinity();}
  89. _LIBCPP_INLINE_VISIBILITY
  90. result_type max() const {return numeric_limits<result_type>::infinity();}
  91. friend _LIBCPP_INLINE_VISIBILITY
  92. bool operator==(const normal_distribution& __x,
  93. const normal_distribution& __y)
  94. {return __x.__p_ == __y.__p_ && __x.__v_hot_ == __y.__v_hot_ &&
  95. (!__x.__v_hot_ || __x.__v_ == __y.__v_);}
  96. friend _LIBCPP_INLINE_VISIBILITY
  97. bool operator!=(const normal_distribution& __x,
  98. const normal_distribution& __y)
  99. {return !(__x == __y);}
  100. template <class _CharT, class _Traits, class _RT>
  101. friend
  102. basic_ostream<_CharT, _Traits>&
  103. operator<<(basic_ostream<_CharT, _Traits>& __os,
  104. const normal_distribution<_RT>& __x);
  105. template <class _CharT, class _Traits, class _RT>
  106. friend
  107. basic_istream<_CharT, _Traits>&
  108. operator>>(basic_istream<_CharT, _Traits>& __is,
  109. normal_distribution<_RT>& __x);
  110. };
  111. template <class _RealType>
  112. template<class _URNG>
  113. _RealType
  114. normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
  115. {
  116. static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
  117. result_type __up;
  118. if (__v_hot_)
  119. {
  120. __v_hot_ = false;
  121. __up = __v_;
  122. }
  123. else
  124. {
  125. uniform_real_distribution<result_type> __uni(-1, 1);
  126. result_type __u;
  127. result_type __v;
  128. result_type __s;
  129. do
  130. {
  131. __u = __uni(__g);
  132. __v = __uni(__g);
  133. __s = __u * __u + __v * __v;
  134. } while (__s > 1 || __s == 0);
  135. result_type __fp = _VSTD::sqrt(-2 * _VSTD::log(__s) / __s);
  136. __v_ = __v * __fp;
  137. __v_hot_ = true;
  138. __up = __u * __fp;
  139. }
  140. return __up * __p.stddev() + __p.mean();
  141. }
  142. template <class _CharT, class _Traits, class _RT>
  143. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  144. operator<<(basic_ostream<_CharT, _Traits>& __os,
  145. const normal_distribution<_RT>& __x)
  146. {
  147. __save_flags<_CharT, _Traits> __lx(__os);
  148. typedef basic_ostream<_CharT, _Traits> _OStream;
  149. __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
  150. _OStream::scientific);
  151. _CharT __sp = __os.widen(' ');
  152. __os.fill(__sp);
  153. __os << __x.mean() << __sp << __x.stddev() << __sp << __x.__v_hot_;
  154. if (__x.__v_hot_)
  155. __os << __sp << __x.__v_;
  156. return __os;
  157. }
  158. template <class _CharT, class _Traits, class _RT>
  159. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  160. operator>>(basic_istream<_CharT, _Traits>& __is,
  161. normal_distribution<_RT>& __x)
  162. {
  163. typedef normal_distribution<_RT> _Eng;
  164. typedef typename _Eng::result_type result_type;
  165. typedef typename _Eng::param_type param_type;
  166. __save_flags<_CharT, _Traits> __lx(__is);
  167. typedef basic_istream<_CharT, _Traits> _Istream;
  168. __is.flags(_Istream::dec | _Istream::skipws);
  169. result_type __mean;
  170. result_type __stddev;
  171. result_type __vp = 0;
  172. bool __v_hot = false;
  173. __is >> __mean >> __stddev >> __v_hot;
  174. if (__v_hot)
  175. __is >> __vp;
  176. if (!__is.fail())
  177. {
  178. __x.param(param_type(__mean, __stddev));
  179. __x.__v_hot_ = __v_hot;
  180. __x.__v_ = __vp;
  181. }
  182. return __is;
  183. }
  184. _LIBCPP_END_NAMESPACE_STD
  185. _LIBCPP_POP_MACROS
  186. #endif // _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H