xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__random/fisher_f_distribution.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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___CXX03___RANDOM_FISHER_F_DISTRIBUTION_H
10 #define _LIBCPP___CXX03___RANDOM_FISHER_F_DISTRIBUTION_H
11 
12 #include <__cxx03/__config>
13 #include <__cxx03/__random/gamma_distribution.h>
14 #include <__cxx03/__random/is_valid.h>
15 #include <__cxx03/iosfwd>
16 #include <__cxx03/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 <__cxx03/__undef_macros>
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
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");
31 
32 public:
33   // types
34   typedef _RealType result_type;
35 
36   class _LIBCPP_TEMPLATE_VIS param_type {
37     result_type __m_;
38     result_type __n_;
39 
40   public:
41     typedef fisher_f_distribution distribution_type;
42 
__m_(__m)43     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __m = 1, result_type __n = 1) : __m_(__m), __n_(__n) {}
44 
m()45     _LIBCPP_HIDE_FROM_ABI result_type m() const { return __m_; }
n()46     _LIBCPP_HIDE_FROM_ABI result_type n() const { return __n_; }
47 
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_;
50     }
51     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
52   };
53 
54 private:
55   param_type __p_;
56 
57 public:
58   // constructor and reset functions
59   _LIBCPP_HIDE_FROM_ABI explicit fisher_f_distribution(result_type __m = 1, result_type __n = 1)
__p_(param_type (__m,__n))60       : __p_(param_type(__m, __n)) {}
fisher_f_distribution(const param_type & __p)61   _LIBCPP_HIDE_FROM_ABI explicit fisher_f_distribution(const param_type& __p) : __p_(__p) {}
reset()62   _LIBCPP_HIDE_FROM_ABI void reset() {}
63 
64   // generating functions
65   template <class _URNG>
operator()66   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
67     return (*this)(__g, __p_);
68   }
69   template <class _URNG>
70   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
71 
72   // property functions
m()73   _LIBCPP_HIDE_FROM_ABI result_type m() const { return __p_.m(); }
n()74   _LIBCPP_HIDE_FROM_ABI result_type n() const { return __p_.n(); }
75 
param()76   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)77   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
78 
min()79   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()80   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
81 
82   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const fisher_f_distribution& __x, const fisher_f_distribution& __y) {
83     return __x.__p_ == __y.__p_;
84   }
85   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const fisher_f_distribution& __x, const fisher_f_distribution& __y) {
86     return !(__x == __y);
87   }
88 };
89 
90 template <class _RealType>
91 template <class _URNG>
operator()92 _RealType fisher_f_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
93   static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
94   gamma_distribution<result_type> __gdm(__p.m() * result_type(.5));
95   gamma_distribution<result_type> __gdn(__p.n() * result_type(.5));
96   return __p.n() * __gdm(__g) / (__p.m() * __gdn(__g));
97 }
98 
99 template <class _CharT, class _Traits, class _RT>
100 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
101 operator<<(basic_ostream<_CharT, _Traits>& __os, const fisher_f_distribution<_RT>& __x) {
102   __save_flags<_CharT, _Traits> __lx(__os);
103   typedef basic_ostream<_CharT, _Traits> _OStream;
104   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
105   _CharT __sp = __os.widen(' ');
106   __os.fill(__sp);
107   __os << __x.m() << __sp << __x.n();
108   return __os;
109 }
110 
111 template <class _CharT, class _Traits, class _RT>
112 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
113 operator>>(basic_istream<_CharT, _Traits>& __is, fisher_f_distribution<_RT>& __x) {
114   typedef fisher_f_distribution<_RT> _Eng;
115   typedef typename _Eng::result_type result_type;
116   typedef typename _Eng::param_type param_type;
117   __save_flags<_CharT, _Traits> __lx(__is);
118   typedef basic_istream<_CharT, _Traits> _Istream;
119   __is.flags(_Istream::dec | _Istream::skipws);
120   result_type __m;
121   result_type __n;
122   __is >> __m >> __n;
123   if (!__is.fail())
124     __x.param(param_type(__m, __n));
125   return __is;
126 }
127 
128 _LIBCPP_END_NAMESPACE_STD
129 
130 _LIBCPP_POP_MACROS
131 
132 #endif // _LIBCPP___CXX03___RANDOM_FISHER_F_DISTRIBUTION_H
133