1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H
13 #include <__random/gamma_distribution.h>
14 #include <__random/is_valid.h>
15 #include <__random/normal_distribution.h>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
25 #include <__undef_macros>
27 _LIBCPP_BEGIN_NAMESPACE_STD
29 template <class _RealType
= double>
30 class _LIBCPP_TEMPLATE_VIS student_t_distribution
{
31 static_assert(__libcpp_random_is_valid_realtype
<_RealType
>::value
,
32 "RealType must be a supported floating-point type");
36 typedef _RealType result_type
;
38 class _LIBCPP_TEMPLATE_VIS param_type
{
42 typedef student_t_distribution distribution_type
;
44 _LIBCPP_HIDE_FROM_ABI
explicit param_type(result_type __n
= 1) : __n_(__n
) {}
46 _LIBCPP_HIDE_FROM_ABI result_type
n() const { return __n_
; }
48 friend _LIBCPP_HIDE_FROM_ABI
bool operator==(const param_type
& __x
, const param_type
& __y
) {
49 return __x
.__n_
== __y
.__n_
;
51 friend _LIBCPP_HIDE_FROM_ABI
bool operator!=(const param_type
& __x
, const param_type
& __y
) { return !(__x
== __y
); }
56 normal_distribution
<result_type
> __nd_
;
59 // constructor and reset functions
60 #ifndef _LIBCPP_CXX03_LANG
61 _LIBCPP_HIDE_FROM_ABI
student_t_distribution() : student_t_distribution(1) {}
62 _LIBCPP_HIDE_FROM_ABI
explicit student_t_distribution(result_type __n
) : __p_(param_type(__n
)) {}
64 _LIBCPP_HIDE_FROM_ABI
explicit student_t_distribution(result_type __n
= 1) : __p_(param_type(__n
)) {}
66 _LIBCPP_HIDE_FROM_ABI
explicit student_t_distribution(const param_type
& __p
) : __p_(__p
) {}
67 _LIBCPP_HIDE_FROM_ABI
void reset() { __nd_
.reset(); }
69 // generating functions
70 template <class _URNG
>
71 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
) {
72 return (*this)(__g
, __p_
);
74 template <class _URNG
>
75 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
, const param_type
& __p
);
78 _LIBCPP_HIDE_FROM_ABI result_type
n() const { return __p_
.n(); }
80 _LIBCPP_HIDE_FROM_ABI param_type
param() const { return __p_
; }
81 _LIBCPP_HIDE_FROM_ABI
void param(const param_type
& __p
) { __p_
= __p
; }
83 _LIBCPP_HIDE_FROM_ABI result_type
min() const { return -numeric_limits
<result_type
>::infinity(); }
84 _LIBCPP_HIDE_FROM_ABI result_type
max() const { return numeric_limits
<result_type
>::infinity(); }
86 friend _LIBCPP_HIDE_FROM_ABI
bool operator==(const student_t_distribution
& __x
, const student_t_distribution
& __y
) {
87 return __x
.__p_
== __y
.__p_
;
89 friend _LIBCPP_HIDE_FROM_ABI
bool operator!=(const student_t_distribution
& __x
, const student_t_distribution
& __y
) {
94 template <class _RealType
>
95 template <class _URNG
>
96 _RealType student_t_distribution
<_RealType
>::operator()(_URNG
& __g
, const param_type
& __p
) {
97 static_assert(__libcpp_random_is_valid_urng
<_URNG
>::value
, "");
98 gamma_distribution
<result_type
> __gd(__p
.n() * .5, 2);
99 return __nd_(__g
) * std::sqrt(__p
.n() / __gd(__g
));
102 template <class _CharT
, class _Traits
, class _RT
>
103 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
104 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const student_t_distribution
<_RT
>& __x
) {
105 __save_flags
<_CharT
, _Traits
> __lx(__os
);
106 typedef basic_ostream
<_CharT
, _Traits
> _OStream
;
107 __os
.flags(_OStream::dec
| _OStream::left
| _OStream::fixed
| _OStream::scientific
);
112 template <class _CharT
, class _Traits
, class _RT
>
113 _LIBCPP_HIDE_FROM_ABI basic_istream
<_CharT
, _Traits
>&
114 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, student_t_distribution
<_RT
>& __x
) {
115 typedef student_t_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
);
124 __x
.param(param_type(__n
));
128 _LIBCPP_END_NAMESPACE_STD
132 #endif // _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H