Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:59

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___RANDOM_CHI_SQUARED_DISTRIBUTION_H
0010 #define _LIBCPP___RANDOM_CHI_SQUARED_DISTRIBUTION_H
0011 
0012 #include <__config>
0013 #include <__random/gamma_distribution.h>
0014 #include <__random/is_valid.h>
0015 #include <iosfwd>
0016 #include <limits>
0017 
0018 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0019 #  pragma GCC system_header
0020 #endif
0021 
0022 _LIBCPP_PUSH_MACROS
0023 #include <__undef_macros>
0024 
0025 _LIBCPP_BEGIN_NAMESPACE_STD
0026 
0027 template <class _RealType = double>
0028 class _LIBCPP_TEMPLATE_VIS chi_squared_distribution {
0029   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
0030                 "RealType must be a supported floating-point type");
0031 
0032 public:
0033   // types
0034   typedef _RealType result_type;
0035 
0036   class _LIBCPP_TEMPLATE_VIS param_type {
0037     result_type __n_;
0038 
0039   public:
0040     typedef chi_squared_distribution distribution_type;
0041 
0042     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __n = 1) : __n_(__n) {}
0043 
0044     _LIBCPP_HIDE_FROM_ABI result_type n() const { return __n_; }
0045 
0046     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0047       return __x.__n_ == __y.__n_;
0048     }
0049     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0050   };
0051 
0052 private:
0053   param_type __p_;
0054 
0055 public:
0056   // constructor and reset functions
0057 #ifndef _LIBCPP_CXX03_LANG
0058   _LIBCPP_HIDE_FROM_ABI chi_squared_distribution() : chi_squared_distribution(1) {}
0059   _LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(result_type __n) : __p_(param_type(__n)) {}
0060 #else
0061   _LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(result_type __n = 1) : __p_(param_type(__n)) {}
0062 #endif
0063   _LIBCPP_HIDE_FROM_ABI explicit chi_squared_distribution(const param_type& __p) : __p_(__p) {}
0064   _LIBCPP_HIDE_FROM_ABI void reset() {}
0065 
0066   // generating functions
0067   template <class _URNG>
0068   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0069     return (*this)(__g, __p_);
0070   }
0071   template <class _URNG>
0072   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p) {
0073     return gamma_distribution<result_type>(__p.n() / 2, 2)(__g);
0074   }
0075 
0076   // property functions
0077   _LIBCPP_HIDE_FROM_ABI result_type n() const { return __p_.n(); }
0078 
0079   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0080   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0081 
0082   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
0083   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
0084 
0085   friend _LIBCPP_HIDE_FROM_ABI bool
0086   operator==(const chi_squared_distribution& __x, const chi_squared_distribution& __y) {
0087     return __x.__p_ == __y.__p_;
0088   }
0089   friend _LIBCPP_HIDE_FROM_ABI bool
0090   operator!=(const chi_squared_distribution& __x, const chi_squared_distribution& __y) {
0091     return !(__x == __y);
0092   }
0093 };
0094 
0095 template <class _CharT, class _Traits, class _RT>
0096 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0097 operator<<(basic_ostream<_CharT, _Traits>& __os, const chi_squared_distribution<_RT>& __x) {
0098   __save_flags<_CharT, _Traits> __lx(__os);
0099   typedef basic_ostream<_CharT, _Traits> _OStream;
0100   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0101   __os << __x.n();
0102   return __os;
0103 }
0104 
0105 template <class _CharT, class _Traits, class _RT>
0106 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0107 operator>>(basic_istream<_CharT, _Traits>& __is, chi_squared_distribution<_RT>& __x) {
0108   typedef chi_squared_distribution<_RT> _Eng;
0109   typedef typename _Eng::result_type result_type;
0110   typedef typename _Eng::param_type param_type;
0111   __save_flags<_CharT, _Traits> __lx(__is);
0112   typedef basic_istream<_CharT, _Traits> _Istream;
0113   __is.flags(_Istream::dec | _Istream::skipws);
0114   result_type __n;
0115   __is >> __n;
0116   if (!__is.fail())
0117     __x.param(param_type(__n));
0118   return __is;
0119 }
0120 
0121 _LIBCPP_END_NAMESPACE_STD
0122 
0123 _LIBCPP_POP_MACROS
0124 
0125 #endif // _LIBCPP___RANDOM_CHI_SQUARED_DISTRIBUTION_H