File indexing completed on 2026-05-03 08:13:39
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___CXX03___RANDOM_NORMAL_DISTRIBUTION_H
0010 #define _LIBCPP___CXX03___RANDOM_NORMAL_DISTRIBUTION_H
0011
0012 #include <__cxx03/__config>
0013 #include <__cxx03/__random/is_valid.h>
0014 #include <__cxx03/__random/uniform_real_distribution.h>
0015 #include <__cxx03/cmath>
0016 #include <__cxx03/iosfwd>
0017 #include <__cxx03/limits>
0018
0019 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0020 # pragma GCC system_header
0021 #endif
0022
0023 _LIBCPP_PUSH_MACROS
0024 #include <__cxx03/__undef_macros>
0025
0026 _LIBCPP_BEGIN_NAMESPACE_STD
0027
0028 template <class _RealType = double>
0029 class _LIBCPP_TEMPLATE_VIS normal_distribution {
0030 static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
0031 "RealType must be a supported floating-point type");
0032
0033 public:
0034
0035 typedef _RealType result_type;
0036
0037 class _LIBCPP_TEMPLATE_VIS param_type {
0038 result_type __mean_;
0039 result_type __stddev_;
0040
0041 public:
0042 typedef normal_distribution distribution_type;
0043
0044 _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __mean = 0, result_type __stddev = 1)
0045 : __mean_(__mean), __stddev_(__stddev) {}
0046
0047 _LIBCPP_HIDE_FROM_ABI result_type mean() const { return __mean_; }
0048 _LIBCPP_HIDE_FROM_ABI result_type stddev() const { return __stddev_; }
0049
0050 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0051 return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;
0052 }
0053 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0054 };
0055
0056 private:
0057 param_type __p_;
0058 result_type __v_;
0059 bool __v_hot_;
0060
0061 public:
0062
0063 #ifndef _LIBCPP_CXX03_LANG
0064 _LIBCPP_HIDE_FROM_ABI normal_distribution() : normal_distribution(0) {}
0065 _LIBCPP_HIDE_FROM_ABI explicit normal_distribution(result_type __mean, result_type __stddev = 1)
0066 : __p_(param_type(__mean, __stddev)), __v_hot_(false) {}
0067 #else
0068 _LIBCPP_HIDE_FROM_ABI explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
0069 : __p_(param_type(__mean, __stddev)), __v_hot_(false) {}
0070 #endif
0071 _LIBCPP_HIDE_FROM_ABI explicit normal_distribution(const param_type& __p) : __p_(__p), __v_hot_(false) {}
0072 _LIBCPP_HIDE_FROM_ABI void reset() { __v_hot_ = false; }
0073
0074
0075 template <class _URNG>
0076 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0077 return (*this)(__g, __p_);
0078 }
0079 template <class _URNG>
0080 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
0081
0082
0083 _LIBCPP_HIDE_FROM_ABI result_type mean() const { return __p_.mean(); }
0084 _LIBCPP_HIDE_FROM_ABI result_type stddev() const { return __p_.stddev(); }
0085
0086 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0087 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0088
0089 _LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); }
0090 _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
0091
0092 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const normal_distribution& __x, const normal_distribution& __y) {
0093 return __x.__p_ == __y.__p_ && __x.__v_hot_ == __y.__v_hot_ && (!__x.__v_hot_ || __x.__v_ == __y.__v_);
0094 }
0095 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const normal_distribution& __x, const normal_distribution& __y) {
0096 return !(__x == __y);
0097 }
0098
0099 template <class _CharT, class _Traits, class _RT>
0100 friend basic_ostream<_CharT, _Traits>&
0101 operator<<(basic_ostream<_CharT, _Traits>& __os, const normal_distribution<_RT>& __x);
0102
0103 template <class _CharT, class _Traits, class _RT>
0104 friend basic_istream<_CharT, _Traits>&
0105 operator>>(basic_istream<_CharT, _Traits>& __is, normal_distribution<_RT>& __x);
0106 };
0107
0108 template <class _RealType>
0109 template <class _URNG>
0110 _RealType normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
0111 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0112 result_type __up;
0113 if (__v_hot_) {
0114 __v_hot_ = false;
0115 __up = __v_;
0116 } else {
0117 uniform_real_distribution<result_type> __uni(-1, 1);
0118 result_type __u;
0119 result_type __v;
0120 result_type __s;
0121 do {
0122 __u = __uni(__g);
0123 __v = __uni(__g);
0124 __s = __u * __u + __v * __v;
0125 } while (__s > 1 || __s == 0);
0126 result_type __fp = std::sqrt(-2 * std::log(__s) / __s);
0127 __v_ = __v * __fp;
0128 __v_hot_ = true;
0129 __up = __u * __fp;
0130 }
0131 return __up * __p.stddev() + __p.mean();
0132 }
0133
0134 template <class _CharT, class _Traits, class _RT>
0135 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0136 operator<<(basic_ostream<_CharT, _Traits>& __os, const normal_distribution<_RT>& __x) {
0137 __save_flags<_CharT, _Traits> __lx(__os);
0138 typedef basic_ostream<_CharT, _Traits> _OStream;
0139 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0140 _CharT __sp = __os.widen(' ');
0141 __os.fill(__sp);
0142 __os << __x.mean() << __sp << __x.stddev() << __sp << __x.__v_hot_;
0143 if (__x.__v_hot_)
0144 __os << __sp << __x.__v_;
0145 return __os;
0146 }
0147
0148 template <class _CharT, class _Traits, class _RT>
0149 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0150 operator>>(basic_istream<_CharT, _Traits>& __is, normal_distribution<_RT>& __x) {
0151 typedef normal_distribution<_RT> _Eng;
0152 typedef typename _Eng::result_type result_type;
0153 typedef typename _Eng::param_type param_type;
0154 __save_flags<_CharT, _Traits> __lx(__is);
0155 typedef basic_istream<_CharT, _Traits> _Istream;
0156 __is.flags(_Istream::dec | _Istream::skipws);
0157 result_type __mean;
0158 result_type __stddev;
0159 result_type __vp = 0;
0160 bool __v_hot = false;
0161 __is >> __mean >> __stddev >> __v_hot;
0162 if (__v_hot)
0163 __is >> __vp;
0164 if (!__is.fail()) {
0165 __x.param(param_type(__mean, __stddev));
0166 __x.__v_hot_ = __v_hot;
0167 __x.__v_ = __vp;
0168 }
0169 return __is;
0170 }
0171
0172 _LIBCPP_END_NAMESPACE_STD
0173
0174 _LIBCPP_POP_MACROS
0175
0176 #endif