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 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H
11 
12 #include <__config>
13 #include <__random/gamma_distribution.h>
14 #include <__random/is_valid.h>
15 #include <__random/normal_distribution.h>
16 #include <cmath>
17 #include <iosfwd>
18 #include <limits>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_PUSH_MACROS
25 #include <__undef_macros>
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 template<class _RealType = double>
30 class _LIBCPP_TEMPLATE_VIS student_t_distribution
31 {
32 public:
33     // types
34     typedef _RealType result_type;
35 
36     class _LIBCPP_TEMPLATE_VIS param_type
37     {
38         result_type __n_;
39     public:
40         typedef student_t_distribution distribution_type;
41 
42         _LIBCPP_INLINE_VISIBILITY
43         explicit param_type(result_type __n = 1) : __n_(__n) {}
44 
45         _LIBCPP_INLINE_VISIBILITY
46         result_type n() const {return __n_;}
47 
48         friend _LIBCPP_INLINE_VISIBILITY
49             bool operator==(const param_type& __x, const param_type& __y)
50             {return __x.__n_ == __y.__n_;}
51         friend _LIBCPP_INLINE_VISIBILITY
52             bool operator!=(const param_type& __x, const param_type& __y)
53             {return !(__x == __y);}
54     };
55 
56 private:
57     param_type __p_;
58     normal_distribution<result_type> __nd_;
59 
60 public:
61     // constructor and reset functions
62 #ifndef _LIBCPP_CXX03_LANG
63     _LIBCPP_INLINE_VISIBILITY
64     student_t_distribution() : student_t_distribution(1) {}
65     _LIBCPP_INLINE_VISIBILITY
66     explicit student_t_distribution(result_type __n)
67         : __p_(param_type(__n)) {}
68 #else
69     _LIBCPP_INLINE_VISIBILITY
70     explicit student_t_distribution(result_type __n = 1)
71         : __p_(param_type(__n)) {}
72 #endif
73     _LIBCPP_INLINE_VISIBILITY
74     explicit student_t_distribution(const param_type& __p)
75         : __p_(__p) {}
76     _LIBCPP_INLINE_VISIBILITY
77     void reset() {__nd_.reset();}
78 
79     // generating functions
80     template<class _URNG>
81         _LIBCPP_INLINE_VISIBILITY
82         result_type operator()(_URNG& __g)
83         {return (*this)(__g, __p_);}
84     template<class _URNG> result_type operator()(_URNG& __g, const param_type& __p);
85 
86     // property functions
87     _LIBCPP_INLINE_VISIBILITY
88     result_type n() const {return __p_.n();}
89 
90     _LIBCPP_INLINE_VISIBILITY
91     param_type param() const {return __p_;}
92     _LIBCPP_INLINE_VISIBILITY
93     void param(const param_type& __p) {__p_ = __p;}
94 
95     _LIBCPP_INLINE_VISIBILITY
96     result_type min() const {return -numeric_limits<result_type>::infinity();}
97     _LIBCPP_INLINE_VISIBILITY
98     result_type max() const {return numeric_limits<result_type>::infinity();}
99 
100     friend _LIBCPP_INLINE_VISIBILITY
101         bool operator==(const student_t_distribution& __x,
102                         const student_t_distribution& __y)
103         {return __x.__p_ == __y.__p_;}
104     friend _LIBCPP_INLINE_VISIBILITY
105         bool operator!=(const student_t_distribution& __x,
106                         const student_t_distribution& __y)
107         {return !(__x == __y);}
108 };
109 
110 template <class _RealType>
111 template<class _URNG>
112 _RealType
113 student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
114 {
115     static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
116     gamma_distribution<result_type> __gd(__p.n() * .5, 2);
117     return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
118 }
119 
120 template <class _CharT, class _Traits, class _RT>
121 basic_ostream<_CharT, _Traits>&
122 operator<<(basic_ostream<_CharT, _Traits>& __os,
123            const student_t_distribution<_RT>& __x)
124 {
125     __save_flags<_CharT, _Traits> __lx(__os);
126     typedef basic_ostream<_CharT, _Traits> _OStream;
127     __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
128                _OStream::scientific);
129     __os << __x.n();
130     return __os;
131 }
132 
133 template <class _CharT, class _Traits, class _RT>
134 basic_istream<_CharT, _Traits>&
135 operator>>(basic_istream<_CharT, _Traits>& __is,
136            student_t_distribution<_RT>& __x)
137 {
138     typedef student_t_distribution<_RT> _Eng;
139     typedef typename _Eng::result_type result_type;
140     typedef typename _Eng::param_type param_type;
141     __save_flags<_CharT, _Traits> __lx(__is);
142     typedef basic_istream<_CharT, _Traits> _Istream;
143     __is.flags(_Istream::dec | _Istream::skipws);
144     result_type __n;
145     __is >> __n;
146     if (!__is.fail())
147         __x.param(param_type(__n));
148     return __is;
149 }
150 
151 _LIBCPP_END_NAMESPACE_STD
152 
153 _LIBCPP_POP_MACROS
154 
155 #endif // _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H
156