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___CXX03___RANDOM_CAUCHY_DISTRIBUTION_H
10 #define _LIBCPP___CXX03___RANDOM_CAUCHY_DISTRIBUTION_H
11
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>
18
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
21 #endif
22
23 _LIBCPP_PUSH_MACROS
24 #include <__cxx03/__undef_macros>
25
26 _LIBCPP_BEGIN_NAMESPACE_STD
27
28 template <class _RealType = double>
29 class _LIBCPP_TEMPLATE_VIS cauchy_distribution {
30 static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
31 "RealType must be a supported floating-point type");
32
33 public:
34 // types
35 typedef _RealType result_type;
36
37 class _LIBCPP_TEMPLATE_VIS param_type {
38 result_type __a_;
39 result_type __b_;
40
41 public:
42 typedef cauchy_distribution distribution_type;
43
__a_(__a)44 _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __a = 0, result_type __b = 1) : __a_(__a), __b_(__b) {}
45
a()46 _LIBCPP_HIDE_FROM_ABI result_type a() const { return __a_; }
b()47 _LIBCPP_HIDE_FROM_ABI result_type b() const { return __b_; }
48
49 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
50 return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;
51 }
52 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
53 };
54
55 private:
56 param_type __p_;
57
58 public:
59 // constructor and reset functions
60 _LIBCPP_HIDE_FROM_ABI explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
__p_(param_type (__a,__b))61 : __p_(param_type(__a, __b)) {}
cauchy_distribution(const param_type & __p)62 _LIBCPP_HIDE_FROM_ABI explicit cauchy_distribution(const param_type& __p) : __p_(__p) {}
reset()63 _LIBCPP_HIDE_FROM_ABI void reset() {}
64
65 // generating functions
66 template <class _URNG>
operator()67 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
68 return (*this)(__g, __p_);
69 }
70 template <class _URNG>
71 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
72
73 // property functions
a()74 _LIBCPP_HIDE_FROM_ABI result_type a() const { return __p_.a(); }
b()75 _LIBCPP_HIDE_FROM_ABI result_type b() const { return __p_.b(); }
76
param()77 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)78 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
79
min()80 _LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); }
max()81 _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
82
83 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const cauchy_distribution& __x, const cauchy_distribution& __y) {
84 return __x.__p_ == __y.__p_;
85 }
86 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const cauchy_distribution& __x, const cauchy_distribution& __y) {
87 return !(__x == __y);
88 }
89 };
90
91 template <class _RealType>
92 template <class _URNG>
operator()93 inline _RealType cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
94 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
95 uniform_real_distribution<result_type> __gen;
96 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
97 return __p.a() + __p.b() * std::tan(3.1415926535897932384626433832795 * __gen(__g));
98 }
99
100 template <class _CharT, class _Traits, class _RT>
101 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
102 operator<<(basic_ostream<_CharT, _Traits>& __os, const cauchy_distribution<_RT>& __x) {
103 __save_flags<_CharT, _Traits> __lx(__os);
104 typedef basic_ostream<_CharT, _Traits> _OStream;
105 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
106 _CharT __sp = __os.widen(' ');
107 __os.fill(__sp);
108 __os << __x.a() << __sp << __x.b();
109 return __os;
110 }
111
112 template <class _CharT, class _Traits, class _RT>
113 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
114 operator>>(basic_istream<_CharT, _Traits>& __is, cauchy_distribution<_RT>& __x) {
115 typedef cauchy_distribution<_RT> _Eng;
116 typedef typename _Eng::result_type result_type;
117 typedef typename _Eng::param_type param_type;
118 __save_flags<_CharT, _Traits> __lx(__is);
119 typedef basic_istream<_CharT, _Traits> _Istream;
120 __is.flags(_Istream::dec | _Istream::skipws);
121 result_type __a;
122 result_type __b;
123 __is >> __a >> __b;
124 if (!__is.fail())
125 __x.param(param_type(__a, __b));
126 return __is;
127 }
128
129 _LIBCPP_END_NAMESPACE_STD
130
131 _LIBCPP_POP_MACROS
132
133 #endif // _LIBCPP___CXX03___RANDOM_CAUCHY_DISTRIBUTION_H
134