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