1344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
2344cef66SArthur O'Dwyer //
3344cef66SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344cef66SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5344cef66SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344cef66SArthur O'Dwyer //
7344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
8344cef66SArthur O'Dwyer
9344cef66SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H
10344cef66SArthur O'Dwyer #define _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H
11344cef66SArthur O'Dwyer
12344cef66SArthur O'Dwyer #include <__config>
137624552eSArthur O'Dwyer #include <__random/is_valid.h>
14344cef66SArthur O'Dwyer #include <__random/uniform_real_distribution.h>
15344cef66SArthur O'Dwyer #include <cmath>
16344cef66SArthur O'Dwyer #include <iosfwd>
17344cef66SArthur O'Dwyer #include <limits>
18344cef66SArthur O'Dwyer
19344cef66SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20344cef66SArthur O'Dwyer # pragma GCC system_header
21344cef66SArthur O'Dwyer #endif
22344cef66SArthur O'Dwyer
23344cef66SArthur O'Dwyer _LIBCPP_PUSH_MACROS
24344cef66SArthur O'Dwyer #include <__undef_macros>
25344cef66SArthur O'Dwyer
26344cef66SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
27344cef66SArthur O'Dwyer
28344cef66SArthur O'Dwyer template <class _RealType = double>
29*9783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS normal_distribution {
30727fef79SNhat Nguyen static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
31727fef79SNhat Nguyen "RealType must be a supported floating-point type");
32727fef79SNhat Nguyen
33344cef66SArthur O'Dwyer public:
34344cef66SArthur O'Dwyer // types
35344cef66SArthur O'Dwyer typedef _RealType result_type;
36344cef66SArthur O'Dwyer
37*9783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS param_type {
38344cef66SArthur O'Dwyer result_type __mean_;
39344cef66SArthur O'Dwyer result_type __stddev_;
40*9783f28cSLouis Dionne
41344cef66SArthur O'Dwyer public:
42344cef66SArthur O'Dwyer typedef normal_distribution distribution_type;
43344cef66SArthur O'Dwyer
44*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __mean = 0, result_type __stddev = 1)
__mean_(__mean)45344cef66SArthur O'Dwyer : __mean_(__mean), __stddev_(__stddev) {}
46344cef66SArthur O'Dwyer
mean()47*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type mean() const { return __mean_; }
stddev()48*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type stddev() const { return __stddev_; }
49344cef66SArthur O'Dwyer
50*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
51*9783f28cSLouis Dionne return __x.__mean_ == __y.__mean_ && __x.__stddev_ == __y.__stddev_;
52*9783f28cSLouis Dionne }
53*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
54344cef66SArthur O'Dwyer };
55344cef66SArthur O'Dwyer
56344cef66SArthur O'Dwyer private:
57344cef66SArthur O'Dwyer param_type __p_;
5884fc2c3cSNikolas Klauser result_type __v_;
5984fc2c3cSNikolas Klauser bool __v_hot_;
60344cef66SArthur O'Dwyer
61344cef66SArthur O'Dwyer public:
62344cef66SArthur O'Dwyer // constructors and reset functions
63344cef66SArthur O'Dwyer #ifndef _LIBCPP_CXX03_LANG
normal_distribution()64*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI normal_distribution() : normal_distribution(0) {}
65*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit normal_distribution(result_type __mean, result_type __stddev = 1)
__p_(param_type (__mean,__stddev))6684fc2c3cSNikolas Klauser : __p_(param_type(__mean, __stddev)), __v_hot_(false) {}
67344cef66SArthur O'Dwyer #else
68*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit normal_distribution(result_type __mean = 0, result_type __stddev = 1)
6984fc2c3cSNikolas Klauser : __p_(param_type(__mean, __stddev)), __v_hot_(false) {}
70344cef66SArthur O'Dwyer #endif
normal_distribution(const param_type & __p)71*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI explicit normal_distribution(const param_type& __p) : __p_(__p), __v_hot_(false) {}
reset()72*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI void reset() { __v_hot_ = false; }
73344cef66SArthur O'Dwyer
74344cef66SArthur O'Dwyer // generating functions
75344cef66SArthur O'Dwyer template <class _URNG>
operator()76*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
77*9783f28cSLouis Dionne return (*this)(__g, __p_);
78*9783f28cSLouis Dionne }
7983ce1397SNikolas Klauser template <class _URNG>
8083ce1397SNikolas Klauser _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
81344cef66SArthur O'Dwyer
82344cef66SArthur O'Dwyer // property functions
mean()83*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type mean() const { return __p_.mean(); }
stddev()84*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type stddev() const { return __p_.stddev(); }
85344cef66SArthur O'Dwyer
param()86*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)87*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
88344cef66SArthur O'Dwyer
min()89*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); }
max()90*9783f28cSLouis Dionne _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
91344cef66SArthur O'Dwyer
92*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator==(const normal_distribution& __x, const normal_distribution& __y) {
93*9783f28cSLouis Dionne return __x.__p_ == __y.__p_ && __x.__v_hot_ == __y.__v_hot_ && (!__x.__v_hot_ || __x.__v_ == __y.__v_);
94*9783f28cSLouis Dionne }
95*9783f28cSLouis Dionne friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const normal_distribution& __x, const normal_distribution& __y) {
96*9783f28cSLouis Dionne return !(__x == __y);
97*9783f28cSLouis Dionne }
98344cef66SArthur O'Dwyer
99344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
100*9783f28cSLouis Dionne friend basic_ostream<_CharT, _Traits>&
101*9783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const normal_distribution<_RT>& __x);
102344cef66SArthur O'Dwyer
103344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
104*9783f28cSLouis Dionne friend basic_istream<_CharT, _Traits>&
105*9783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, normal_distribution<_RT>& __x);
106344cef66SArthur O'Dwyer };
107344cef66SArthur O'Dwyer
108344cef66SArthur O'Dwyer template <class _RealType>
109344cef66SArthur O'Dwyer template <class _URNG>
operator()110*9783f28cSLouis Dionne _RealType normal_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
1117624552eSArthur O'Dwyer static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
112d05f8895SNikolas Klauser result_type __up;
113*9783f28cSLouis Dionne if (__v_hot_) {
11484fc2c3cSNikolas Klauser __v_hot_ = false;
115d05f8895SNikolas Klauser __up = __v_;
116*9783f28cSLouis Dionne } else {
117d05f8895SNikolas Klauser uniform_real_distribution<result_type> __uni(-1, 1);
118344cef66SArthur O'Dwyer result_type __u;
119344cef66SArthur O'Dwyer result_type __v;
120344cef66SArthur O'Dwyer result_type __s;
121*9783f28cSLouis Dionne do {
122d05f8895SNikolas Klauser __u = __uni(__g);
123d05f8895SNikolas Klauser __v = __uni(__g);
124344cef66SArthur O'Dwyer __s = __u * __u + __v * __v;
125344cef66SArthur O'Dwyer } while (__s > 1 || __s == 0);
12677a00c0dSLouis Dionne result_type __fp = std::sqrt(-2 * std::log(__s) / __s);
127d05f8895SNikolas Klauser __v_ = __v * __fp;
12884fc2c3cSNikolas Klauser __v_hot_ = true;
129d05f8895SNikolas Klauser __up = __u * __fp;
130344cef66SArthur O'Dwyer }
131d05f8895SNikolas Klauser return __up * __p.stddev() + __p.mean();
132344cef66SArthur O'Dwyer }
133344cef66SArthur O'Dwyer
134344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
13580c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
136*9783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const normal_distribution<_RT>& __x) {
137344cef66SArthur O'Dwyer __save_flags<_CharT, _Traits> __lx(__os);
138344cef66SArthur O'Dwyer typedef basic_ostream<_CharT, _Traits> _OStream;
139*9783f28cSLouis Dionne __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
140344cef66SArthur O'Dwyer _CharT __sp = __os.widen(' ');
141344cef66SArthur O'Dwyer __os.fill(__sp);
14284fc2c3cSNikolas Klauser __os << __x.mean() << __sp << __x.stddev() << __sp << __x.__v_hot_;
14384fc2c3cSNikolas Klauser if (__x.__v_hot_)
14484fc2c3cSNikolas Klauser __os << __sp << __x.__v_;
145344cef66SArthur O'Dwyer return __os;
146344cef66SArthur O'Dwyer }
147344cef66SArthur O'Dwyer
148344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
14980c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
150*9783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, normal_distribution<_RT>& __x) {
151344cef66SArthur O'Dwyer typedef normal_distribution<_RT> _Eng;
152344cef66SArthur O'Dwyer typedef typename _Eng::result_type result_type;
153344cef66SArthur O'Dwyer typedef typename _Eng::param_type param_type;
154344cef66SArthur O'Dwyer __save_flags<_CharT, _Traits> __lx(__is);
155344cef66SArthur O'Dwyer typedef basic_istream<_CharT, _Traits> _Istream;
156344cef66SArthur O'Dwyer __is.flags(_Istream::dec | _Istream::skipws);
157344cef66SArthur O'Dwyer result_type __mean;
158344cef66SArthur O'Dwyer result_type __stddev;
159d05f8895SNikolas Klauser result_type __vp = 0;
160d05f8895SNikolas Klauser bool __v_hot = false;
161d05f8895SNikolas Klauser __is >> __mean >> __stddev >> __v_hot;
162d05f8895SNikolas Klauser if (__v_hot)
163d05f8895SNikolas Klauser __is >> __vp;
164*9783f28cSLouis Dionne if (!__is.fail()) {
165344cef66SArthur O'Dwyer __x.param(param_type(__mean, __stddev));
166d05f8895SNikolas Klauser __x.__v_hot_ = __v_hot;
167d05f8895SNikolas Klauser __x.__v_ = __vp;
168344cef66SArthur O'Dwyer }
169344cef66SArthur O'Dwyer return __is;
170344cef66SArthur O'Dwyer }
171344cef66SArthur O'Dwyer
172344cef66SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
173344cef66SArthur O'Dwyer
174344cef66SArthur O'Dwyer _LIBCPP_POP_MACROS
175344cef66SArthur O'Dwyer
176344cef66SArthur O'Dwyer #endif // _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H
177