1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H
12 #include <__cxx03/__config>
13 #include <__cxx03/__random/is_valid.h>
14 #include <__cxx03/__random/uniform_real_distribution.h>
15 #include <__cxx03/cmath>
16 #include <__cxx03/iosfwd>
17 #include <__cxx03/limits>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
24 #include <__cxx03/__undef_macros>
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 template <class _RealType
= double>
29 class _LIBCPP_TEMPLATE_VIS normal_distribution
{
30 static_assert(__libcpp_random_is_valid_realtype
<_RealType
>::value
,
31 "RealType must be a supported floating-point type");
35 typedef _RealType result_type
;
37 class _LIBCPP_TEMPLATE_VIS param_type
{
39 result_type __stddev_
;
42 typedef normal_distribution distribution_type
;
44 _LIBCPP_HIDE_FROM_ABI
explicit param_type(result_type __mean
= 0, result_type __stddev
= 1)
45 : __mean_(__mean
), __stddev_(__stddev
) {}
47 _LIBCPP_HIDE_FROM_ABI result_type
mean() const { return __mean_
; }
48 _LIBCPP_HIDE_FROM_ABI result_type
stddev() const { return __stddev_
; }
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_
;
53 friend _LIBCPP_HIDE_FROM_ABI
bool operator!=(const param_type
& __x
, const param_type
& __y
) { return !(__x
== __y
); }
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) {}
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) {}
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; }
74 // generating functions
75 template <class _URNG
>
76 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
) {
77 return (*this)(__g
, __p_
);
79 template <class _URNG
>
80 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
, const param_type
& __p
);
83 _LIBCPP_HIDE_FROM_ABI result_type
mean() const { return __p_
.mean(); }
84 _LIBCPP_HIDE_FROM_ABI result_type
stddev() const { return __p_
.stddev(); }
86 _LIBCPP_HIDE_FROM_ABI param_type
param() const { return __p_
; }
87 _LIBCPP_HIDE_FROM_ABI
void param(const param_type
& __p
) { __p_
= __p
; }
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(); }
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_
);
95 friend _LIBCPP_HIDE_FROM_ABI
bool operator!=(const normal_distribution
& __x
, const normal_distribution
& __y
) {
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
);
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
);
108 template <class _RealType
>
109 template <class _URNG
>
110 _RealType normal_distribution
<_RealType
>::operator()(_URNG
& __g
, const param_type
& __p
) {
111 static_assert(__libcpp_random_is_valid_urng
<_URNG
>::value
, "");
117 uniform_real_distribution
<result_type
> __uni(-1, 1);
124 __s
= __u
* __u
+ __v
* __v
;
125 } while (__s
> 1 || __s
== 0);
126 result_type __fp
= std::sqrt(-2 * std::log(__s
) / __s
);
131 return __up
* __p
.stddev() + __p
.mean();
134 template <class _CharT
, class _Traits
, class _RT
>
135 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
136 operator<<(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(' ');
142 __os
<< __x
.mean() << __sp
<< __x
.stddev() << __sp
<< __x
.__v_hot_
;
144 __os
<< __sp
<< __x
.__v_
;
148 template <class _CharT
, class _Traits
, class _RT
>
149 _LIBCPP_HIDE_FROM_ABI basic_istream
<_CharT
, _Traits
>&
150 operator>>(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
);
158 result_type __stddev
;
159 result_type __vp
= 0;
160 bool __v_hot
= false;
161 __is
>> __mean
>> __stddev
>> __v_hot
;
165 __x
.param(param_type(__mean
, __stddev
));
166 __x
.__v_hot_
= __v_hot
;
172 _LIBCPP_END_NAMESPACE_STD
176 #endif // _LIBCPP___RANDOM_NORMAL_DISTRIBUTION_H