xref: /freebsd-src/contrib/llvm-project/libcxx/include/__random/chi_squared_distribution.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
14824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
24824e7fdSDimitry Andric //
34824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
54824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64824e7fdSDimitry Andric //
74824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
84824e7fdSDimitry Andric 
94824e7fdSDimitry Andric #ifndef _LIBCPP___RANDOM_CHI_SQUARED_DISTRIBUTION_H
104824e7fdSDimitry Andric #define _LIBCPP___RANDOM_CHI_SQUARED_DISTRIBUTION_H
114824e7fdSDimitry Andric 
124824e7fdSDimitry Andric #include <__config>
134824e7fdSDimitry Andric #include <__random/gamma_distribution.h>
145f757f3fSDimitry Andric #include <__random/is_valid.h>
154824e7fdSDimitry Andric #include <iosfwd>
1604eeddc0SDimitry Andric #include <limits>
174824e7fdSDimitry Andric 
184824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
194824e7fdSDimitry Andric #  pragma GCC system_header
204824e7fdSDimitry Andric #endif
214824e7fdSDimitry Andric 
224824e7fdSDimitry Andric _LIBCPP_PUSH_MACROS
234824e7fdSDimitry Andric #include <__undef_macros>
244824e7fdSDimitry Andric 
254824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
264824e7fdSDimitry Andric 
274824e7fdSDimitry Andric template <class _RealType = double>
28*cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS chi_squared_distribution {
295f757f3fSDimitry Andric   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
305f757f3fSDimitry Andric                 "RealType must be a supported floating-point type");
315f757f3fSDimitry Andric 
324824e7fdSDimitry Andric public:
334824e7fdSDimitry Andric   // types
344824e7fdSDimitry Andric   typedef _RealType result_type;
354824e7fdSDimitry Andric 
36*cb14a3feSDimitry Andric   class _LIBCPP_TEMPLATE_VIS param_type {
374824e7fdSDimitry Andric     result_type __n_;
38*cb14a3feSDimitry Andric 
394824e7fdSDimitry Andric   public:
404824e7fdSDimitry Andric     typedef chi_squared_distribution distribution_type;
414824e7fdSDimitry Andric 
__n_(__n)42*cb14a3feSDimitry Andric     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __n = 1) : __n_(__n) {}
434824e7fdSDimitry Andric 
n()44*cb14a3feSDimitry Andric     _LIBCPP_HIDE_FROM_ABI result_type n() const { return __n_; }
454824e7fdSDimitry Andric 
46*cb14a3feSDimitry Andric     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
47*cb14a3feSDimitry Andric       return __x.__n_ == __y.__n_;
48*cb14a3feSDimitry Andric     }
49*cb14a3feSDimitry Andric     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
504824e7fdSDimitry Andric   };
514824e7fdSDimitry Andric 
524824e7fdSDimitry Andric private:
534824e7fdSDimitry Andric   param_type __p_;
544824e7fdSDimitry Andric 
554824e7fdSDimitry Andric public:
564824e7fdSDimitry Andric   // constructor and reset functions
574824e7fdSDimitry Andric #ifndef _LIBCPP_CXX03_LANG
chi_squared_distribution()58*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI chi_squared_distribution() : chi_squared_distribution(1) {}
chi_squared_distribution(result_type __n)59*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(result_type __n) : __p_(param_type(__n)) {}
604824e7fdSDimitry Andric #else
61*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(result_type __n = 1) : __p_(param_type(__n)) {}
624824e7fdSDimitry Andric #endif
chi_squared_distribution(const param_type & __p)63*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(const param_type& __p) : __p_(__p) {}
reset()64*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void reset() {}
654824e7fdSDimitry Andric 
664824e7fdSDimitry Andric   // generating functions
674824e7fdSDimitry Andric   template <class _URNG>
operator()68*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
69*cb14a3feSDimitry Andric     return (*this)(__g, __p_);
70*cb14a3feSDimitry Andric   }
714824e7fdSDimitry Andric   template <class _URNG>
operator()72*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) {
73*cb14a3feSDimitry Andric     return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);
74*cb14a3feSDimitry Andric   }
754824e7fdSDimitry Andric 
764824e7fdSDimitry Andric   // property functions
n()77*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type n() const { return __p_.n(); }
784824e7fdSDimitry Andric 
param()79*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
param(const param_type & __p)80*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
814824e7fdSDimitry Andric 
min()82*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
max()83*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
844824e7fdSDimitry Andric 
85*cb14a3feSDimitry Andric   friend _LIBCPP_HIDE_FROM_ABI bool
86*cb14a3feSDimitry Andric   operator==(const chi_squared_distribution& __x, const chi_squared_distribution& __y) {
87*cb14a3feSDimitry Andric     return __x.__p_ == __y.__p_;
88*cb14a3feSDimitry Andric   }
89*cb14a3feSDimitry Andric   friend _LIBCPP_HIDE_FROM_ABI bool
90*cb14a3feSDimitry Andric   operator!=(const chi_squared_distribution& __x, const chi_squared_distribution& __y) {
91*cb14a3feSDimitry Andric     return !(__x == __y);
92*cb14a3feSDimitry Andric   }
934824e7fdSDimitry Andric };
944824e7fdSDimitry Andric 
954824e7fdSDimitry Andric template <class _CharT, class _Traits, class _RT>
96bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
97*cb14a3feSDimitry Andric operator<<(basic_ostream<_CharT, _Traits>& __os, const chi_squared_distribution<_RT>& __x) {
984824e7fdSDimitry Andric   __save_flags<_CharT, _Traits> __lx(__os);
994824e7fdSDimitry Andric   typedef basic_ostream<_CharT, _Traits> _OStream;
100*cb14a3feSDimitry Andric   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
1014824e7fdSDimitry Andric   __os << __x.n();
1024824e7fdSDimitry Andric   return __os;
1034824e7fdSDimitry Andric }
1044824e7fdSDimitry Andric 
1054824e7fdSDimitry Andric template <class _CharT, class _Traits, class _RT>
106bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
107*cb14a3feSDimitry Andric operator>>(basic_istream<_CharT, _Traits>& __is, chi_squared_distribution<_RT>& __x) {
1084824e7fdSDimitry Andric   typedef chi_squared_distribution<_RT> _Eng;
1094824e7fdSDimitry Andric   typedef typename _Eng::result_type result_type;
1104824e7fdSDimitry Andric   typedef typename _Eng::param_type param_type;
1114824e7fdSDimitry Andric   __save_flags<_CharT, _Traits> __lx(__is);
1124824e7fdSDimitry Andric   typedef basic_istream<_CharT, _Traits> _Istream;
1134824e7fdSDimitry Andric   __is.flags(_Istream::dec | _Istream::skipws);
1144824e7fdSDimitry Andric   result_type __n;
1154824e7fdSDimitry Andric   __is >> __n;
1164824e7fdSDimitry Andric   if (!__is.fail())
1174824e7fdSDimitry Andric     __x.param(param_type(__n));
1184824e7fdSDimitry Andric   return __is;
1194824e7fdSDimitry Andric }
1204824e7fdSDimitry Andric 
1214824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
1224824e7fdSDimitry Andric 
1234824e7fdSDimitry Andric _LIBCPP_POP_MACROS
1244824e7fdSDimitry Andric 
1254824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_CHI_SQUARED_DISTRIBUTION_H
126