Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __random / cauchy_distribution.h
blob2fda6b53837673de47cbdc4038d2a32948db689d
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 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANDOM_CAUCHY_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_CAUCHY_DISTRIBUTION_H
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>
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
21 #endif
23 _LIBCPP_PUSH_MACROS
24 #include <__undef_macros>
26 _LIBCPP_BEGIN_NAMESPACE_STD
28 template<class _RealType = double>
29 class _LIBCPP_TEMPLATE_VIS cauchy_distribution
31 public:
32 // types
33 typedef _RealType result_type;
35 class _LIBCPP_TEMPLATE_VIS param_type
37 result_type __a_;
38 result_type __b_;
39 public:
40 typedef cauchy_distribution distribution_type;
42 _LIBCPP_INLINE_VISIBILITY
43 explicit param_type(result_type __a = 0, result_type __b = 1)
44 : __a_(__a), __b_(__b) {}
46 _LIBCPP_INLINE_VISIBILITY
47 result_type a() const {return __a_;}
48 _LIBCPP_INLINE_VISIBILITY
49 result_type b() const {return __b_;}
51 friend _LIBCPP_INLINE_VISIBILITY
52 bool operator==(const param_type& __x, const param_type& __y)
53 {return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;}
54 friend _LIBCPP_INLINE_VISIBILITY
55 bool operator!=(const param_type& __x, const param_type& __y)
56 {return !(__x == __y);}
59 private:
60 param_type __p_;
62 public:
63 // constructor and reset functions
64 #ifndef _LIBCPP_CXX03_LANG
65 _LIBCPP_INLINE_VISIBILITY
66 cauchy_distribution() : cauchy_distribution(0) {}
67 _LIBCPP_INLINE_VISIBILITY
68 explicit cauchy_distribution(result_type __a, result_type __b = 1)
69 : __p_(param_type(__a, __b)) {}
70 #else
71 _LIBCPP_INLINE_VISIBILITY
72 explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
73 : __p_(param_type(__a, __b)) {}
74 #endif
75 _LIBCPP_INLINE_VISIBILITY
76 explicit cauchy_distribution(const param_type& __p)
77 : __p_(__p) {}
78 _LIBCPP_INLINE_VISIBILITY
79 void reset() {}
81 // generating functions
82 template<class _URNG>
83 _LIBCPP_INLINE_VISIBILITY
84 result_type operator()(_URNG& __g)
85 {return (*this)(__g, __p_);}
86 template<class _URNG> _LIBCPP_INLINE_VISIBILITY result_type operator()(_URNG& __g, const param_type& __p);
88 // property functions
89 _LIBCPP_INLINE_VISIBILITY
90 result_type a() const {return __p_.a();}
91 _LIBCPP_INLINE_VISIBILITY
92 result_type b() const {return __p_.b();}
94 _LIBCPP_INLINE_VISIBILITY
95 param_type param() const {return __p_;}
96 _LIBCPP_INLINE_VISIBILITY
97 void param(const param_type& __p) {__p_ = __p;}
99 _LIBCPP_INLINE_VISIBILITY
100 result_type min() const {return -numeric_limits<result_type>::infinity();}
101 _LIBCPP_INLINE_VISIBILITY
102 result_type max() const {return numeric_limits<result_type>::infinity();}
104 friend _LIBCPP_INLINE_VISIBILITY
105 bool operator==(const cauchy_distribution& __x,
106 const cauchy_distribution& __y)
107 {return __x.__p_ == __y.__p_;}
108 friend _LIBCPP_INLINE_VISIBILITY
109 bool operator!=(const cauchy_distribution& __x,
110 const cauchy_distribution& __y)
111 {return !(__x == __y);}
114 template <class _RealType>
115 template<class _URNG>
116 inline
117 _RealType
118 cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
120 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
121 uniform_real_distribution<result_type> __gen;
122 // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite
123 return __p.a() + __p.b() * _VSTD::tan(3.1415926535897932384626433832795 * __gen(__g));
126 template <class _CharT, class _Traits, class _RT>
127 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
128 operator<<(basic_ostream<_CharT, _Traits>& __os,
129 const cauchy_distribution<_RT>& __x)
131 __save_flags<_CharT, _Traits> __lx(__os);
132 typedef basic_ostream<_CharT, _Traits> _OStream;
133 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
134 _OStream::scientific);
135 _CharT __sp = __os.widen(' ');
136 __os.fill(__sp);
137 __os << __x.a() << __sp << __x.b();
138 return __os;
141 template <class _CharT, class _Traits, class _RT>
142 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
143 operator>>(basic_istream<_CharT, _Traits>& __is,
144 cauchy_distribution<_RT>& __x)
146 typedef cauchy_distribution<_RT> _Eng;
147 typedef typename _Eng::result_type result_type;
148 typedef typename _Eng::param_type param_type;
149 __save_flags<_CharT, _Traits> __lx(__is);
150 typedef basic_istream<_CharT, _Traits> _Istream;
151 __is.flags(_Istream::dec | _Istream::skipws);
152 result_type __a;
153 result_type __b;
154 __is >> __a >> __b;
155 if (!__is.fail())
156 __x.param(param_type(__a, __b));
157 return __is;
160 _LIBCPP_END_NAMESPACE_STD
162 _LIBCPP_POP_MACROS
164 #endif // _LIBCPP___RANDOM_CAUCHY_DISTRIBUTION_H