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