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