14824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 24824e7fdSDimitry Andric // 34824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 44824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 54824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 64824e7fdSDimitry Andric // 74824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 84824e7fdSDimitry Andric 94824e7fdSDimitry Andric #ifndef _LIBCPP___RANDOM_CAUCHY_DISTRIBUTION_H 104824e7fdSDimitry Andric #define _LIBCPP___RANDOM_CAUCHY_DISTRIBUTION_H 114824e7fdSDimitry Andric 124824e7fdSDimitry Andric #include <__config> 1381ad6265SDimitry Andric #include <__random/is_valid.h> 144824e7fdSDimitry Andric #include <__random/uniform_real_distribution.h> 154824e7fdSDimitry Andric #include <cmath> 164824e7fdSDimitry Andric #include <iosfwd> 174824e7fdSDimitry Andric #include <limits> 184824e7fdSDimitry Andric 194824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 204824e7fdSDimitry Andric # pragma GCC system_header 214824e7fdSDimitry Andric #endif 224824e7fdSDimitry Andric 234824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS 244824e7fdSDimitry Andric #include <__undef_macros> 254824e7fdSDimitry Andric 264824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 274824e7fdSDimitry Andric 284824e7fdSDimitry Andric template <class _RealType = double> 29*cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS cauchy_distribution { 305f757f3fSDimitry Andric static_assert(__libcpp_random_is_valid_realtype<_RealType>::value, 315f757f3fSDimitry Andric "RealType must be a supported floating-point type"); 325f757f3fSDimitry Andric 334824e7fdSDimitry Andric public: 344824e7fdSDimitry Andric // types 354824e7fdSDimitry Andric typedef _RealType result_type; 364824e7fdSDimitry Andric 37*cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS param_type { 384824e7fdSDimitry Andric result_type __a_; 394824e7fdSDimitry Andric result_type __b_; 40*cb14a3feSDimitry Andric 414824e7fdSDimitry Andric public: 424824e7fdSDimitry Andric typedef cauchy_distribution distribution_type; 434824e7fdSDimitry Andric 44*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __a = 0, result_type __b = 1) : __a_(__a), __b_(__b) {} 454824e7fdSDimitry Andric 46*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type a() const { return __a_; } 47*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type b() const { return __b_; } 484824e7fdSDimitry Andric 49*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) { 50*cb14a3feSDimitry Andric return __x.__a_ == __y.__a_ && __x.__b_ == __y.__b_; 51*cb14a3feSDimitry Andric } 52*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); } 534824e7fdSDimitry Andric }; 544824e7fdSDimitry Andric 554824e7fdSDimitry Andric private: 564824e7fdSDimitry Andric param_type __p_; 574824e7fdSDimitry Andric 584824e7fdSDimitry Andric public: 594824e7fdSDimitry Andric // constructor and reset functions 604824e7fdSDimitry Andric #ifndef _LIBCPP_CXX03_LANG 61*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI cauchy_distribution() : cauchy_distribution(0) {} 62*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit cauchy_distribution(result_type __a, result_type __b = 1) 634824e7fdSDimitry Andric : __p_(param_type(__a, __b)) {} 644824e7fdSDimitry Andric #else 65*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit cauchy_distribution(result_type __a = 0, result_type __b = 1) 664824e7fdSDimitry Andric : __p_(param_type(__a, __b)) {} 674824e7fdSDimitry Andric #endif 68*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit cauchy_distribution(const param_type& __p) : __p_(__p) {} 69*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void reset() {} 704824e7fdSDimitry Andric 714824e7fdSDimitry Andric // generating functions 724824e7fdSDimitry Andric template <class _URNG> 73*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) { 74*cb14a3feSDimitry Andric return (*this)(__g, __p_); 75*cb14a3feSDimitry Andric } 76*cb14a3feSDimitry Andric template <class _URNG> 77*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p); 784824e7fdSDimitry Andric 794824e7fdSDimitry Andric // property functions 80*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type a() const { return __p_.a(); } 81*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type b() const { return __p_.b(); } 824824e7fdSDimitry Andric 83*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; } 84*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; } 854824e7fdSDimitry Andric 86*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); } 87*cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); } 884824e7fdSDimitry Andric 89*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator==(const cauchy_distribution& __x, const cauchy_distribution& __y) { 90*cb14a3feSDimitry Andric return __x.__p_ == __y.__p_; 91*cb14a3feSDimitry Andric } 92*cb14a3feSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const cauchy_distribution& __x, const cauchy_distribution& __y) { 93*cb14a3feSDimitry Andric return !(__x == __y); 94*cb14a3feSDimitry Andric } 954824e7fdSDimitry Andric }; 964824e7fdSDimitry Andric 974824e7fdSDimitry Andric template <class _RealType> 984824e7fdSDimitry Andric template <class _URNG> 99*cb14a3feSDimitry Andric inline _RealType cauchy_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) { 10081ad6265SDimitry Andric static_assert(__libcpp_random_is_valid_urng<_URNG>::value, ""); 1014824e7fdSDimitry Andric uniform_real_distribution<result_type> __gen; 1024824e7fdSDimitry Andric // purposefully let tan arg get as close to pi/2 as it wants, tan will return a finite 1035f757f3fSDimitry Andric return __p.a() + __p.b() * std::tan(3.1415926535897932384626433832795 * __gen(__g)); 1044824e7fdSDimitry Andric } 1054824e7fdSDimitry Andric 1064824e7fdSDimitry Andric template <class _CharT, class _Traits, class _RT> 107bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 108*cb14a3feSDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os, const cauchy_distribution<_RT>& __x) { 1094824e7fdSDimitry Andric __save_flags<_CharT, _Traits> __lx(__os); 1104824e7fdSDimitry Andric typedef basic_ostream<_CharT, _Traits> _OStream; 111*cb14a3feSDimitry Andric __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific); 1124824e7fdSDimitry Andric _CharT __sp = __os.widen(' '); 1134824e7fdSDimitry Andric __os.fill(__sp); 1144824e7fdSDimitry Andric __os << __x.a() << __sp << __x.b(); 1154824e7fdSDimitry Andric return __os; 1164824e7fdSDimitry Andric } 1174824e7fdSDimitry Andric 1184824e7fdSDimitry Andric template <class _CharT, class _Traits, class _RT> 119bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& 120*cb14a3feSDimitry Andric operator>>(basic_istream<_CharT, _Traits>& __is, cauchy_distribution<_RT>& __x) { 1214824e7fdSDimitry Andric typedef cauchy_distribution<_RT> _Eng; 1224824e7fdSDimitry Andric typedef typename _Eng::result_type result_type; 1234824e7fdSDimitry Andric typedef typename _Eng::param_type param_type; 1244824e7fdSDimitry Andric __save_flags<_CharT, _Traits> __lx(__is); 1254824e7fdSDimitry Andric typedef basic_istream<_CharT, _Traits> _Istream; 1264824e7fdSDimitry Andric __is.flags(_Istream::dec | _Istream::skipws); 1274824e7fdSDimitry Andric result_type __a; 1284824e7fdSDimitry Andric result_type __b; 1294824e7fdSDimitry Andric __is >> __a >> __b; 1304824e7fdSDimitry Andric if (!__is.fail()) 1314824e7fdSDimitry Andric __x.param(param_type(__a, __b)); 1324824e7fdSDimitry Andric return __is; 1334824e7fdSDimitry Andric } 1344824e7fdSDimitry Andric 1354824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD 1364824e7fdSDimitry Andric 1374824e7fdSDimitry Andric _LIBCPP_POP_MACROS 1384824e7fdSDimitry Andric 1394824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_CAUCHY_DISTRIBUTION_H 140