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