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_CAUCHY_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_CAUCHY_DISTRIBUTION_H
13 #include <__random/uniform_real_distribution.h>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
23 #include <__undef_macros>
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 template<class _RealType
= double>
28 class _LIBCPP_TEMPLATE_VIS cauchy_distribution
32 typedef _RealType result_type
;
34 class _LIBCPP_TEMPLATE_VIS param_type
39 typedef cauchy_distribution distribution_type
;
41 _LIBCPP_INLINE_VISIBILITY
42 explicit param_type(result_type __a
= 0, result_type __b
= 1)
43 : __a_(__a
), __b_(__b
) {}
45 _LIBCPP_INLINE_VISIBILITY
46 result_type
a() const {return __a_
;}
47 _LIBCPP_INLINE_VISIBILITY
48 result_type
b() const {return __b_
;}
50 friend _LIBCPP_INLINE_VISIBILITY
51 bool operator==(const param_type
& __x
, const param_type
& __y
)
52 {return __x
.__a_
== __y
.__a_
&& __x
.__b_
== __y
.__b_
;}
53 friend _LIBCPP_INLINE_VISIBILITY
54 bool operator!=(const param_type
& __x
, const param_type
& __y
)
55 {return !(__x
== __y
);}
62 // constructor and reset functions
63 #ifndef _LIBCPP_CXX03_LANG
64 _LIBCPP_INLINE_VISIBILITY
65 cauchy_distribution() : cauchy_distribution(0) {}
66 _LIBCPP_INLINE_VISIBILITY
67 explicit cauchy_distribution(result_type __a
, result_type __b
= 1)
68 : __p_(param_type(__a
, __b
)) {}
70 _LIBCPP_INLINE_VISIBILITY
71 explicit cauchy_distribution(result_type __a
= 0, result_type __b
= 1)
72 : __p_(param_type(__a
, __b
)) {}
74 _LIBCPP_INLINE_VISIBILITY
75 explicit cauchy_distribution(const param_type
& __p
)
77 _LIBCPP_INLINE_VISIBILITY
80 // generating functions
82 _LIBCPP_INLINE_VISIBILITY
83 result_type
operator()(_URNG
& __g
)
84 {return (*this)(__g
, __p_
);}
85 template<class _URNG
> _LIBCPP_INLINE_VISIBILITY result_type
operator()(_URNG
& __g
, const param_type
& __p
);
88 _LIBCPP_INLINE_VISIBILITY
89 result_type
a() const {return __p_
.a();}
90 _LIBCPP_INLINE_VISIBILITY
91 result_type
b() const {return __p_
.b();}
93 _LIBCPP_INLINE_VISIBILITY
94 param_type
param() const {return __p_
;}
95 _LIBCPP_INLINE_VISIBILITY
96 void param(const param_type
& __p
) {__p_
= __p
;}
98 _LIBCPP_INLINE_VISIBILITY
99 result_type
min() const {return -numeric_limits
<result_type
>::infinity();}
100 _LIBCPP_INLINE_VISIBILITY
101 result_type
max() const {return numeric_limits
<result_type
>::infinity();}
103 friend _LIBCPP_INLINE_VISIBILITY
104 bool operator==(const cauchy_distribution
& __x
,
105 const cauchy_distribution
& __y
)
106 {return __x
.__p_
== __y
.__p_
;}
107 friend _LIBCPP_INLINE_VISIBILITY
108 bool operator!=(const cauchy_distribution
& __x
,
109 const cauchy_distribution
& __y
)
110 {return !(__x
== __y
);}
113 template <class _RealType
>
114 template<class _URNG
>
117 cauchy_distribution
<_RealType
>::operator()(_URNG
& __g
, const param_type
& __p
)
119 uniform_real_distribution
<result_type
> __gen
;
120 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
121 return __p
.a() + __p
.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g
));
124 template <class _CharT
, class _Traits
, class _RT
>
125 basic_ostream
<_CharT
, _Traits
>&
126 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
127 const cauchy_distribution
<_RT
>& __x
)
129 __save_flags
<_CharT
, _Traits
> __lx(__os
);
130 typedef basic_ostream
<_CharT
, _Traits
> _OStream
;
131 __os
.flags(_OStream::dec
| _OStream::left
| _OStream::fixed
|
132 _OStream::scientific
);
133 _CharT __sp
= __os
.widen(' ');
135 __os
<< __x
.a() << __sp
<< __x
.b();
139 template <class _CharT
, class _Traits
, class _RT
>
140 basic_istream
<_CharT
, _Traits
>&
141 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
142 cauchy_distribution
<_RT
>& __x
)
144 typedef cauchy_distribution
<_RT
> _Eng
;
145 typedef typename
_Eng::result_type result_type
;
146 typedef typename
_Eng::param_type param_type
;
147 __save_flags
<_CharT
, _Traits
> __lx(__is
);
148 typedef basic_istream
<_CharT
, _Traits
> _Istream
;
149 __is
.flags(_Istream::dec
| _Istream::skipws
);
154 __x
.param(param_type(__a
, __b
));
158 _LIBCPP_END_NAMESPACE_STD
162 #endif // _LIBCPP___RANDOM_CAUCHY_DISTRIBUTION_H