Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:00

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_PIECEWISE_LINEAR_DISTRIBUTION_H
0010 #define _LIBCPP___RANDOM_PIECEWISE_LINEAR_DISTRIBUTION_H
0011 
0012 #include <__algorithm/upper_bound.h>
0013 #include <__config>
0014 #include <__cstddef/ptrdiff_t.h>
0015 #include <__random/is_valid.h>
0016 #include <__random/uniform_real_distribution.h>
0017 #include <__vector/comparison.h>
0018 #include <__vector/vector.h>
0019 #include <cmath>
0020 #include <initializer_list>
0021 #include <iosfwd>
0022 
0023 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0024 #  pragma GCC system_header
0025 #endif
0026 
0027 _LIBCPP_PUSH_MACROS
0028 #include <__undef_macros>
0029 
0030 _LIBCPP_BEGIN_NAMESPACE_STD
0031 
0032 template <class _RealType = double>
0033 class _LIBCPP_TEMPLATE_VIS piecewise_linear_distribution {
0034   static_assert(__libcpp_random_is_valid_realtype<_RealType>::value,
0035                 "RealType must be a supported floating-point type");
0036 
0037 public:
0038   // types
0039   typedef _RealType result_type;
0040 
0041   class _LIBCPP_TEMPLATE_VIS param_type {
0042     vector<result_type> __b_;
0043     vector<result_type> __densities_;
0044     vector<result_type> __areas_;
0045 
0046   public:
0047     typedef piecewise_linear_distribution distribution_type;
0048 
0049     _LIBCPP_HIDE_FROM_ABI param_type();
0050     template <class _InputIteratorB, class _InputIteratorW>
0051     _LIBCPP_HIDE_FROM_ABI param_type(_InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w);
0052 #ifndef _LIBCPP_CXX03_LANG
0053     template <class _UnaryOperation>
0054     _LIBCPP_HIDE_FROM_ABI param_type(initializer_list<result_type> __bl, _UnaryOperation __fw);
0055 #endif // _LIBCPP_CXX03_LANG
0056     template <class _UnaryOperation>
0057     _LIBCPP_HIDE_FROM_ABI param_type(size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw);
0058     _LIBCPP_HIDE_FROM_ABI param_type(param_type const&) = default;
0059     _LIBCPP_HIDE_FROM_ABI param_type& operator=(const param_type& __rhs);
0060 
0061     _LIBCPP_HIDE_FROM_ABI vector<result_type> intervals() const { return __b_; }
0062     _LIBCPP_HIDE_FROM_ABI vector<result_type> densities() const { return __densities_; }
0063 
0064     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const param_type& __x, const param_type& __y) {
0065       return __x.__densities_ == __y.__densities_ && __x.__b_ == __y.__b_;
0066     }
0067     friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const param_type& __x, const param_type& __y) { return !(__x == __y); }
0068 
0069   private:
0070     _LIBCPP_HIDE_FROM_ABI void __init();
0071 
0072     friend class piecewise_linear_distribution;
0073 
0074     template <class _CharT, class _Traits, class _RT>
0075     friend basic_ostream<_CharT, _Traits>&
0076     operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_linear_distribution<_RT>& __x);
0077 
0078     template <class _CharT, class _Traits, class _RT>
0079     friend basic_istream<_CharT, _Traits>&
0080     operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_linear_distribution<_RT>& __x);
0081   };
0082 
0083 private:
0084   param_type __p_;
0085 
0086 public:
0087   // constructor and reset functions
0088   _LIBCPP_HIDE_FROM_ABI piecewise_linear_distribution() {}
0089   template <class _InputIteratorB, class _InputIteratorW>
0090   _LIBCPP_HIDE_FROM_ABI
0091   piecewise_linear_distribution(_InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w)
0092       : __p_(__f_b, __l_b, __f_w) {}
0093 
0094 #ifndef _LIBCPP_CXX03_LANG
0095   template <class _UnaryOperation>
0096   _LIBCPP_HIDE_FROM_ABI piecewise_linear_distribution(initializer_list<result_type> __bl, _UnaryOperation __fw)
0097       : __p_(__bl, __fw) {}
0098 #endif // _LIBCPP_CXX03_LANG
0099 
0100   template <class _UnaryOperation>
0101   _LIBCPP_HIDE_FROM_ABI
0102   piecewise_linear_distribution(size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
0103       : __p_(__nw, __xmin, __xmax, __fw) {}
0104 
0105   _LIBCPP_HIDE_FROM_ABI explicit piecewise_linear_distribution(const param_type& __p) : __p_(__p) {}
0106 
0107   _LIBCPP_HIDE_FROM_ABI void reset() {}
0108 
0109   // generating functions
0110   template <class _URNG>
0111   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g) {
0112     return (*this)(__g, __p_);
0113   }
0114   template <class _URNG>
0115   _LIBCPP_HIDE_FROM_ABI result_type operator()(_URNG& __g, const param_type& __p);
0116 
0117   // property functions
0118   _LIBCPP_HIDE_FROM_ABI vector<result_type> intervals() const { return __p_.intervals(); }
0119   _LIBCPP_HIDE_FROM_ABI vector<result_type> densities() const { return __p_.densities(); }
0120 
0121   _LIBCPP_HIDE_FROM_ABI param_type param() const { return __p_; }
0122   _LIBCPP_HIDE_FROM_ABI void param(const param_type& __p) { __p_ = __p; }
0123 
0124   _LIBCPP_HIDE_FROM_ABI result_type min() const { return __p_.__b_.front(); }
0125   _LIBCPP_HIDE_FROM_ABI result_type max() const { return __p_.__b_.back(); }
0126 
0127   friend _LIBCPP_HIDE_FROM_ABI bool
0128   operator==(const piecewise_linear_distribution& __x, const piecewise_linear_distribution& __y) {
0129     return __x.__p_ == __y.__p_;
0130   }
0131   friend _LIBCPP_HIDE_FROM_ABI bool
0132   operator!=(const piecewise_linear_distribution& __x, const piecewise_linear_distribution& __y) {
0133     return !(__x == __y);
0134   }
0135 
0136   template <class _CharT, class _Traits, class _RT>
0137   friend basic_ostream<_CharT, _Traits>&
0138   operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_linear_distribution<_RT>& __x);
0139 
0140   template <class _CharT, class _Traits, class _RT>
0141   friend basic_istream<_CharT, _Traits>&
0142   operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_linear_distribution<_RT>& __x);
0143 };
0144 
0145 template <class _RealType>
0146 typename piecewise_linear_distribution<_RealType>::param_type&
0147 piecewise_linear_distribution<_RealType>::param_type::operator=(const param_type& __rhs) {
0148   //  These can throw
0149   __b_.reserve(__rhs.__b_.size());
0150   __densities_.reserve(__rhs.__densities_.size());
0151   __areas_.reserve(__rhs.__areas_.size());
0152 
0153   //  These can not throw
0154   __b_         = __rhs.__b_;
0155   __densities_ = __rhs.__densities_;
0156   __areas_     = __rhs.__areas_;
0157   return *this;
0158 }
0159 
0160 template <class _RealType>
0161 void piecewise_linear_distribution<_RealType>::param_type::__init() {
0162   __areas_.assign(__densities_.size() - 1, result_type());
0163   result_type __sp = 0;
0164   for (size_t __i = 0; __i < __areas_.size(); ++__i) {
0165     __areas_[__i] = (__densities_[__i + 1] + __densities_[__i]) * (__b_[__i + 1] - __b_[__i]) * .5;
0166     __sp += __areas_[__i];
0167   }
0168   for (size_t __i = __areas_.size(); __i > 1;) {
0169     --__i;
0170     __areas_[__i] = __areas_[__i - 1] / __sp;
0171   }
0172   __areas_[0] = 0;
0173   for (size_t __i = 1; __i < __areas_.size(); ++__i)
0174     __areas_[__i] += __areas_[__i - 1];
0175   for (size_t __i = 0; __i < __densities_.size(); ++__i)
0176     __densities_[__i] /= __sp;
0177 }
0178 
0179 template <class _RealType>
0180 piecewise_linear_distribution<_RealType>::param_type::param_type() : __b_(2), __densities_(2, 1.0), __areas_(1, 0.0) {
0181   __b_[1] = 1;
0182 }
0183 
0184 template <class _RealType>
0185 template <class _InputIteratorB, class _InputIteratorW>
0186 piecewise_linear_distribution<_RealType>::param_type::param_type(
0187     _InputIteratorB __f_b, _InputIteratorB __l_b, _InputIteratorW __f_w)
0188     : __b_(__f_b, __l_b) {
0189   if (__b_.size() < 2) {
0190     __b_.resize(2);
0191     __b_[0] = 0;
0192     __b_[1] = 1;
0193     __densities_.assign(2, 1.0);
0194     __areas_.assign(1, 0.0);
0195   } else {
0196     __densities_.reserve(__b_.size());
0197     for (size_t __i = 0; __i < __b_.size(); ++__i, ++__f_w)
0198       __densities_.push_back(*__f_w);
0199     __init();
0200   }
0201 }
0202 
0203 #ifndef _LIBCPP_CXX03_LANG
0204 
0205 template <class _RealType>
0206 template <class _UnaryOperation>
0207 piecewise_linear_distribution<_RealType>::param_type::param_type(
0208     initializer_list<result_type> __bl, _UnaryOperation __fw)
0209     : __b_(__bl.begin(), __bl.end()) {
0210   if (__b_.size() < 2) {
0211     __b_.resize(2);
0212     __b_[0] = 0;
0213     __b_[1] = 1;
0214     __densities_.assign(2, 1.0);
0215     __areas_.assign(1, 0.0);
0216   } else {
0217     __densities_.reserve(__b_.size());
0218     for (size_t __i = 0; __i < __b_.size(); ++__i)
0219       __densities_.push_back(__fw(__b_[__i]));
0220     __init();
0221   }
0222 }
0223 
0224 #endif // _LIBCPP_CXX03_LANG
0225 
0226 template <class _RealType>
0227 template <class _UnaryOperation>
0228 piecewise_linear_distribution<_RealType>::param_type::param_type(
0229     size_t __nw, result_type __xmin, result_type __xmax, _UnaryOperation __fw)
0230     : __b_(__nw == 0 ? 2 : __nw + 1) {
0231   size_t __n      = __b_.size() - 1;
0232   result_type __d = (__xmax - __xmin) / __n;
0233   __densities_.reserve(__b_.size());
0234   for (size_t __i = 0; __i < __n; ++__i) {
0235     __b_[__i] = __xmin + __i * __d;
0236     __densities_.push_back(__fw(__b_[__i]));
0237   }
0238   __b_[__n] = __xmax;
0239   __densities_.push_back(__fw(__b_[__n]));
0240   __init();
0241 }
0242 
0243 template <class _RealType>
0244 template <class _URNG>
0245 _RealType piecewise_linear_distribution<_RealType>::operator()(_URNG& __g, const param_type& __p) {
0246   static_assert(__libcpp_random_is_valid_urng<_URNG>::value, "");
0247   typedef uniform_real_distribution<result_type> _Gen;
0248   result_type __u = _Gen()(__g);
0249   ptrdiff_t __k   = std::upper_bound(__p.__areas_.begin(), __p.__areas_.end(), __u) - __p.__areas_.begin() - 1;
0250   __u -= __p.__areas_[__k];
0251   const result_type __dk     = __p.__densities_[__k];
0252   const result_type __dk1    = __p.__densities_[__k + 1];
0253   const result_type __deltad = __dk1 - __dk;
0254   const result_type __bk     = __p.__b_[__k];
0255   if (__deltad == 0)
0256     return __u / __dk + __bk;
0257   const result_type __bk1    = __p.__b_[__k + 1];
0258   const result_type __deltab = __bk1 - __bk;
0259   return (__bk * __dk1 - __bk1 * __dk + std::sqrt(__deltab * (__deltab * __dk * __dk + 2 * __deltad * __u))) / __deltad;
0260 }
0261 
0262 template <class _CharT, class _Traits, class _RT>
0263 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0264 operator<<(basic_ostream<_CharT, _Traits>& __os, const piecewise_linear_distribution<_RT>& __x) {
0265   __save_flags<_CharT, _Traits> __lx(__os);
0266   typedef basic_ostream<_CharT, _Traits> _OStream;
0267   __os.flags(_OStream::dec | _OStream::left | _OStream::fixed | _OStream::scientific);
0268   _CharT __sp = __os.widen(' ');
0269   __os.fill(__sp);
0270   size_t __n = __x.__p_.__b_.size();
0271   __os << __n;
0272   for (size_t __i = 0; __i < __n; ++__i)
0273     __os << __sp << __x.__p_.__b_[__i];
0274   __n = __x.__p_.__densities_.size();
0275   __os << __sp << __n;
0276   for (size_t __i = 0; __i < __n; ++__i)
0277     __os << __sp << __x.__p_.__densities_[__i];
0278   __n = __x.__p_.__areas_.size();
0279   __os << __sp << __n;
0280   for (size_t __i = 0; __i < __n; ++__i)
0281     __os << __sp << __x.__p_.__areas_[__i];
0282   return __os;
0283 }
0284 
0285 template <class _CharT, class _Traits, class _RT>
0286 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0287 operator>>(basic_istream<_CharT, _Traits>& __is, piecewise_linear_distribution<_RT>& __x) {
0288   typedef piecewise_linear_distribution<_RT> _Eng;
0289   typedef typename _Eng::result_type result_type;
0290   __save_flags<_CharT, _Traits> __lx(__is);
0291   typedef basic_istream<_CharT, _Traits> _Istream;
0292   __is.flags(_Istream::dec | _Istream::skipws);
0293   size_t __n;
0294   __is >> __n;
0295   vector<result_type> __b(__n);
0296   for (size_t __i = 0; __i < __n; ++__i)
0297     __is >> __b[__i];
0298   __is >> __n;
0299   vector<result_type> __densities(__n);
0300   for (size_t __i = 0; __i < __n; ++__i)
0301     __is >> __densities[__i];
0302   __is >> __n;
0303   vector<result_type> __areas(__n);
0304   for (size_t __i = 0; __i < __n; ++__i)
0305     __is >> __areas[__i];
0306   if (!__is.fail()) {
0307     swap(__x.__p_.__b_, __b);
0308     swap(__x.__p_.__densities_, __densities);
0309     swap(__x.__p_.__areas_, __areas);
0310   }
0311   return __is;
0312 }
0313 
0314 _LIBCPP_END_NAMESPACE_STD
0315 
0316 _LIBCPP_POP_MACROS
0317 
0318 #endif // _LIBCPP___RANDOM_PIECEWISE_LINEAR_DISTRIBUTION_H