1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANDOM_FISHER_F_DISTRIBUTION_H
10 #define _LIBCPP___RANDOM_FISHER_F_DISTRIBUTION_H
13 #include <__random/gamma_distribution.h>
14 #include <__random/is_valid.h>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
23 #include <__undef_macros>
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 template <class _RealType
= double>
28 class _LIBCPP_TEMPLATE_VIS fisher_f_distribution
{
29 static_assert(__libcpp_random_is_valid_realtype
<_RealType
>::value
,
30 "RealType must be a supported floating-point type");
34 typedef _RealType result_type
;
36 class _LIBCPP_TEMPLATE_VIS param_type
{
41 typedef fisher_f_distribution distribution_type
;
43 _LIBCPP_HIDE_FROM_ABI
explicit param_type(result_type __m
= 1, result_type __n
= 1) : __m_(__m
), __n_(__n
) {}
45 _LIBCPP_HIDE_FROM_ABI result_type
m() const { return __m_
; }
46 _LIBCPP_HIDE_FROM_ABI result_type
n() const { return __n_
; }
48 friend _LIBCPP_HIDE_FROM_ABI
bool operator==(const param_type
& __x
, const param_type
& __y
) {
49 return __x
.__m_
== __y
.__m_
&& __x
.__n_
== __y
.__n_
;
51 friend _LIBCPP_HIDE_FROM_ABI
bool operator!=(const param_type
& __x
, const param_type
& __y
) { return !(__x
== __y
); }
58 // constructor and reset functions
59 #ifndef _LIBCPP_CXX03_LANG
60 _LIBCPP_HIDE_FROM_ABI
fisher_f_distribution() : fisher_f_distribution(1) {}
61 _LIBCPP_HIDE_FROM_ABI
explicit fisher_f_distribution(result_type __m
, result_type __n
= 1)
62 : __p_(param_type(__m
, __n
)) {}
64 _LIBCPP_HIDE_FROM_ABI
explicit fisher_f_distribution(result_type __m
= 1, result_type __n
= 1)
65 : __p_(param_type(__m
, __n
)) {}
67 _LIBCPP_HIDE_FROM_ABI
explicit fisher_f_distribution(const param_type
& __p
) : __p_(__p
) {}
68 _LIBCPP_HIDE_FROM_ABI
void reset() {}
70 // generating functions
71 template <class _URNG
>
72 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
) {
73 return (*this)(__g
, __p_
);
75 template <class _URNG
>
76 _LIBCPP_HIDE_FROM_ABI result_type
operator()(_URNG
& __g
, const param_type
& __p
);
79 _LIBCPP_HIDE_FROM_ABI result_type
m() const { return __p_
.m(); }
80 _LIBCPP_HIDE_FROM_ABI result_type
n() const { return __p_
.n(); }
82 _LIBCPP_HIDE_FROM_ABI param_type
param() const { return __p_
; }
83 _LIBCPP_HIDE_FROM_ABI
void param(const param_type
& __p
) { __p_
= __p
; }
85 _LIBCPP_HIDE_FROM_ABI result_type
min() const { return 0; }
86 _LIBCPP_HIDE_FROM_ABI result_type
max() const { return numeric_limits
<result_type
>::infinity(); }
88 friend _LIBCPP_HIDE_FROM_ABI
bool operator==(const fisher_f_distribution
& __x
, const fisher_f_distribution
& __y
) {
89 return __x
.__p_
== __y
.__p_
;
91 friend _LIBCPP_HIDE_FROM_ABI
bool operator!=(const fisher_f_distribution
& __x
, const fisher_f_distribution
& __y
) {
96 template <class _RealType
>
97 template <class _URNG
>
98 _RealType fisher_f_distribution
<_RealType
>::operator()(_URNG
& __g
, const param_type
& __p
) {
99 static_assert(__libcpp_random_is_valid_urng
<_URNG
>::value
, "");
100 gamma_distribution
<result_type
> __gdm(__p
.m() * result_type(.5));
101 gamma_distribution
<result_type
> __gdn(__p
.n() * result_type(.5));
102 return __p
.n() * __gdm(__g
) / (__p
.m() * __gdn(__g
));
105 template <class _CharT
, class _Traits
, class _RT
>
106 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
107 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
, const fisher_f_distribution
<_RT
>& __x
) {
108 __save_flags
<_CharT
, _Traits
> __lx(__os
);
109 typedef basic_ostream
<_CharT
, _Traits
> _OStream
;
110 __os
.flags(_OStream::dec
| _OStream::left
| _OStream::fixed
| _OStream::scientific
);
111 _CharT __sp
= __os
.widen(' ');
113 __os
<< __x
.m() << __sp
<< __x
.n();
117 template <class _CharT
, class _Traits
, class _RT
>
118 _LIBCPP_HIDE_FROM_ABI basic_istream
<_CharT
, _Traits
>&
119 operator>>(basic_istream
<_CharT
, _Traits
>& __is
, fisher_f_distribution
<_RT
>& __x
) {
120 typedef fisher_f_distribution
<_RT
> _Eng
;
121 typedef typename
_Eng::result_type result_type
;
122 typedef typename
_Eng::param_type param_type
;
123 __save_flags
<_CharT
, _Traits
> __lx(__is
);
124 typedef basic_istream
<_CharT
, _Traits
> _Istream
;
125 __is
.flags(_Istream::dec
| _Istream::skipws
);
130 __x
.param(param_type(__m
, __n
));
134 _LIBCPP_END_NAMESPACE_STD
138 #endif // _LIBCPP___RANDOM_FISHER_F_DISTRIBUTION_H