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>
85     _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
86 
87     // property functions
88     _LIBCPP_INLINE_VISIBILITY
89     result_type n() const {return __p_.n();}
90 
91     _LIBCPP_INLINE_VISIBILITY
92     param_type param() const {return __p_;}
93     _LIBCPP_INLINE_VISIBILITY
94     void param(const param_type& __p) {__p_ = __p;}
95 
96     _LIBCPP_INLINE_VISIBILITY
97     result_type min() const {return -numeric_limits<result_type>::infinity();}
98     _LIBCPP_INLINE_VISIBILITY
99     result_type max() const {return numeric_limits<result_type>::infinity();}
100 
101     friend _LIBCPP_INLINE_VISIBILITY
102         bool operator==(const student_t_distribution& __x,
103                         const student_t_distribution& __y)
104         {return __x.__p_ == __y.__p_;}
105     friend _LIBCPP_INLINE_VISIBILITY
106         bool operator!=(const student_t_distribution& __x,
107                         const student_t_distribution& __y)
108         {return !(__x == __y);}
109 };
110 
111 template <class _RealType>
112 template<class _URNG>
113 _RealType
114 student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p)
115 {
116     static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
117     gamma_distribution<result_type> __gd(__p.n() * .5, 2);
118     return __nd_(__g) * _VSTD::sqrt(__p.n()/__gd(__g));
119 }
120 
121 template <class _CharT, class _Traits, class _RT>
122 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
123 operator<<(basic_ostream<_CharT, _Traits>& __os,
124            const student_t_distribution<_RT>& __x)
125 {
126     __save_flags<_CharT, _Traits> __lx(__os);
127     typedef basic_ostream<_CharT, _Traits> _OStream;
128     __os.flags(_OStream::dec | _OStream::left | _OStream::fixed |
129                _OStream::scientific);
130     __os << __x.n();
131     return __os;
132 }
133 
134 template <class _CharT, class _Traits, class _RT>
135 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
136 operator>>(basic_istream<_CharT, _Traits>& __is,
137            student_t_distribution<_RT>& __x)
138 {
139     typedef student_t_distribution<_RT> _Eng;
140     typedef typename _Eng::result_type result_type;
141     typedef typename _Eng::param_type param_type;
142     __save_flags<_CharT, _Traits> __lx(__is);
143     typedef basic_istream<_CharT, _Traits> _Istream;
144     __is.flags(_Istream::dec | _Istream::skipws);
145     result_type __n;
146     __is >> __n;
147     if (!__is.fail())
148         __x.param(param_type(__n));
149     return __is;
150 }
151 
152 _LIBCPP_END_NAMESPACE_STD
153 
154 _LIBCPP_POP_MACROS
155 
156 #endif // _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H
157