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_CHI_SQUARED_DISTRIBUTION_H |
10 | #define _LIBCPP___RANDOM_CHI_SQUARED_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 chi_squared_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 __n_; |
38 | |
39 | public: |
40 | typedef chi_squared_distribution distribution_type; |
41 | |
42 | _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __n = 1) : __n_(__n) {} |
43 | |
44 | _LIBCPP_HIDE_FROM_ABI result_type n() const { return __n_; } |
45 | |
46 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) { |
47 | return __x.__n_ == __y.__n_; |
48 | } |
49 | friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); } |
50 | }; |
51 | |
52 | private: |
53 | param_type __p_; |
54 | |
55 | public: |
56 | // constructor and reset functions |
57 | #ifndef _LIBCPP_CXX03_LANG |
58 | _LIBCPP_HIDE_FROM_ABI chi_squared_distribution() : chi_squared_distribution(1) {} |
59 | _LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(result_type __n) : __p_(param_type(__n)) {} |
60 | #else |
61 | _LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(result_type __n = 1) : __p_(param_type(__n)) {} |
62 | #endif |
63 | _LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(const param_type& __p) : __p_(__p) {} |
64 | _LIBCPP_HIDE_FROM_ABI void reset() {} |
65 | |
66 | // generating functions |
67 | template <class _URNG> |
68 | _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) { |
69 | return (*this)(__g, __p_); |
70 | } |
71 | template <class _URNG> |
72 | _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) { |
73 | return gamma_distribution<result_type>(__p.n() / 2, 2)(__g); |
74 | } |
75 | |
76 | // property functions |
77 | _LIBCPP_HIDE_FROM_ABI result_type n() const { return __p_.n(); } |
78 | |
79 | _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; } |
80 | _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; } |
81 | |
82 | _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; } |
83 | _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); } |
84 | |
85 | friend _LIBCPP_HIDE_FROM_ABI bool |
86 | operator==(const chi_squared_distribution& __x, const chi_squared_distribution& __y) { |
87 | return __x.__p_ == __y.__p_; |
88 | } |
89 | friend _LIBCPP_HIDE_FROM_ABI bool |
90 | operator!=(const chi_squared_distribution& __x, const chi_squared_distribution& __y) { |
91 | return !(__x == __y); |
92 | } |
93 | }; |
94 | |
95 | template <class _CharT, class _Traits, class _RT> |
96 | _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
97 | operator<<(basic_ostream<_CharT, _Traits>& __os, const chi_squared_distribution<_RT>& __x) { |
98 | __save_flags<_CharT, _Traits> __lx(__os); |
99 | typedef basic_ostream<_CharT, _Traits> _OStream; |
100 | __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific); |
101 | __os << __x.n(); |
102 | return __os; |
103 | } |
104 | |
105 | template <class _CharT, class _Traits, class _RT> |
106 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
107 | operator>>(basic_istream<_CharT, _Traits>& __is, chi_squared_distribution<_RT>& __x) { |
108 | typedef chi_squared_distribution<_RT> _Eng; |
109 | typedef typename _Eng::result_type result_type; |
110 | typedef typename _Eng::param_type param_type; |
111 | __save_flags<_CharT, _Traits> __lx(__is); |
112 | typedef basic_istream<_CharT, _Traits> _Istream; |
113 | __is.flags(_Istream::dec | _Istream::skipws); |
114 | result_type __n; |
115 | __is >> __n; |
116 | if (!__is.fail()) |
117 | __x.param(param_type(__n)); |
118 | return __is; |
119 | } |
120 | |
121 | _LIBCPP_END_NAMESPACE_STD |
122 | |
123 | _LIBCPP_POP_MACROS |
124 | |
125 | #endif // _LIBCPP___RANDOM_CHI_SQUARED_DISTRIBUTION_H |
126 | |