Back to home page

EIC code displayed by LXR

 
 

    


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

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___CXX03___RANDOM_GAMMA_DISTRIBUTION_H
0010 #define _LIBCPP___CXX03___RANDOM_GAMMA_DISTRIBUTION_H
0011 
0012 #include <__cxx03/__config>
0013 #include <__cxx03/__random/exponential_distribution.h>
0014 #include <__cxx03/__random/is_valid.h>
0015 #include <__cxx03/__random/uniform_real_distribution.h>
0016 #include <__cxx03/cmath>
0017 #include <__cxx03/iosfwd>
0018 #include <__cxx03/limits>
0019 
0020 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0021 #  pragma GCC system_header
0022 #endif
0023 
0024 _LIBCPP_PUSH_MACROS
0025 #include <__cxx03/__undef_macros>
0026 
0027 _LIBCPP_BEGIN_NAMESPACE_STD
0028 
0029 template <class _RealType = double>
0030 class _LIBCPP_TEMPLATE_VIS gamma_distribution {
0031   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
0032                 "RealType must be a supported floating-point type");
0033 
0034 public:
0035   // types
0036   typedef _RealType result_type;
0037 
0038   class _LIBCPP_TEMPLATE_VIS param_type {
0039     result_type __alpha_;
0040     result_type __beta_;
0041 
0042   public:
0043     typedef gamma_distribution distribution_type;
0044 
0045     _LIBCPP_HIDE_FROM_ABI explicit param_type(result_type __alpha = 1, result_type __beta = 1)
0046         : __alpha_(__alpha), __beta_(__beta) {}
0047 
0048     _LIBCPP_HIDE_FROM_ABI result_type alpha() const { return __alpha_; }
0049     _LIBCPP_HIDE_FROM_ABI result_type beta() const { return __beta_; }
0050 
0051     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0052       return __x.__alpha_ == __y.__alpha_ && __x.__beta_ == __y.__beta_;
0053     }
0054     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0055   };
0056 
0057 private:
0058   param_type __p_;
0059 
0060 public:
0061   // constructors and reset functions
0062 #ifndef _LIBCPP_CXX03_LANG
0063   _LIBCPP_HIDE_FROM_ABI gamma_distribution() : gamma_distribution(1) {}
0064   _LIBCPP_HIDE_FROM_ABI explicit gamma_distribution(result_type __alpha, result_type __beta = 1)
0065       : __p_(param_type(__alpha, __beta)) {}
0066 #else
0067   _LIBCPP_HIDE_FROM_ABI explicit gamma_distribution(result_type __alpha = 1, result_type __beta = 1)
0068       : __p_(param_type(__alpha, __beta)) {}
0069 #endif
0070   _LIBCPP_HIDE_FROM_ABI explicit gamma_distribution(const param_type& __p) : __p_(__p) {}
0071   _LIBCPP_HIDE_FROM_ABI void reset() {}
0072 
0073   // generating functions
0074   template <class _URNG>
0075   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0076     return (*this)(__g, __p_);
0077   }
0078   template <class _URNG>
0079   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
0080 
0081   // property functions
0082   _LIBCPP_HIDE_FROM_ABI result_type alpha() const { return __p_.alpha(); }
0083   _LIBCPP_HIDE_FROM_ABI result_type beta() const { return __p_.beta(); }
0084 
0085   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0086   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0087 
0088   _LIBCPP_HIDE_FROM_ABI result_type min() const { return 0; }
0089   _LIBCPP_HIDE_FROM_ABI result_type max() const { return numeric_limits<result_type>::infinity(); }
0090 
0091   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const gamma_distribution& __x, const gamma_distribution& __y) {
0092     return __x.__p_ == __y.__p_;
0093   }
0094   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const gamma_distribution& __x, const gamma_distribution& __y) {
0095     return !(__x == __y);
0096   }
0097 };
0098 
0099 template <class _RealType>
0100 template <class _URNG>
0101 _RealType gamma_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
0102   static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0103   result_type __a = __p.alpha();
0104   uniform_real_distribution<result_type> __gen(0, 1);
0105   exponential_distribution<result_type> __egen;
0106   result_type __x;
0107   if (__a == 1)
0108     __x = __egen(__g);
0109   else if (__a > 1) {
0110     const result_type __b = __a - 1;
0111     const result_type __c = 3 * __a - result_type(0.75);
0112     while (true) {
0113       const result_type __u = __gen(__g);
0114       const result_type __v = __gen(__g);
0115       const result_type __w = __u * (1 - __u);
0116       if (__w != 0) {
0117         const result_type __y = std::sqrt(__c / __w) * (__u - result_type(0.5));
0118         __x                   = __b + __y;
0119         if (__x >= 0) {
0120           const result_type __z = 64 * __w * __w * __w * __v * __v;
0121           if (__z <= 1 - 2 * __y * __y / __x)
0122             break;
0123           if (std::log(__z) <= 2 * (__b * std::log(__x / __b) - __y))
0124             break;
0125         }
0126       }
0127     }
0128   } else // __a < 1
0129   {
0130     while (true) {
0131       const result_type __u  = __gen(__g);
0132       const result_type __es = __egen(__g);
0133       if (__u <= 1 - __a) {
0134         __x = std::pow(__u, 1 / __a);
0135         if (__x <= __es)
0136           break;
0137       } else {
0138         const result_type __e = -std::log((1 - __u) / __a);
0139         __x                   = std::pow(1 - __a + __a * __e, 1 / __a);
0140         if (__x <= __e + __es)
0141           break;
0142       }
0143     }
0144   }
0145   return __x * __p.beta();
0146 }
0147 
0148 template <class _CharT, class _Traits, class _RT>
0149 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0150 operator<<(basic_ostream<_CharT, _Traits>& __os, const gamma_distribution<_RT>& __x) {
0151   __save_flags<_CharT, _Traits> __lx(__os);
0152   typedef basic_ostream<_CharT, _Traits> _OStream;
0153   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0154   _CharT __sp = __os.widen(' ');
0155   __os.fill(__sp);
0156   __os << __x.alpha() << __sp << __x.beta();
0157   return __os;
0158 }
0159 
0160 template <class _CharT, class _Traits, class _RT>
0161 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0162 operator>>(basic_istream<_CharT, _Traits>& __is, gamma_distribution<_RT>& __x) {
0163   typedef gamma_distribution<_RT> _Eng;
0164   typedef typename _Eng::result_type result_type;
0165   typedef typename _Eng::param_type param_type;
0166   __save_flags<_CharT, _Traits> __lx(__is);
0167   typedef basic_istream<_CharT, _Traits> _Istream;
0168   __is.flags(_Istream::dec | _Istream::skipws);
0169   result_type __alpha;
0170   result_type __beta;
0171   __is >> __alpha >> __beta;
0172   if (!__is.fail())
0173     __x.param(param_type(__alpha, __beta));
0174   return __is;
0175 }
0176 
0177 _LIBCPP_END_NAMESPACE_STD
0178 
0179 _LIBCPP_POP_MACROS
0180 
0181 #endif // _LIBCPP___CXX03___RANDOM_GAMMA_DISTRIBUTION_H