xref: /llvm-project/libcxx/include/__random/student_t_distribution.h (revision 9783f28cbb155e4a8d49c12e1c60ce14dcfaf0c7)
1344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
2344cef66SArthur O'Dwyer //
3344cef66SArthur O'Dwyer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344cef66SArthur O'Dwyer // See https://llvm.org/LICENSE.txt for license information.
5344cef66SArthur O'Dwyer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344cef66SArthur O'Dwyer //
7344cef66SArthur O'Dwyer //===----------------------------------------------------------------------===//
8344cef66SArthur O'Dwyer 
9344cef66SArthur O'Dwyer #ifndef _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H
10344cef66SArthur O'Dwyer #define _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H
11344cef66SArthur O'Dwyer 
12344cef66SArthur O'Dwyer #include <__config>
13344cef66SArthur O'Dwyer #include <__random/gamma_distribution.h>
147624552eSArthur O'Dwyer #include <__random/is_valid.h>
15344cef66SArthur O'Dwyer #include <__random/normal_distribution.h>
16344cef66SArthur O'Dwyer #include <cmath>
17344cef66SArthur O'Dwyer #include <iosfwd>
18344cef66SArthur O'Dwyer #include <limits>
19344cef66SArthur O'Dwyer 
20344cef66SArthur O'Dwyer #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21344cef66SArthur O'Dwyer #  pragma GCC system_header
22344cef66SArthur O'Dwyer #endif
23344cef66SArthur O'Dwyer 
24344cef66SArthur O'Dwyer _LIBCPP_PUSH_MACROS
25344cef66SArthur O'Dwyer #include <__undef_macros>
26344cef66SArthur O'Dwyer 
27344cef66SArthur O'Dwyer _LIBCPP_BEGIN_NAMESPACE_STD
28344cef66SArthur O'Dwyer 
29344cef66SArthur O'Dwyer template <class _RealType = double>
30*9783f28cSLouis Dionne class _LIBCPP_TEMPLATE_VIS student_t_distribution {
31727fef79SNhat Nguyen   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
32727fef79SNhat Nguyen                 "RealType must be a supported floating-point type");
33727fef79SNhat Nguyen 
34344cef66SArthur O'Dwyer public:
35344cef66SArthur O'Dwyer   // types
36344cef66SArthur O'Dwyer   typedef _RealType result_type;
37344cef66SArthur O'Dwyer 
38*9783f28cSLouis Dionne   class _LIBCPP_TEMPLATE_VIS param_type {
39344cef66SArthur O'Dwyer     result_type __n_;
40*9783f28cSLouis Dionne 
41344cef66SArthur O'Dwyer   public:
42344cef66SArthur O'Dwyer     typedef student_t_distribution distribution_type;
43344cef66SArthur O'Dwyer 
__n_(__n)44*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __n = 1) : __n_(__n) {}
45344cef66SArthur O'Dwyer 
n()46*9783f28cSLouis Dionne     _LIBCPP_HIDE_FROM_ABI result_type n() const { return __n_; }
47344cef66SArthur O'Dwyer 
48*9783f28cSLouis Dionne     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
49*9783f28cSLouis Dionne       return __x.__n_ == __y.__n_;
50*9783f28cSLouis Dionne     }
51*9783f28cSLouis Dionne     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
52344cef66SArthur O'Dwyer   };
53344cef66SArthur O'Dwyer 
54344cef66SArthur O'Dwyer private:
55344cef66SArthur O'Dwyer   param_type __p_;
56344cef66SArthur O'Dwyer   normal_distribution<result_type> __nd_;
57344cef66SArthur O'Dwyer 
58344cef66SArthur O'Dwyer public:
59344cef66SArthur O'Dwyer   // constructor and reset functions
60344cef66SArthur O'Dwyer #ifndef _LIBCPP_CXX03_LANG
student_t_distribution()61*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI student_t_distribution() : student_t_distribution(1) {}
student_t_distribution(result_type __n)62*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit student_t_distribution(result_type __n) : __p_(param_type(__n)) {}
63344cef66SArthur O'Dwyer #else
64*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit student_t_distribution(result_type __n = 1) : __p_(param_type(__n)) {}
65344cef66SArthur O'Dwyer #endif
student_t_distribution(const param_type & __p)66*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI explicit student_t_distribution(const param_type& __p) : __p_(__p) {}
reset()67*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI void reset() { __nd_.reset(); }
68344cef66SArthur O'Dwyer 
69344cef66SArthur O'Dwyer   // generating functions
70344cef66SArthur O'Dwyer   template <class _URNG>
operator()71*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
72*9783f28cSLouis Dionne     return (*this)(__g, __p_);
73*9783f28cSLouis Dionne   }
7483ce1397SNikolas Klauser   template <class _URNG>
7583ce1397SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
76344cef66SArthur O'Dwyer 
77344cef66SArthur O'Dwyer   // property functions
n()78*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type n() const { return __p_.n(); }
79344cef66SArthur O'Dwyer 
param()80*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)81*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
82344cef66SArthur O'Dwyer 
min()83*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type min() const { return -numeric_limits<result_type>::infinity(); }
max()84*9783f28cSLouis Dionne   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
85344cef66SArthur O'Dwyer 
86*9783f28cSLouis Dionne   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const student_t_distribution& __x, const student_t_distribution& __y) {
87*9783f28cSLouis Dionne     return __x.__p_ == __y.__p_;
88*9783f28cSLouis Dionne   }
89*9783f28cSLouis Dionne   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const student_t_distribution& __x, const student_t_distribution& __y) {
90*9783f28cSLouis Dionne     return !(__x == __y);
91*9783f28cSLouis Dionne   }
92344cef66SArthur O'Dwyer };
93344cef66SArthur O'Dwyer 
94344cef66SArthur O'Dwyer template <class _RealType>
95344cef66SArthur O'Dwyer template <class _URNG>
operator()96*9783f28cSLouis Dionne _RealType student_t_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
977624552eSArthur O'Dwyer   static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
98344cef66SArthur O'Dwyer   gamma_distribution<result_type> __gd(__p.n() * .5, 2);
9977a00c0dSLouis Dionne   return __nd_(__g) * std::sqrt(__p.n() / __gd(__g));
100344cef66SArthur O'Dwyer }
101344cef66SArthur O'Dwyer 
102344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
10380c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
104*9783f28cSLouis Dionne operator<<(basic_ostream<_CharT, _Traits>& __os, const student_t_distribution<_RT>& __x) {
105344cef66SArthur O'Dwyer   __save_flags<_CharT, _Traits> __lx(__os);
106344cef66SArthur O'Dwyer   typedef basic_ostream<_CharT, _Traits> _OStream;
107*9783f28cSLouis Dionne   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
108344cef66SArthur O'Dwyer   __os << __x.n();
109344cef66SArthur O'Dwyer   return __os;
110344cef66SArthur O'Dwyer }
111344cef66SArthur O'Dwyer 
112344cef66SArthur O'Dwyer template <class _CharT, class _Traits, class _RT>
11380c7e93aSNikolas Klauser _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
114*9783f28cSLouis Dionne operator>>(basic_istream<_CharT, _Traits>& __is, student_t_distribution<_RT>& __x) {
115344cef66SArthur O'Dwyer   typedef student_t_distribution<_RT> _Eng;
116344cef66SArthur O'Dwyer   typedef typename _Eng::result_type result_type;
117344cef66SArthur O'Dwyer   typedef typename _Eng::param_type param_type;
118344cef66SArthur O'Dwyer   __save_flags<_CharT, _Traits> __lx(__is);
119344cef66SArthur O'Dwyer   typedef basic_istream<_CharT, _Traits> _Istream;
120344cef66SArthur O'Dwyer   __is.flags(_Istream::dec | _Istream::skipws);
121344cef66SArthur O'Dwyer   result_type __n;
122344cef66SArthur O'Dwyer   __is >> __n;
123344cef66SArthur O'Dwyer   if (!__is.fail())
124344cef66SArthur O'Dwyer     __x.param(param_type(__n));
125344cef66SArthur O'Dwyer   return __is;
126344cef66SArthur O'Dwyer }
127344cef66SArthur O'Dwyer 
128344cef66SArthur O'Dwyer _LIBCPP_END_NAMESPACE_STD
129344cef66SArthur O'Dwyer 
130344cef66SArthur O'Dwyer _LIBCPP_POP_MACROS
131344cef66SArthur O'Dwyer 
132344cef66SArthur O'Dwyer #endif // _LIBCPP___RANDOM_STUDENT_T_DISTRIBUTION_H
133