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