File indexing completed on 2026-05-03 08:13:59
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___RANDOM_CAUCHY_DISTRIBUTION_H
0010 #define _LIBCPP___RANDOM_CAUCHY_DISTRIBUTION_H
0011
0012 #include <__config>
0013 #include <__random/is_valid.h>
0014 #include <__random/uniform_real_distribution.h>
0015 #include <cmath>
0016 #include <iosfwd>
0017 #include <limits>
0018
0019 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0020 # pragma GCC system_header
0021 #endif
0022
0023 _LIBCPP_PUSH_MACROS
0024 #include <__undef_macros>
0025
0026 _LIBCPP_BEGIN_NAMESPACE_STD
0027
0028 template <class _RealType = double>
0029 class _LIBCPP_TEMPLATE_VIS cauchy_distribution {
0030 static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
0031 "RealType must be a supported floating-point type");
0032
0033 public:
0034
0035 typedef _RealType result_type;
0036
0037 class _LIBCPP_TEMPLATE_VIS param_type {
0038 result_type __a_;
0039 result_type __b_;
0040
0041 public:
0042 typedef cauchy_distribution distribution_type;
0043
0044 _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __a = 0, result_type __b = 1) : __a_(__a), __b_(__b) {}
0045
0046 _LIBCPP_HIDE_FROM_ABI result_type a() const { return __a_; }
0047 _LIBCPP_HIDE_FROM_ABI result_type b() const { return __b_; }
0048
0049 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0050 return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_;
0051 }
0052 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0053 };
0054
0055 private:
0056 param_type __p_;
0057
0058 public:
0059
0060 #ifndef _LIBCPP_CXX03_LANG
0061 _LIBCPP_HIDE_FROM_ABI cauchy_distribution() : cauchy_distribution(0) {}
0062 _LIBCPP_HIDE_FROM_ABI explicit cauchy_distribution(result_type __a, result_type __b = 1)
0063 : __p_(param_type(__a, __b)) {}
0064 #else
0065 _LIBCPP_HIDE_FROM_ABI explicit cauchy_distribution(result_type __a = 0, result_type __b = 1)
0066 : __p_(param_type(__a, __b)) {}
0067 #endif
0068 _LIBCPP_HIDE_FROM_ABI explicit cauchy_distribution(const param_type& __p) : __p_(__p) {}
0069 _LIBCPP_HIDE_FROM_ABI void reset() {}
0070
0071
0072 template <class _URNG>
0073 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0074 return (*this)(__g, __p_);
0075 }
0076 template <class _URNG>
0077 _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
0078
0079
0080 _LIBCPP_HIDE_FROM_ABI result_type a() const { return __p_.a(); }
0081 _LIBCPP_HIDE_FROM_ABI result_type b() const { return __p_.b(); }
0082
0083 _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0084 _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0085
0086 _LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); }
0087 _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
0088
0089 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const cauchy_distribution& __x, const cauchy_distribution& __y) {
0090 return __x.__p_ == __y.__p_;
0091 }
0092 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const cauchy_distribution& __x, const cauchy_distribution& __y) {
0093 return !(__x == __y);
0094 }
0095 };
0096
0097 template <class _RealType>
0098 template <class _URNG>
0099 inline _RealType cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
0100 static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0101 uniform_real_distribution<result_type> __gen;
0102
0103 return __p.a() + __p.b() * std::tan(3.1415926535897932384626433832795 * __gen(__g));
0104 }
0105
0106 template <class _CharT, class _Traits, class _RT>
0107 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0108 operator<<(basic_ostream<_CharT, _Traits>& __os, const cauchy_distribution<_RT>& __x) {
0109 __save_flags<_CharT, _Traits> __lx(__os);
0110 typedef basic_ostream<_CharT, _Traits> _OStream;
0111 __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0112 _CharT __sp = __os.widen(' ');
0113 __os.fill(__sp);
0114 __os << __x.a() << __sp << __x.b();
0115 return __os;
0116 }
0117
0118 template <class _CharT, class _Traits, class _RT>
0119 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0120 operator>>(basic_istream<_CharT, _Traits>& __is, cauchy_distribution<_RT>& __x) {
0121 typedef cauchy_distribution<_RT> _Eng;
0122 typedef typename _Eng::result_type result_type;
0123 typedef typename _Eng::param_type param_type;
0124 __save_flags<_CharT, _Traits> __lx(__is);
0125 typedef basic_istream<_CharT, _Traits> _Istream;
0126 __is.flags(_Istream::dec | _Istream::skipws);
0127 result_type __a;
0128 result_type __b;
0129 __is >> __a >> __b;
0130 if (!__is.fail())
0131 __x.param(param_type(__a, __b));
0132 return __is;
0133 }
0134
0135 _LIBCPP_END_NAMESPACE_STD
0136
0137 _LIBCPP_POP_MACROS
0138
0139 #endif