random 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788
  1. // Random number extensions -*- C++ -*-
  2. // Copyright (C) 2012-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/random
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _EXT_RANDOM
  24. #define _EXT_RANDOM 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <random>
  30. #include <algorithm>
  31. #include <array>
  32. #include <ext/cmath>
  33. #ifdef __SSE2__
  34. # include <emmintrin.h>
  35. #endif
  36. #if defined(_GLIBCXX_USE_C99_STDINT_TR1) && defined(UINT32_C)
  37. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  41. /* Mersenne twister implementation optimized for vector operations.
  42. *
  43. * Reference: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/
  44. */
  45. template<typename _UIntType, size_t __m,
  46. size_t __pos1, size_t __sl1, size_t __sl2,
  47. size_t __sr1, size_t __sr2,
  48. uint32_t __msk1, uint32_t __msk2,
  49. uint32_t __msk3, uint32_t __msk4,
  50. uint32_t __parity1, uint32_t __parity2,
  51. uint32_t __parity3, uint32_t __parity4>
  52. class simd_fast_mersenne_twister_engine
  53. {
  54. static_assert(std::is_unsigned<_UIntType>::value, "template argument "
  55. "substituting _UIntType not an unsigned integral type");
  56. static_assert(__sr1 < 32, "first right shift too large");
  57. static_assert(__sr2 < 16, "second right shift too large");
  58. static_assert(__sl1 < 32, "first left shift too large");
  59. static_assert(__sl2 < 16, "second left shift too large");
  60. public:
  61. typedef _UIntType result_type;
  62. private:
  63. static constexpr size_t m_w = sizeof(result_type) * 8;
  64. static constexpr size_t _M_nstate = __m / 128 + 1;
  65. static constexpr size_t _M_nstate32 = _M_nstate * 4;
  66. static_assert(std::is_unsigned<_UIntType>::value, "template argument "
  67. "substituting _UIntType not an unsigned integral type");
  68. static_assert(__pos1 < _M_nstate, "POS1 not smaller than state size");
  69. static_assert(16 % sizeof(_UIntType) == 0,
  70. "UIntType size must divide 16");
  71. public:
  72. static constexpr size_t state_size = _M_nstate * (16
  73. / sizeof(result_type));
  74. static constexpr result_type default_seed = 5489u;
  75. // constructors and member function
  76. explicit
  77. simd_fast_mersenne_twister_engine(result_type __sd = default_seed)
  78. { seed(__sd); }
  79. template<typename _Sseq, typename = typename
  80. std::enable_if<!std::is_same<_Sseq,
  81. simd_fast_mersenne_twister_engine>::value>
  82. ::type>
  83. explicit
  84. simd_fast_mersenne_twister_engine(_Sseq& __q)
  85. { seed(__q); }
  86. void
  87. seed(result_type __sd = default_seed);
  88. template<typename _Sseq>
  89. typename std::enable_if<std::is_class<_Sseq>::value>::type
  90. seed(_Sseq& __q);
  91. static constexpr result_type
  92. min()
  93. { return 0; }
  94. static constexpr result_type
  95. max()
  96. { return std::numeric_limits<result_type>::max(); }
  97. void
  98. discard(unsigned long long __z);
  99. result_type
  100. operator()()
  101. {
  102. if (__builtin_expect(_M_pos >= state_size, 0))
  103. _M_gen_rand();
  104. return _M_stateT[_M_pos++];
  105. }
  106. template<typename _UIntType_2, size_t __m_2,
  107. size_t __pos1_2, size_t __sl1_2, size_t __sl2_2,
  108. size_t __sr1_2, size_t __sr2_2,
  109. uint32_t __msk1_2, uint32_t __msk2_2,
  110. uint32_t __msk3_2, uint32_t __msk4_2,
  111. uint32_t __parity1_2, uint32_t __parity2_2,
  112. uint32_t __parity3_2, uint32_t __parity4_2>
  113. friend bool
  114. operator==(const simd_fast_mersenne_twister_engine<_UIntType_2,
  115. __m_2, __pos1_2, __sl1_2, __sl2_2, __sr1_2, __sr2_2,
  116. __msk1_2, __msk2_2, __msk3_2, __msk4_2,
  117. __parity1_2, __parity2_2, __parity3_2, __parity4_2>& __lhs,
  118. const simd_fast_mersenne_twister_engine<_UIntType_2,
  119. __m_2, __pos1_2, __sl1_2, __sl2_2, __sr1_2, __sr2_2,
  120. __msk1_2, __msk2_2, __msk3_2, __msk4_2,
  121. __parity1_2, __parity2_2, __parity3_2, __parity4_2>& __rhs);
  122. template<typename _UIntType_2, size_t __m_2,
  123. size_t __pos1_2, size_t __sl1_2, size_t __sl2_2,
  124. size_t __sr1_2, size_t __sr2_2,
  125. uint32_t __msk1_2, uint32_t __msk2_2,
  126. uint32_t __msk3_2, uint32_t __msk4_2,
  127. uint32_t __parity1_2, uint32_t __parity2_2,
  128. uint32_t __parity3_2, uint32_t __parity4_2,
  129. typename _CharT, typename _Traits>
  130. friend std::basic_ostream<_CharT, _Traits>&
  131. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  132. const __gnu_cxx::simd_fast_mersenne_twister_engine
  133. <_UIntType_2,
  134. __m_2, __pos1_2, __sl1_2, __sl2_2, __sr1_2, __sr2_2,
  135. __msk1_2, __msk2_2, __msk3_2, __msk4_2,
  136. __parity1_2, __parity2_2, __parity3_2, __parity4_2>& __x);
  137. template<typename _UIntType_2, size_t __m_2,
  138. size_t __pos1_2, size_t __sl1_2, size_t __sl2_2,
  139. size_t __sr1_2, size_t __sr2_2,
  140. uint32_t __msk1_2, uint32_t __msk2_2,
  141. uint32_t __msk3_2, uint32_t __msk4_2,
  142. uint32_t __parity1_2, uint32_t __parity2_2,
  143. uint32_t __parity3_2, uint32_t __parity4_2,
  144. typename _CharT, typename _Traits>
  145. friend std::basic_istream<_CharT, _Traits>&
  146. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  147. __gnu_cxx::simd_fast_mersenne_twister_engine<_UIntType_2,
  148. __m_2, __pos1_2, __sl1_2, __sl2_2, __sr1_2, __sr2_2,
  149. __msk1_2, __msk2_2, __msk3_2, __msk4_2,
  150. __parity1_2, __parity2_2, __parity3_2, __parity4_2>& __x);
  151. private:
  152. union
  153. {
  154. #ifdef __SSE2__
  155. __m128i _M_state[_M_nstate];
  156. #endif
  157. #ifdef __ARM_NEON
  158. #ifdef __aarch64__
  159. __Uint32x4_t _M_state[_M_nstate];
  160. #endif
  161. #endif
  162. uint32_t _M_state32[_M_nstate32];
  163. result_type _M_stateT[state_size];
  164. } __attribute__ ((__aligned__ (16)));
  165. size_t _M_pos;
  166. void _M_gen_rand(void);
  167. void _M_period_certification();
  168. };
  169. template<typename _UIntType, size_t __m,
  170. size_t __pos1, size_t __sl1, size_t __sl2,
  171. size_t __sr1, size_t __sr2,
  172. uint32_t __msk1, uint32_t __msk2,
  173. uint32_t __msk3, uint32_t __msk4,
  174. uint32_t __parity1, uint32_t __parity2,
  175. uint32_t __parity3, uint32_t __parity4>
  176. inline bool
  177. operator!=(const __gnu_cxx::simd_fast_mersenne_twister_engine<_UIntType,
  178. __m, __pos1, __sl1, __sl2, __sr1, __sr2, __msk1, __msk2, __msk3,
  179. __msk4, __parity1, __parity2, __parity3, __parity4>& __lhs,
  180. const __gnu_cxx::simd_fast_mersenne_twister_engine<_UIntType,
  181. __m, __pos1, __sl1, __sl2, __sr1, __sr2, __msk1, __msk2, __msk3,
  182. __msk4, __parity1, __parity2, __parity3, __parity4>& __rhs)
  183. { return !(__lhs == __rhs); }
  184. /* Definitions for the SIMD-oriented Fast Mersenne Twister as defined
  185. * in the C implementation by Daito and Matsumoto, as both a 32-bit
  186. * and 64-bit version.
  187. */
  188. typedef simd_fast_mersenne_twister_engine<uint32_t, 607, 2,
  189. 15, 3, 13, 3,
  190. 0xfdff37ffU, 0xef7f3f7dU,
  191. 0xff777b7dU, 0x7ff7fb2fU,
  192. 0x00000001U, 0x00000000U,
  193. 0x00000000U, 0x5986f054U>
  194. sfmt607;
  195. typedef simd_fast_mersenne_twister_engine<uint64_t, 607, 2,
  196. 15, 3, 13, 3,
  197. 0xfdff37ffU, 0xef7f3f7dU,
  198. 0xff777b7dU, 0x7ff7fb2fU,
  199. 0x00000001U, 0x00000000U,
  200. 0x00000000U, 0x5986f054U>
  201. sfmt607_64;
  202. typedef simd_fast_mersenne_twister_engine<uint32_t, 1279, 7,
  203. 14, 3, 5, 1,
  204. 0xf7fefffdU, 0x7fefcfffU,
  205. 0xaff3ef3fU, 0xb5ffff7fU,
  206. 0x00000001U, 0x00000000U,
  207. 0x00000000U, 0x20000000U>
  208. sfmt1279;
  209. typedef simd_fast_mersenne_twister_engine<uint64_t, 1279, 7,
  210. 14, 3, 5, 1,
  211. 0xf7fefffdU, 0x7fefcfffU,
  212. 0xaff3ef3fU, 0xb5ffff7fU,
  213. 0x00000001U, 0x00000000U,
  214. 0x00000000U, 0x20000000U>
  215. sfmt1279_64;
  216. typedef simd_fast_mersenne_twister_engine<uint32_t, 2281, 12,
  217. 19, 1, 5, 1,
  218. 0xbff7ffbfU, 0xfdfffffeU,
  219. 0xf7ffef7fU, 0xf2f7cbbfU,
  220. 0x00000001U, 0x00000000U,
  221. 0x00000000U, 0x41dfa600U>
  222. sfmt2281;
  223. typedef simd_fast_mersenne_twister_engine<uint64_t, 2281, 12,
  224. 19, 1, 5, 1,
  225. 0xbff7ffbfU, 0xfdfffffeU,
  226. 0xf7ffef7fU, 0xf2f7cbbfU,
  227. 0x00000001U, 0x00000000U,
  228. 0x00000000U, 0x41dfa600U>
  229. sfmt2281_64;
  230. typedef simd_fast_mersenne_twister_engine<uint32_t, 4253, 17,
  231. 20, 1, 7, 1,
  232. 0x9f7bffffU, 0x9fffff5fU,
  233. 0x3efffffbU, 0xfffff7bbU,
  234. 0xa8000001U, 0xaf5390a3U,
  235. 0xb740b3f8U, 0x6c11486dU>
  236. sfmt4253;
  237. typedef simd_fast_mersenne_twister_engine<uint64_t, 4253, 17,
  238. 20, 1, 7, 1,
  239. 0x9f7bffffU, 0x9fffff5fU,
  240. 0x3efffffbU, 0xfffff7bbU,
  241. 0xa8000001U, 0xaf5390a3U,
  242. 0xb740b3f8U, 0x6c11486dU>
  243. sfmt4253_64;
  244. typedef simd_fast_mersenne_twister_engine<uint32_t, 11213, 68,
  245. 14, 3, 7, 3,
  246. 0xeffff7fbU, 0xffffffefU,
  247. 0xdfdfbfffU, 0x7fffdbfdU,
  248. 0x00000001U, 0x00000000U,
  249. 0xe8148000U, 0xd0c7afa3U>
  250. sfmt11213;
  251. typedef simd_fast_mersenne_twister_engine<uint64_t, 11213, 68,
  252. 14, 3, 7, 3,
  253. 0xeffff7fbU, 0xffffffefU,
  254. 0xdfdfbfffU, 0x7fffdbfdU,
  255. 0x00000001U, 0x00000000U,
  256. 0xe8148000U, 0xd0c7afa3U>
  257. sfmt11213_64;
  258. typedef simd_fast_mersenne_twister_engine<uint32_t, 19937, 122,
  259. 18, 1, 11, 1,
  260. 0xdfffffefU, 0xddfecb7fU,
  261. 0xbffaffffU, 0xbffffff6U,
  262. 0x00000001U, 0x00000000U,
  263. 0x00000000U, 0x13c9e684U>
  264. sfmt19937;
  265. typedef simd_fast_mersenne_twister_engine<uint64_t, 19937, 122,
  266. 18, 1, 11, 1,
  267. 0xdfffffefU, 0xddfecb7fU,
  268. 0xbffaffffU, 0xbffffff6U,
  269. 0x00000001U, 0x00000000U,
  270. 0x00000000U, 0x13c9e684U>
  271. sfmt19937_64;
  272. typedef simd_fast_mersenne_twister_engine<uint32_t, 44497, 330,
  273. 5, 3, 9, 3,
  274. 0xeffffffbU, 0xdfbebfffU,
  275. 0xbfbf7befU, 0x9ffd7bffU,
  276. 0x00000001U, 0x00000000U,
  277. 0xa3ac4000U, 0xecc1327aU>
  278. sfmt44497;
  279. typedef simd_fast_mersenne_twister_engine<uint64_t, 44497, 330,
  280. 5, 3, 9, 3,
  281. 0xeffffffbU, 0xdfbebfffU,
  282. 0xbfbf7befU, 0x9ffd7bffU,
  283. 0x00000001U, 0x00000000U,
  284. 0xa3ac4000U, 0xecc1327aU>
  285. sfmt44497_64;
  286. typedef simd_fast_mersenne_twister_engine<uint32_t, 86243, 366,
  287. 6, 7, 19, 1,
  288. 0xfdbffbffU, 0xbff7ff3fU,
  289. 0xfd77efffU, 0xbf9ff3ffU,
  290. 0x00000001U, 0x00000000U,
  291. 0x00000000U, 0xe9528d85U>
  292. sfmt86243;
  293. typedef simd_fast_mersenne_twister_engine<uint64_t, 86243, 366,
  294. 6, 7, 19, 1,
  295. 0xfdbffbffU, 0xbff7ff3fU,
  296. 0xfd77efffU, 0xbf9ff3ffU,
  297. 0x00000001U, 0x00000000U,
  298. 0x00000000U, 0xe9528d85U>
  299. sfmt86243_64;
  300. typedef simd_fast_mersenne_twister_engine<uint32_t, 132049, 110,
  301. 19, 1, 21, 1,
  302. 0xffffbb5fU, 0xfb6ebf95U,
  303. 0xfffefffaU, 0xcff77fffU,
  304. 0x00000001U, 0x00000000U,
  305. 0xcb520000U, 0xc7e91c7dU>
  306. sfmt132049;
  307. typedef simd_fast_mersenne_twister_engine<uint64_t, 132049, 110,
  308. 19, 1, 21, 1,
  309. 0xffffbb5fU, 0xfb6ebf95U,
  310. 0xfffefffaU, 0xcff77fffU,
  311. 0x00000001U, 0x00000000U,
  312. 0xcb520000U, 0xc7e91c7dU>
  313. sfmt132049_64;
  314. typedef simd_fast_mersenne_twister_engine<uint32_t, 216091, 627,
  315. 11, 3, 10, 1,
  316. 0xbff7bff7U, 0xbfffffffU,
  317. 0xbffffa7fU, 0xffddfbfbU,
  318. 0xf8000001U, 0x89e80709U,
  319. 0x3bd2b64bU, 0x0c64b1e4U>
  320. sfmt216091;
  321. typedef simd_fast_mersenne_twister_engine<uint64_t, 216091, 627,
  322. 11, 3, 10, 1,
  323. 0xbff7bff7U, 0xbfffffffU,
  324. 0xbffffa7fU, 0xffddfbfbU,
  325. 0xf8000001U, 0x89e80709U,
  326. 0x3bd2b64bU, 0x0c64b1e4U>
  327. sfmt216091_64;
  328. #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  329. /**
  330. * @brief A beta continuous distribution for random numbers.
  331. *
  332. * The formula for the beta probability density function is:
  333. * @f[
  334. * p(x|\alpha,\beta) = \frac{1}{B(\alpha,\beta)}
  335. * x^{\alpha - 1} (1 - x)^{\beta - 1}
  336. * @f]
  337. */
  338. template<typename _RealType = double>
  339. class beta_distribution
  340. {
  341. static_assert(std::is_floating_point<_RealType>::value,
  342. "template argument not a floating point type");
  343. public:
  344. /** The type of the range of the distribution. */
  345. typedef _RealType result_type;
  346. /** Parameter type. */
  347. struct param_type
  348. {
  349. typedef beta_distribution<_RealType> distribution_type;
  350. friend class beta_distribution<_RealType>;
  351. explicit
  352. param_type(_RealType __alpha_val = _RealType(1),
  353. _RealType __beta_val = _RealType(1))
  354. : _M_alpha(__alpha_val), _M_beta(__beta_val)
  355. {
  356. __glibcxx_assert(_M_alpha > _RealType(0));
  357. __glibcxx_assert(_M_beta > _RealType(0));
  358. }
  359. _RealType
  360. alpha() const
  361. { return _M_alpha; }
  362. _RealType
  363. beta() const
  364. { return _M_beta; }
  365. friend bool
  366. operator==(const param_type& __p1, const param_type& __p2)
  367. { return (__p1._M_alpha == __p2._M_alpha
  368. && __p1._M_beta == __p2._M_beta); }
  369. friend bool
  370. operator!=(const param_type& __p1, const param_type& __p2)
  371. { return !(__p1 == __p2); }
  372. private:
  373. void
  374. _M_initialize();
  375. _RealType _M_alpha;
  376. _RealType _M_beta;
  377. };
  378. public:
  379. /**
  380. * @brief Constructs a beta distribution with parameters
  381. * @f$\alpha@f$ and @f$\beta@f$.
  382. */
  383. explicit
  384. beta_distribution(_RealType __alpha_val = _RealType(1),
  385. _RealType __beta_val = _RealType(1))
  386. : _M_param(__alpha_val, __beta_val)
  387. { }
  388. explicit
  389. beta_distribution(const param_type& __p)
  390. : _M_param(__p)
  391. { }
  392. /**
  393. * @brief Resets the distribution state.
  394. */
  395. void
  396. reset()
  397. { }
  398. /**
  399. * @brief Returns the @f$\alpha@f$ of the distribution.
  400. */
  401. _RealType
  402. alpha() const
  403. { return _M_param.alpha(); }
  404. /**
  405. * @brief Returns the @f$\beta@f$ of the distribution.
  406. */
  407. _RealType
  408. beta() const
  409. { return _M_param.beta(); }
  410. /**
  411. * @brief Returns the parameter set of the distribution.
  412. */
  413. param_type
  414. param() const
  415. { return _M_param; }
  416. /**
  417. * @brief Sets the parameter set of the distribution.
  418. * @param __param The new parameter set of the distribution.
  419. */
  420. void
  421. param(const param_type& __param)
  422. { _M_param = __param; }
  423. /**
  424. * @brief Returns the greatest lower bound value of the distribution.
  425. */
  426. result_type
  427. min() const
  428. { return result_type(0); }
  429. /**
  430. * @brief Returns the least upper bound value of the distribution.
  431. */
  432. result_type
  433. max() const
  434. { return result_type(1); }
  435. /**
  436. * @brief Generating functions.
  437. */
  438. template<typename _UniformRandomNumberGenerator>
  439. result_type
  440. operator()(_UniformRandomNumberGenerator& __urng)
  441. { return this->operator()(__urng, _M_param); }
  442. template<typename _UniformRandomNumberGenerator>
  443. result_type
  444. operator()(_UniformRandomNumberGenerator& __urng,
  445. const param_type& __p);
  446. template<typename _ForwardIterator,
  447. typename _UniformRandomNumberGenerator>
  448. void
  449. __generate(_ForwardIterator __f, _ForwardIterator __t,
  450. _UniformRandomNumberGenerator& __urng)
  451. { this->__generate(__f, __t, __urng, _M_param); }
  452. template<typename _ForwardIterator,
  453. typename _UniformRandomNumberGenerator>
  454. void
  455. __generate(_ForwardIterator __f, _ForwardIterator __t,
  456. _UniformRandomNumberGenerator& __urng,
  457. const param_type& __p)
  458. { this->__generate_impl(__f, __t, __urng, __p); }
  459. template<typename _UniformRandomNumberGenerator>
  460. void
  461. __generate(result_type* __f, result_type* __t,
  462. _UniformRandomNumberGenerator& __urng,
  463. const param_type& __p)
  464. { this->__generate_impl(__f, __t, __urng, __p); }
  465. /**
  466. * @brief Return true if two beta distributions have the same
  467. * parameters and the sequences that would be generated
  468. * are equal.
  469. */
  470. friend bool
  471. operator==(const beta_distribution& __d1,
  472. const beta_distribution& __d2)
  473. { return __d1._M_param == __d2._M_param; }
  474. /**
  475. * @brief Inserts a %beta_distribution random number distribution
  476. * @p __x into the output stream @p __os.
  477. *
  478. * @param __os An output stream.
  479. * @param __x A %beta_distribution random number distribution.
  480. *
  481. * @returns The output stream with the state of @p __x inserted or in
  482. * an error state.
  483. */
  484. template<typename _RealType1, typename _CharT, typename _Traits>
  485. friend std::basic_ostream<_CharT, _Traits>&
  486. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  487. const __gnu_cxx::beta_distribution<_RealType1>& __x);
  488. /**
  489. * @brief Extracts a %beta_distribution random number distribution
  490. * @p __x from the input stream @p __is.
  491. *
  492. * @param __is An input stream.
  493. * @param __x A %beta_distribution random number generator engine.
  494. *
  495. * @returns The input stream with @p __x extracted or in an error state.
  496. */
  497. template<typename _RealType1, typename _CharT, typename _Traits>
  498. friend std::basic_istream<_CharT, _Traits>&
  499. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  500. __gnu_cxx::beta_distribution<_RealType1>& __x);
  501. private:
  502. template<typename _ForwardIterator,
  503. typename _UniformRandomNumberGenerator>
  504. void
  505. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  506. _UniformRandomNumberGenerator& __urng,
  507. const param_type& __p);
  508. param_type _M_param;
  509. };
  510. /**
  511. * @brief Return true if two beta distributions are different.
  512. */
  513. template<typename _RealType>
  514. inline bool
  515. operator!=(const __gnu_cxx::beta_distribution<_RealType>& __d1,
  516. const __gnu_cxx::beta_distribution<_RealType>& __d2)
  517. { return !(__d1 == __d2); }
  518. /**
  519. * @brief A multi-variate normal continuous distribution for random numbers.
  520. *
  521. * The formula for the normal probability density function is
  522. * @f[
  523. * p(\overrightarrow{x}|\overrightarrow{\mu },\Sigma) =
  524. * \frac{1}{\sqrt{(2\pi )^k\det(\Sigma))}}
  525. * e^{-\frac{1}{2}(\overrightarrow{x}-\overrightarrow{\mu})^\text{T}
  526. * \Sigma ^{-1}(\overrightarrow{x}-\overrightarrow{\mu})}
  527. * @f]
  528. *
  529. * where @f$\overrightarrow{x}@f$ and @f$\overrightarrow{\mu}@f$ are
  530. * vectors of dimension @f$k@f$ and @f$\Sigma@f$ is the covariance
  531. * matrix (which must be positive-definite).
  532. */
  533. template<std::size_t _Dimen, typename _RealType = double>
  534. class normal_mv_distribution
  535. {
  536. static_assert(std::is_floating_point<_RealType>::value,
  537. "template argument not a floating point type");
  538. static_assert(_Dimen != 0, "dimension is zero");
  539. public:
  540. /** The type of the range of the distribution. */
  541. typedef std::array<_RealType, _Dimen> result_type;
  542. /** Parameter type. */
  543. class param_type
  544. {
  545. static constexpr size_t _M_t_size = _Dimen * (_Dimen + 1) / 2;
  546. public:
  547. typedef normal_mv_distribution<_Dimen, _RealType> distribution_type;
  548. friend class normal_mv_distribution<_Dimen, _RealType>;
  549. param_type()
  550. {
  551. std::fill(_M_mean.begin(), _M_mean.end(), _RealType(0));
  552. auto __it = _M_t.begin();
  553. for (size_t __i = 0; __i < _Dimen; ++__i)
  554. {
  555. std::fill_n(__it, __i, _RealType(0));
  556. __it += __i;
  557. *__it++ = _RealType(1);
  558. }
  559. }
  560. template<typename _ForwardIterator1, typename _ForwardIterator2>
  561. param_type(_ForwardIterator1 __meanbegin,
  562. _ForwardIterator1 __meanend,
  563. _ForwardIterator2 __varcovbegin,
  564. _ForwardIterator2 __varcovend)
  565. {
  566. __glibcxx_function_requires(_ForwardIteratorConcept<
  567. _ForwardIterator1>)
  568. __glibcxx_function_requires(_ForwardIteratorConcept<
  569. _ForwardIterator2>)
  570. _GLIBCXX_DEBUG_ASSERT(std::distance(__meanbegin, __meanend)
  571. <= _Dimen);
  572. const auto __dist = std::distance(__varcovbegin, __varcovend);
  573. _GLIBCXX_DEBUG_ASSERT(__dist == _Dimen * _Dimen
  574. || __dist == _Dimen * (_Dimen + 1) / 2
  575. || __dist == _Dimen);
  576. if (__dist == _Dimen * _Dimen)
  577. _M_init_full(__meanbegin, __meanend, __varcovbegin, __varcovend);
  578. else if (__dist == _Dimen * (_Dimen + 1) / 2)
  579. _M_init_lower(__meanbegin, __meanend, __varcovbegin, __varcovend);
  580. else
  581. {
  582. __glibcxx_assert(__dist == _Dimen);
  583. _M_init_diagonal(__meanbegin, __meanend,
  584. __varcovbegin, __varcovend);
  585. }
  586. }
  587. param_type(std::initializer_list<_RealType> __mean,
  588. std::initializer_list<_RealType> __varcov)
  589. {
  590. _GLIBCXX_DEBUG_ASSERT(__mean.size() <= _Dimen);
  591. _GLIBCXX_DEBUG_ASSERT(__varcov.size() == _Dimen * _Dimen
  592. || __varcov.size() == _Dimen * (_Dimen + 1) / 2
  593. || __varcov.size() == _Dimen);
  594. if (__varcov.size() == _Dimen * _Dimen)
  595. _M_init_full(__mean.begin(), __mean.end(),
  596. __varcov.begin(), __varcov.end());
  597. else if (__varcov.size() == _Dimen * (_Dimen + 1) / 2)
  598. _M_init_lower(__mean.begin(), __mean.end(),
  599. __varcov.begin(), __varcov.end());
  600. else
  601. {
  602. __glibcxx_assert(__varcov.size() == _Dimen);
  603. _M_init_diagonal(__mean.begin(), __mean.end(),
  604. __varcov.begin(), __varcov.end());
  605. }
  606. }
  607. std::array<_RealType, _Dimen>
  608. mean() const
  609. { return _M_mean; }
  610. std::array<_RealType, _M_t_size>
  611. varcov() const
  612. { return _M_t; }
  613. friend bool
  614. operator==(const param_type& __p1, const param_type& __p2)
  615. { return __p1._M_mean == __p2._M_mean && __p1._M_t == __p2._M_t; }
  616. friend bool
  617. operator!=(const param_type& __p1, const param_type& __p2)
  618. { return !(__p1 == __p2); }
  619. private:
  620. template <typename _InputIterator1, typename _InputIterator2>
  621. void _M_init_full(_InputIterator1 __meanbegin,
  622. _InputIterator1 __meanend,
  623. _InputIterator2 __varcovbegin,
  624. _InputIterator2 __varcovend);
  625. template <typename _InputIterator1, typename _InputIterator2>
  626. void _M_init_lower(_InputIterator1 __meanbegin,
  627. _InputIterator1 __meanend,
  628. _InputIterator2 __varcovbegin,
  629. _InputIterator2 __varcovend);
  630. template <typename _InputIterator1, typename _InputIterator2>
  631. void _M_init_diagonal(_InputIterator1 __meanbegin,
  632. _InputIterator1 __meanend,
  633. _InputIterator2 __varbegin,
  634. _InputIterator2 __varend);
  635. std::array<_RealType, _Dimen> _M_mean;
  636. std::array<_RealType, _M_t_size> _M_t;
  637. };
  638. public:
  639. normal_mv_distribution()
  640. : _M_param(), _M_nd()
  641. { }
  642. template<typename _ForwardIterator1, typename _ForwardIterator2>
  643. normal_mv_distribution(_ForwardIterator1 __meanbegin,
  644. _ForwardIterator1 __meanend,
  645. _ForwardIterator2 __varcovbegin,
  646. _ForwardIterator2 __varcovend)
  647. : _M_param(__meanbegin, __meanend, __varcovbegin, __varcovend),
  648. _M_nd()
  649. { }
  650. normal_mv_distribution(std::initializer_list<_RealType> __mean,
  651. std::initializer_list<_RealType> __varcov)
  652. : _M_param(__mean, __varcov), _M_nd()
  653. { }
  654. explicit
  655. normal_mv_distribution(const param_type& __p)
  656. : _M_param(__p), _M_nd()
  657. { }
  658. /**
  659. * @brief Resets the distribution state.
  660. */
  661. void
  662. reset()
  663. { _M_nd.reset(); }
  664. /**
  665. * @brief Returns the mean of the distribution.
  666. */
  667. result_type
  668. mean() const
  669. { return _M_param.mean(); }
  670. /**
  671. * @brief Returns the compact form of the variance/covariance
  672. * matrix of the distribution.
  673. */
  674. std::array<_RealType, _Dimen * (_Dimen + 1) / 2>
  675. varcov() const
  676. { return _M_param.varcov(); }
  677. /**
  678. * @brief Returns the parameter set of the distribution.
  679. */
  680. param_type
  681. param() const
  682. { return _M_param; }
  683. /**
  684. * @brief Sets the parameter set of the distribution.
  685. * @param __param The new parameter set of the distribution.
  686. */
  687. void
  688. param(const param_type& __param)
  689. { _M_param = __param; }
  690. /**
  691. * @brief Returns the greatest lower bound value of the distribution.
  692. */
  693. result_type
  694. min() const
  695. { result_type __res;
  696. __res.fill(std::numeric_limits<_RealType>::lowest());
  697. return __res; }
  698. /**
  699. * @brief Returns the least upper bound value of the distribution.
  700. */
  701. result_type
  702. max() const
  703. { result_type __res;
  704. __res.fill(std::numeric_limits<_RealType>::max());
  705. return __res; }
  706. /**
  707. * @brief Generating functions.
  708. */
  709. template<typename _UniformRandomNumberGenerator>
  710. result_type
  711. operator()(_UniformRandomNumberGenerator& __urng)
  712. { return this->operator()(__urng, _M_param); }
  713. template<typename _UniformRandomNumberGenerator>
  714. result_type
  715. operator()(_UniformRandomNumberGenerator& __urng,
  716. const param_type& __p);
  717. template<typename _ForwardIterator,
  718. typename _UniformRandomNumberGenerator>
  719. void
  720. __generate(_ForwardIterator __f, _ForwardIterator __t,
  721. _UniformRandomNumberGenerator& __urng)
  722. { return this->__generate_impl(__f, __t, __urng, _M_param); }
  723. template<typename _ForwardIterator,
  724. typename _UniformRandomNumberGenerator>
  725. void
  726. __generate(_ForwardIterator __f, _ForwardIterator __t,
  727. _UniformRandomNumberGenerator& __urng,
  728. const param_type& __p)
  729. { return this->__generate_impl(__f, __t, __urng, __p); }
  730. /**
  731. * @brief Return true if two multi-variant normal distributions have
  732. * the same parameters and the sequences that would
  733. * be generated are equal.
  734. */
  735. template<size_t _Dimen1, typename _RealType1>
  736. friend bool
  737. operator==(const
  738. __gnu_cxx::normal_mv_distribution<_Dimen1, _RealType1>&
  739. __d1,
  740. const
  741. __gnu_cxx::normal_mv_distribution<_Dimen1, _RealType1>&
  742. __d2);
  743. /**
  744. * @brief Inserts a %normal_mv_distribution random number distribution
  745. * @p __x into the output stream @p __os.
  746. *
  747. * @param __os An output stream.
  748. * @param __x A %normal_mv_distribution random number distribution.
  749. *
  750. * @returns The output stream with the state of @p __x inserted or in
  751. * an error state.
  752. */
  753. template<size_t _Dimen1, typename _RealType1,
  754. typename _CharT, typename _Traits>
  755. friend std::basic_ostream<_CharT, _Traits>&
  756. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  757. const
  758. __gnu_cxx::normal_mv_distribution<_Dimen1, _RealType1>&
  759. __x);
  760. /**
  761. * @brief Extracts a %normal_mv_distribution random number distribution
  762. * @p __x from the input stream @p __is.
  763. *
  764. * @param __is An input stream.
  765. * @param __x A %normal_mv_distribution random number generator engine.
  766. *
  767. * @returns The input stream with @p __x extracted or in an error
  768. * state.
  769. */
  770. template<size_t _Dimen1, typename _RealType1,
  771. typename _CharT, typename _Traits>
  772. friend std::basic_istream<_CharT, _Traits>&
  773. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  774. __gnu_cxx::normal_mv_distribution<_Dimen1, _RealType1>&
  775. __x);
  776. private:
  777. template<typename _ForwardIterator,
  778. typename _UniformRandomNumberGenerator>
  779. void
  780. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  781. _UniformRandomNumberGenerator& __urng,
  782. const param_type& __p);
  783. param_type _M_param;
  784. std::normal_distribution<_RealType> _M_nd;
  785. };
  786. /**
  787. * @brief Return true if two multi-variate normal distributions are
  788. * different.
  789. */
  790. template<size_t _Dimen, typename _RealType>
  791. inline bool
  792. operator!=(const __gnu_cxx::normal_mv_distribution<_Dimen, _RealType>&
  793. __d1,
  794. const __gnu_cxx::normal_mv_distribution<_Dimen, _RealType>&
  795. __d2)
  796. { return !(__d1 == __d2); }
  797. /**
  798. * @brief A Rice continuous distribution for random numbers.
  799. *
  800. * The formula for the Rice probability density function is
  801. * @f[
  802. * p(x|\nu,\sigma) = \frac{x}{\sigma^2}
  803. * \exp\left(-\frac{x^2+\nu^2}{2\sigma^2}\right)
  804. * I_0\left(\frac{x \nu}{\sigma^2}\right)
  805. * @f]
  806. * where @f$I_0(z)@f$ is the modified Bessel function of the first kind
  807. * of order 0 and @f$\nu >= 0@f$ and @f$\sigma > 0@f$.
  808. *
  809. * <table border=1 cellpadding=10 cellspacing=0>
  810. * <caption align=top>Distribution Statistics</caption>
  811. * <tr><td>Mean</td><td>@f$\sqrt{\pi/2}L_{1/2}(-\nu^2/2\sigma^2)@f$</td></tr>
  812. * <tr><td>Variance</td><td>@f$2\sigma^2 + \nu^2
  813. * + (\pi\sigma^2/2)L^2_{1/2}(-\nu^2/2\sigma^2)@f$</td></tr>
  814. * <tr><td>Range</td><td>@f$[0, \infty)@f$</td></tr>
  815. * </table>
  816. * where @f$L_{1/2}(x)@f$ is the Laguerre polynomial of order 1/2.
  817. */
  818. template<typename _RealType = double>
  819. class
  820. rice_distribution
  821. {
  822. static_assert(std::is_floating_point<_RealType>::value,
  823. "template argument not a floating point type");
  824. public:
  825. /** The type of the range of the distribution. */
  826. typedef _RealType result_type;
  827. /** Parameter type. */
  828. struct param_type
  829. {
  830. typedef rice_distribution<result_type> distribution_type;
  831. param_type(result_type __nu_val = result_type(0),
  832. result_type __sigma_val = result_type(1))
  833. : _M_nu(__nu_val), _M_sigma(__sigma_val)
  834. {
  835. __glibcxx_assert(_M_nu >= result_type(0));
  836. __glibcxx_assert(_M_sigma > result_type(0));
  837. }
  838. result_type
  839. nu() const
  840. { return _M_nu; }
  841. result_type
  842. sigma() const
  843. { return _M_sigma; }
  844. friend bool
  845. operator==(const param_type& __p1, const param_type& __p2)
  846. { return __p1._M_nu == __p2._M_nu && __p1._M_sigma == __p2._M_sigma; }
  847. friend bool
  848. operator!=(const param_type& __p1, const param_type& __p2)
  849. { return !(__p1 == __p2); }
  850. private:
  851. void _M_initialize();
  852. result_type _M_nu;
  853. result_type _M_sigma;
  854. };
  855. /**
  856. * @brief Constructors.
  857. */
  858. explicit
  859. rice_distribution(result_type __nu_val = result_type(0),
  860. result_type __sigma_val = result_type(1))
  861. : _M_param(__nu_val, __sigma_val),
  862. _M_ndx(__nu_val, __sigma_val),
  863. _M_ndy(result_type(0), __sigma_val)
  864. { }
  865. explicit
  866. rice_distribution(const param_type& __p)
  867. : _M_param(__p),
  868. _M_ndx(__p.nu(), __p.sigma()),
  869. _M_ndy(result_type(0), __p.sigma())
  870. { }
  871. /**
  872. * @brief Resets the distribution state.
  873. */
  874. void
  875. reset()
  876. {
  877. _M_ndx.reset();
  878. _M_ndy.reset();
  879. }
  880. /**
  881. * @brief Return the parameters of the distribution.
  882. */
  883. result_type
  884. nu() const
  885. { return _M_param.nu(); }
  886. result_type
  887. sigma() const
  888. { return _M_param.sigma(); }
  889. /**
  890. * @brief Returns the parameter set of the distribution.
  891. */
  892. param_type
  893. param() const
  894. { return _M_param; }
  895. /**
  896. * @brief Sets the parameter set of the distribution.
  897. * @param __param The new parameter set of the distribution.
  898. */
  899. void
  900. param(const param_type& __param)
  901. { _M_param = __param; }
  902. /**
  903. * @brief Returns the greatest lower bound value of the distribution.
  904. */
  905. result_type
  906. min() const
  907. { return result_type(0); }
  908. /**
  909. * @brief Returns the least upper bound value of the distribution.
  910. */
  911. result_type
  912. max() const
  913. { return std::numeric_limits<result_type>::max(); }
  914. /**
  915. * @brief Generating functions.
  916. */
  917. template<typename _UniformRandomNumberGenerator>
  918. result_type
  919. operator()(_UniformRandomNumberGenerator& __urng)
  920. {
  921. result_type __x = this->_M_ndx(__urng);
  922. result_type __y = this->_M_ndy(__urng);
  923. #if _GLIBCXX_USE_C99_MATH_TR1
  924. return std::hypot(__x, __y);
  925. #else
  926. return std::sqrt(__x * __x + __y * __y);
  927. #endif
  928. }
  929. template<typename _UniformRandomNumberGenerator>
  930. result_type
  931. operator()(_UniformRandomNumberGenerator& __urng,
  932. const param_type& __p)
  933. {
  934. typename std::normal_distribution<result_type>::param_type
  935. __px(__p.nu(), __p.sigma()), __py(result_type(0), __p.sigma());
  936. result_type __x = this->_M_ndx(__px, __urng);
  937. result_type __y = this->_M_ndy(__py, __urng);
  938. #if _GLIBCXX_USE_C99_MATH_TR1
  939. return std::hypot(__x, __y);
  940. #else
  941. return std::sqrt(__x * __x + __y * __y);
  942. #endif
  943. }
  944. template<typename _ForwardIterator,
  945. typename _UniformRandomNumberGenerator>
  946. void
  947. __generate(_ForwardIterator __f, _ForwardIterator __t,
  948. _UniformRandomNumberGenerator& __urng)
  949. { this->__generate(__f, __t, __urng, _M_param); }
  950. template<typename _ForwardIterator,
  951. typename _UniformRandomNumberGenerator>
  952. void
  953. __generate(_ForwardIterator __f, _ForwardIterator __t,
  954. _UniformRandomNumberGenerator& __urng,
  955. const param_type& __p)
  956. { this->__generate_impl(__f, __t, __urng, __p); }
  957. template<typename _UniformRandomNumberGenerator>
  958. void
  959. __generate(result_type* __f, result_type* __t,
  960. _UniformRandomNumberGenerator& __urng,
  961. const param_type& __p)
  962. { this->__generate_impl(__f, __t, __urng, __p); }
  963. /**
  964. * @brief Return true if two Rice distributions have
  965. * the same parameters and the sequences that would
  966. * be generated are equal.
  967. */
  968. friend bool
  969. operator==(const rice_distribution& __d1,
  970. const rice_distribution& __d2)
  971. { return (__d1._M_param == __d2._M_param
  972. && __d1._M_ndx == __d2._M_ndx
  973. && __d1._M_ndy == __d2._M_ndy); }
  974. /**
  975. * @brief Inserts a %rice_distribution random number distribution
  976. * @p __x into the output stream @p __os.
  977. *
  978. * @param __os An output stream.
  979. * @param __x A %rice_distribution random number distribution.
  980. *
  981. * @returns The output stream with the state of @p __x inserted or in
  982. * an error state.
  983. */
  984. template<typename _RealType1, typename _CharT, typename _Traits>
  985. friend std::basic_ostream<_CharT, _Traits>&
  986. operator<<(std::basic_ostream<_CharT, _Traits>&,
  987. const rice_distribution<_RealType1>&);
  988. /**
  989. * @brief Extracts a %rice_distribution random number distribution
  990. * @p __x from the input stream @p __is.
  991. *
  992. * @param __is An input stream.
  993. * @param __x A %rice_distribution random number
  994. * generator engine.
  995. *
  996. * @returns The input stream with @p __x extracted or in an error state.
  997. */
  998. template<typename _RealType1, typename _CharT, typename _Traits>
  999. friend std::basic_istream<_CharT, _Traits>&
  1000. operator>>(std::basic_istream<_CharT, _Traits>&,
  1001. rice_distribution<_RealType1>&);
  1002. private:
  1003. template<typename _ForwardIterator,
  1004. typename _UniformRandomNumberGenerator>
  1005. void
  1006. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1007. _UniformRandomNumberGenerator& __urng,
  1008. const param_type& __p);
  1009. param_type _M_param;
  1010. std::normal_distribution<result_type> _M_ndx;
  1011. std::normal_distribution<result_type> _M_ndy;
  1012. };
  1013. /**
  1014. * @brief Return true if two Rice distributions are not equal.
  1015. */
  1016. template<typename _RealType1>
  1017. inline bool
  1018. operator!=(const rice_distribution<_RealType1>& __d1,
  1019. const rice_distribution<_RealType1>& __d2)
  1020. { return !(__d1 == __d2); }
  1021. /**
  1022. * @brief A Nakagami continuous distribution for random numbers.
  1023. *
  1024. * The formula for the Nakagami probability density function is
  1025. * @f[
  1026. * p(x|\mu,\omega) = \frac{2\mu^\mu}{\Gamma(\mu)\omega^\mu}
  1027. * x^{2\mu-1}e^{-\mu x / \omega}
  1028. * @f]
  1029. * where @f$\Gamma(z)@f$ is the gamma function and @f$\mu >= 0.5@f$
  1030. * and @f$\omega > 0@f$.
  1031. */
  1032. template<typename _RealType = double>
  1033. class
  1034. nakagami_distribution
  1035. {
  1036. static_assert(std::is_floating_point<_RealType>::value,
  1037. "template argument not a floating point type");
  1038. public:
  1039. /** The type of the range of the distribution. */
  1040. typedef _RealType result_type;
  1041. /** Parameter type. */
  1042. struct param_type
  1043. {
  1044. typedef nakagami_distribution<result_type> distribution_type;
  1045. param_type(result_type __mu_val = result_type(1),
  1046. result_type __omega_val = result_type(1))
  1047. : _M_mu(__mu_val), _M_omega(__omega_val)
  1048. {
  1049. __glibcxx_assert(_M_mu >= result_type(0.5L));
  1050. __glibcxx_assert(_M_omega > result_type(0));
  1051. }
  1052. result_type
  1053. mu() const
  1054. { return _M_mu; }
  1055. result_type
  1056. omega() const
  1057. { return _M_omega; }
  1058. friend bool
  1059. operator==(const param_type& __p1, const param_type& __p2)
  1060. { return __p1._M_mu == __p2._M_mu && __p1._M_omega == __p2._M_omega; }
  1061. friend bool
  1062. operator!=(const param_type& __p1, const param_type& __p2)
  1063. { return !(__p1 == __p2); }
  1064. private:
  1065. void _M_initialize();
  1066. result_type _M_mu;
  1067. result_type _M_omega;
  1068. };
  1069. /**
  1070. * @brief Constructors.
  1071. */
  1072. explicit
  1073. nakagami_distribution(result_type __mu_val = result_type(1),
  1074. result_type __omega_val = result_type(1))
  1075. : _M_param(__mu_val, __omega_val),
  1076. _M_gd(__mu_val, __omega_val / __mu_val)
  1077. { }
  1078. explicit
  1079. nakagami_distribution(const param_type& __p)
  1080. : _M_param(__p),
  1081. _M_gd(__p.mu(), __p.omega() / __p.mu())
  1082. { }
  1083. /**
  1084. * @brief Resets the distribution state.
  1085. */
  1086. void
  1087. reset()
  1088. { _M_gd.reset(); }
  1089. /**
  1090. * @brief Return the parameters of the distribution.
  1091. */
  1092. result_type
  1093. mu() const
  1094. { return _M_param.mu(); }
  1095. result_type
  1096. omega() const
  1097. { return _M_param.omega(); }
  1098. /**
  1099. * @brief Returns the parameter set of the distribution.
  1100. */
  1101. param_type
  1102. param() const
  1103. { return _M_param; }
  1104. /**
  1105. * @brief Sets the parameter set of the distribution.
  1106. * @param __param The new parameter set of the distribution.
  1107. */
  1108. void
  1109. param(const param_type& __param)
  1110. { _M_param = __param; }
  1111. /**
  1112. * @brief Returns the greatest lower bound value of the distribution.
  1113. */
  1114. result_type
  1115. min() const
  1116. { return result_type(0); }
  1117. /**
  1118. * @brief Returns the least upper bound value of the distribution.
  1119. */
  1120. result_type
  1121. max() const
  1122. { return std::numeric_limits<result_type>::max(); }
  1123. /**
  1124. * @brief Generating functions.
  1125. */
  1126. template<typename _UniformRandomNumberGenerator>
  1127. result_type
  1128. operator()(_UniformRandomNumberGenerator& __urng)
  1129. { return std::sqrt(this->_M_gd(__urng)); }
  1130. template<typename _UniformRandomNumberGenerator>
  1131. result_type
  1132. operator()(_UniformRandomNumberGenerator& __urng,
  1133. const param_type& __p)
  1134. {
  1135. typename std::gamma_distribution<result_type>::param_type
  1136. __pg(__p.mu(), __p.omega() / __p.mu());
  1137. return std::sqrt(this->_M_gd(__pg, __urng));
  1138. }
  1139. template<typename _ForwardIterator,
  1140. typename _UniformRandomNumberGenerator>
  1141. void
  1142. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1143. _UniformRandomNumberGenerator& __urng)
  1144. { this->__generate(__f, __t, __urng, _M_param); }
  1145. template<typename _ForwardIterator,
  1146. typename _UniformRandomNumberGenerator>
  1147. void
  1148. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1149. _UniformRandomNumberGenerator& __urng,
  1150. const param_type& __p)
  1151. { this->__generate_impl(__f, __t, __urng, __p); }
  1152. template<typename _UniformRandomNumberGenerator>
  1153. void
  1154. __generate(result_type* __f, result_type* __t,
  1155. _UniformRandomNumberGenerator& __urng,
  1156. const param_type& __p)
  1157. { this->__generate_impl(__f, __t, __urng, __p); }
  1158. /**
  1159. * @brief Return true if two Nakagami distributions have
  1160. * the same parameters and the sequences that would
  1161. * be generated are equal.
  1162. */
  1163. friend bool
  1164. operator==(const nakagami_distribution& __d1,
  1165. const nakagami_distribution& __d2)
  1166. { return (__d1._M_param == __d2._M_param
  1167. && __d1._M_gd == __d2._M_gd); }
  1168. /**
  1169. * @brief Inserts a %nakagami_distribution random number distribution
  1170. * @p __x into the output stream @p __os.
  1171. *
  1172. * @param __os An output stream.
  1173. * @param __x A %nakagami_distribution random number distribution.
  1174. *
  1175. * @returns The output stream with the state of @p __x inserted or in
  1176. * an error state.
  1177. */
  1178. template<typename _RealType1, typename _CharT, typename _Traits>
  1179. friend std::basic_ostream<_CharT, _Traits>&
  1180. operator<<(std::basic_ostream<_CharT, _Traits>&,
  1181. const nakagami_distribution<_RealType1>&);
  1182. /**
  1183. * @brief Extracts a %nakagami_distribution random number distribution
  1184. * @p __x from the input stream @p __is.
  1185. *
  1186. * @param __is An input stream.
  1187. * @param __x A %nakagami_distribution random number
  1188. * generator engine.
  1189. *
  1190. * @returns The input stream with @p __x extracted or in an error state.
  1191. */
  1192. template<typename _RealType1, typename _CharT, typename _Traits>
  1193. friend std::basic_istream<_CharT, _Traits>&
  1194. operator>>(std::basic_istream<_CharT, _Traits>&,
  1195. nakagami_distribution<_RealType1>&);
  1196. private:
  1197. template<typename _ForwardIterator,
  1198. typename _UniformRandomNumberGenerator>
  1199. void
  1200. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1201. _UniformRandomNumberGenerator& __urng,
  1202. const param_type& __p);
  1203. param_type _M_param;
  1204. std::gamma_distribution<result_type> _M_gd;
  1205. };
  1206. /**
  1207. * @brief Return true if two Nakagami distributions are not equal.
  1208. */
  1209. template<typename _RealType>
  1210. inline bool
  1211. operator!=(const nakagami_distribution<_RealType>& __d1,
  1212. const nakagami_distribution<_RealType>& __d2)
  1213. { return !(__d1 == __d2); }
  1214. /**
  1215. * @brief A Pareto continuous distribution for random numbers.
  1216. *
  1217. * The formula for the Pareto cumulative probability function is
  1218. * @f[
  1219. * P(x|\alpha,\mu) = 1 - \left(\frac{\mu}{x}\right)^\alpha
  1220. * @f]
  1221. * The formula for the Pareto probability density function is
  1222. * @f[
  1223. * p(x|\alpha,\mu) = \frac{\alpha + 1}{\mu}
  1224. * \left(\frac{\mu}{x}\right)^{\alpha + 1}
  1225. * @f]
  1226. * where @f$x >= \mu@f$ and @f$\mu > 0@f$, @f$\alpha > 0@f$.
  1227. *
  1228. * <table border=1 cellpadding=10 cellspacing=0>
  1229. * <caption align=top>Distribution Statistics</caption>
  1230. * <tr><td>Mean</td><td>@f$\alpha \mu / (\alpha - 1)@f$
  1231. * for @f$\alpha > 1@f$</td></tr>
  1232. * <tr><td>Variance</td><td>@f$\alpha \mu^2 / [(\alpha - 1)^2(\alpha - 2)]@f$
  1233. * for @f$\alpha > 2@f$</td></tr>
  1234. * <tr><td>Range</td><td>@f$[\mu, \infty)@f$</td></tr>
  1235. * </table>
  1236. */
  1237. template<typename _RealType = double>
  1238. class
  1239. pareto_distribution
  1240. {
  1241. static_assert(std::is_floating_point<_RealType>::value,
  1242. "template argument not a floating point type");
  1243. public:
  1244. /** The type of the range of the distribution. */
  1245. typedef _RealType result_type;
  1246. /** Parameter type. */
  1247. struct param_type
  1248. {
  1249. typedef pareto_distribution<result_type> distribution_type;
  1250. param_type(result_type __alpha_val = result_type(1),
  1251. result_type __mu_val = result_type(1))
  1252. : _M_alpha(__alpha_val), _M_mu(__mu_val)
  1253. {
  1254. __glibcxx_assert(_M_alpha > result_type(0));
  1255. __glibcxx_assert(_M_mu > result_type(0));
  1256. }
  1257. result_type
  1258. alpha() const
  1259. { return _M_alpha; }
  1260. result_type
  1261. mu() const
  1262. { return _M_mu; }
  1263. friend bool
  1264. operator==(const param_type& __p1, const param_type& __p2)
  1265. { return __p1._M_alpha == __p2._M_alpha && __p1._M_mu == __p2._M_mu; }
  1266. friend bool
  1267. operator!=(const param_type& __p1, const param_type& __p2)
  1268. { return !(__p1 == __p2); }
  1269. private:
  1270. void _M_initialize();
  1271. result_type _M_alpha;
  1272. result_type _M_mu;
  1273. };
  1274. /**
  1275. * @brief Constructors.
  1276. */
  1277. explicit
  1278. pareto_distribution(result_type __alpha_val = result_type(1),
  1279. result_type __mu_val = result_type(1))
  1280. : _M_param(__alpha_val, __mu_val),
  1281. _M_ud()
  1282. { }
  1283. explicit
  1284. pareto_distribution(const param_type& __p)
  1285. : _M_param(__p),
  1286. _M_ud()
  1287. { }
  1288. /**
  1289. * @brief Resets the distribution state.
  1290. */
  1291. void
  1292. reset()
  1293. {
  1294. _M_ud.reset();
  1295. }
  1296. /**
  1297. * @brief Return the parameters of the distribution.
  1298. */
  1299. result_type
  1300. alpha() const
  1301. { return _M_param.alpha(); }
  1302. result_type
  1303. mu() const
  1304. { return _M_param.mu(); }
  1305. /**
  1306. * @brief Returns the parameter set of the distribution.
  1307. */
  1308. param_type
  1309. param() const
  1310. { return _M_param; }
  1311. /**
  1312. * @brief Sets the parameter set of the distribution.
  1313. * @param __param The new parameter set of the distribution.
  1314. */
  1315. void
  1316. param(const param_type& __param)
  1317. { _M_param = __param; }
  1318. /**
  1319. * @brief Returns the greatest lower bound value of the distribution.
  1320. */
  1321. result_type
  1322. min() const
  1323. { return this->mu(); }
  1324. /**
  1325. * @brief Returns the least upper bound value of the distribution.
  1326. */
  1327. result_type
  1328. max() const
  1329. { return std::numeric_limits<result_type>::max(); }
  1330. /**
  1331. * @brief Generating functions.
  1332. */
  1333. template<typename _UniformRandomNumberGenerator>
  1334. result_type
  1335. operator()(_UniformRandomNumberGenerator& __urng)
  1336. {
  1337. return this->mu() * std::pow(this->_M_ud(__urng),
  1338. -result_type(1) / this->alpha());
  1339. }
  1340. template<typename _UniformRandomNumberGenerator>
  1341. result_type
  1342. operator()(_UniformRandomNumberGenerator& __urng,
  1343. const param_type& __p)
  1344. {
  1345. return __p.mu() * std::pow(this->_M_ud(__urng),
  1346. -result_type(1) / __p.alpha());
  1347. }
  1348. template<typename _ForwardIterator,
  1349. typename _UniformRandomNumberGenerator>
  1350. void
  1351. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1352. _UniformRandomNumberGenerator& __urng)
  1353. { this->__generate(__f, __t, __urng, _M_param); }
  1354. template<typename _ForwardIterator,
  1355. typename _UniformRandomNumberGenerator>
  1356. void
  1357. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1358. _UniformRandomNumberGenerator& __urng,
  1359. const param_type& __p)
  1360. { this->__generate_impl(__f, __t, __urng, __p); }
  1361. template<typename _UniformRandomNumberGenerator>
  1362. void
  1363. __generate(result_type* __f, result_type* __t,
  1364. _UniformRandomNumberGenerator& __urng,
  1365. const param_type& __p)
  1366. { this->__generate_impl(__f, __t, __urng, __p); }
  1367. /**
  1368. * @brief Return true if two Pareto distributions have
  1369. * the same parameters and the sequences that would
  1370. * be generated are equal.
  1371. */
  1372. friend bool
  1373. operator==(const pareto_distribution& __d1,
  1374. const pareto_distribution& __d2)
  1375. { return (__d1._M_param == __d2._M_param
  1376. && __d1._M_ud == __d2._M_ud); }
  1377. /**
  1378. * @brief Inserts a %pareto_distribution random number distribution
  1379. * @p __x into the output stream @p __os.
  1380. *
  1381. * @param __os An output stream.
  1382. * @param __x A %pareto_distribution random number distribution.
  1383. *
  1384. * @returns The output stream with the state of @p __x inserted or in
  1385. * an error state.
  1386. */
  1387. template<typename _RealType1, typename _CharT, typename _Traits>
  1388. friend std::basic_ostream<_CharT, _Traits>&
  1389. operator<<(std::basic_ostream<_CharT, _Traits>&,
  1390. const pareto_distribution<_RealType1>&);
  1391. /**
  1392. * @brief Extracts a %pareto_distribution random number distribution
  1393. * @p __x from the input stream @p __is.
  1394. *
  1395. * @param __is An input stream.
  1396. * @param __x A %pareto_distribution random number
  1397. * generator engine.
  1398. *
  1399. * @returns The input stream with @p __x extracted or in an error state.
  1400. */
  1401. template<typename _RealType1, typename _CharT, typename _Traits>
  1402. friend std::basic_istream<_CharT, _Traits>&
  1403. operator>>(std::basic_istream<_CharT, _Traits>&,
  1404. pareto_distribution<_RealType1>&);
  1405. private:
  1406. template<typename _ForwardIterator,
  1407. typename _UniformRandomNumberGenerator>
  1408. void
  1409. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1410. _UniformRandomNumberGenerator& __urng,
  1411. const param_type& __p);
  1412. param_type _M_param;
  1413. std::uniform_real_distribution<result_type> _M_ud;
  1414. };
  1415. /**
  1416. * @brief Return true if two Pareto distributions are not equal.
  1417. */
  1418. template<typename _RealType>
  1419. inline bool
  1420. operator!=(const pareto_distribution<_RealType>& __d1,
  1421. const pareto_distribution<_RealType>& __d2)
  1422. { return !(__d1 == __d2); }
  1423. /**
  1424. * @brief A K continuous distribution for random numbers.
  1425. *
  1426. * The formula for the K probability density function is
  1427. * @f[
  1428. * p(x|\lambda, \mu, \nu) = \frac{2}{x}
  1429. * \left(\frac{\lambda\nu x}{\mu}\right)^{\frac{\lambda + \nu}{2}}
  1430. * \frac{1}{\Gamma(\lambda)\Gamma(\nu)}
  1431. * K_{\nu - \lambda}\left(2\sqrt{\frac{\lambda\nu x}{\mu}}\right)
  1432. * @f]
  1433. * where @f$I_0(z)@f$ is the modified Bessel function of the second kind
  1434. * of order @f$\nu - \lambda@f$ and @f$\lambda > 0@f$, @f$\mu > 0@f$
  1435. * and @f$\nu > 0@f$.
  1436. *
  1437. * <table border=1 cellpadding=10 cellspacing=0>
  1438. * <caption align=top>Distribution Statistics</caption>
  1439. * <tr><td>Mean</td><td>@f$\mu@f$</td></tr>
  1440. * <tr><td>Variance</td><td>@f$\mu^2\frac{\lambda + \nu + 1}{\lambda\nu}@f$</td></tr>
  1441. * <tr><td>Range</td><td>@f$[0, \infty)@f$</td></tr>
  1442. * </table>
  1443. */
  1444. template<typename _RealType = double>
  1445. class
  1446. k_distribution
  1447. {
  1448. static_assert(std::is_floating_point<_RealType>::value,
  1449. "template argument not a floating point type");
  1450. public:
  1451. /** The type of the range of the distribution. */
  1452. typedef _RealType result_type;
  1453. /** Parameter type. */
  1454. struct param_type
  1455. {
  1456. typedef k_distribution<result_type> distribution_type;
  1457. param_type(result_type __lambda_val = result_type(1),
  1458. result_type __mu_val = result_type(1),
  1459. result_type __nu_val = result_type(1))
  1460. : _M_lambda(__lambda_val), _M_mu(__mu_val), _M_nu(__nu_val)
  1461. {
  1462. __glibcxx_assert(_M_lambda > result_type(0));
  1463. __glibcxx_assert(_M_mu > result_type(0));
  1464. __glibcxx_assert(_M_nu > result_type(0));
  1465. }
  1466. result_type
  1467. lambda() const
  1468. { return _M_lambda; }
  1469. result_type
  1470. mu() const
  1471. { return _M_mu; }
  1472. result_type
  1473. nu() const
  1474. { return _M_nu; }
  1475. friend bool
  1476. operator==(const param_type& __p1, const param_type& __p2)
  1477. {
  1478. return __p1._M_lambda == __p2._M_lambda
  1479. && __p1._M_mu == __p2._M_mu
  1480. && __p1._M_nu == __p2._M_nu;
  1481. }
  1482. friend bool
  1483. operator!=(const param_type& __p1, const param_type& __p2)
  1484. { return !(__p1 == __p2); }
  1485. private:
  1486. void _M_initialize();
  1487. result_type _M_lambda;
  1488. result_type _M_mu;
  1489. result_type _M_nu;
  1490. };
  1491. /**
  1492. * @brief Constructors.
  1493. */
  1494. explicit
  1495. k_distribution(result_type __lambda_val = result_type(1),
  1496. result_type __mu_val = result_type(1),
  1497. result_type __nu_val = result_type(1))
  1498. : _M_param(__lambda_val, __mu_val, __nu_val),
  1499. _M_gd1(__lambda_val, result_type(1) / __lambda_val),
  1500. _M_gd2(__nu_val, __mu_val / __nu_val)
  1501. { }
  1502. explicit
  1503. k_distribution(const param_type& __p)
  1504. : _M_param(__p),
  1505. _M_gd1(__p.lambda(), result_type(1) / __p.lambda()),
  1506. _M_gd2(__p.nu(), __p.mu() / __p.nu())
  1507. { }
  1508. /**
  1509. * @brief Resets the distribution state.
  1510. */
  1511. void
  1512. reset()
  1513. {
  1514. _M_gd1.reset();
  1515. _M_gd2.reset();
  1516. }
  1517. /**
  1518. * @brief Return the parameters of the distribution.
  1519. */
  1520. result_type
  1521. lambda() const
  1522. { return _M_param.lambda(); }
  1523. result_type
  1524. mu() const
  1525. { return _M_param.mu(); }
  1526. result_type
  1527. nu() const
  1528. { return _M_param.nu(); }
  1529. /**
  1530. * @brief Returns the parameter set of the distribution.
  1531. */
  1532. param_type
  1533. param() const
  1534. { return _M_param; }
  1535. /**
  1536. * @brief Sets the parameter set of the distribution.
  1537. * @param __param The new parameter set of the distribution.
  1538. */
  1539. void
  1540. param(const param_type& __param)
  1541. { _M_param = __param; }
  1542. /**
  1543. * @brief Returns the greatest lower bound value of the distribution.
  1544. */
  1545. result_type
  1546. min() const
  1547. { return result_type(0); }
  1548. /**
  1549. * @brief Returns the least upper bound value of the distribution.
  1550. */
  1551. result_type
  1552. max() const
  1553. { return std::numeric_limits<result_type>::max(); }
  1554. /**
  1555. * @brief Generating functions.
  1556. */
  1557. template<typename _UniformRandomNumberGenerator>
  1558. result_type
  1559. operator()(_UniformRandomNumberGenerator&);
  1560. template<typename _UniformRandomNumberGenerator>
  1561. result_type
  1562. operator()(_UniformRandomNumberGenerator&, const param_type&);
  1563. template<typename _ForwardIterator,
  1564. typename _UniformRandomNumberGenerator>
  1565. void
  1566. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1567. _UniformRandomNumberGenerator& __urng)
  1568. { this->__generate(__f, __t, __urng, _M_param); }
  1569. template<typename _ForwardIterator,
  1570. typename _UniformRandomNumberGenerator>
  1571. void
  1572. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1573. _UniformRandomNumberGenerator& __urng,
  1574. const param_type& __p)
  1575. { this->__generate_impl(__f, __t, __urng, __p); }
  1576. template<typename _UniformRandomNumberGenerator>
  1577. void
  1578. __generate(result_type* __f, result_type* __t,
  1579. _UniformRandomNumberGenerator& __urng,
  1580. const param_type& __p)
  1581. { this->__generate_impl(__f, __t, __urng, __p); }
  1582. /**
  1583. * @brief Return true if two K distributions have
  1584. * the same parameters and the sequences that would
  1585. * be generated are equal.
  1586. */
  1587. friend bool
  1588. operator==(const k_distribution& __d1,
  1589. const k_distribution& __d2)
  1590. { return (__d1._M_param == __d2._M_param
  1591. && __d1._M_gd1 == __d2._M_gd1
  1592. && __d1._M_gd2 == __d2._M_gd2); }
  1593. /**
  1594. * @brief Inserts a %k_distribution random number distribution
  1595. * @p __x into the output stream @p __os.
  1596. *
  1597. * @param __os An output stream.
  1598. * @param __x A %k_distribution random number distribution.
  1599. *
  1600. * @returns The output stream with the state of @p __x inserted or in
  1601. * an error state.
  1602. */
  1603. template<typename _RealType1, typename _CharT, typename _Traits>
  1604. friend std::basic_ostream<_CharT, _Traits>&
  1605. operator<<(std::basic_ostream<_CharT, _Traits>&,
  1606. const k_distribution<_RealType1>&);
  1607. /**
  1608. * @brief Extracts a %k_distribution random number distribution
  1609. * @p __x from the input stream @p __is.
  1610. *
  1611. * @param __is An input stream.
  1612. * @param __x A %k_distribution random number
  1613. * generator engine.
  1614. *
  1615. * @returns The input stream with @p __x extracted or in an error state.
  1616. */
  1617. template<typename _RealType1, typename _CharT, typename _Traits>
  1618. friend std::basic_istream<_CharT, _Traits>&
  1619. operator>>(std::basic_istream<_CharT, _Traits>&,
  1620. k_distribution<_RealType1>&);
  1621. private:
  1622. template<typename _ForwardIterator,
  1623. typename _UniformRandomNumberGenerator>
  1624. void
  1625. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1626. _UniformRandomNumberGenerator& __urng,
  1627. const param_type& __p);
  1628. param_type _M_param;
  1629. std::gamma_distribution<result_type> _M_gd1;
  1630. std::gamma_distribution<result_type> _M_gd2;
  1631. };
  1632. /**
  1633. * @brief Return true if two K distributions are not equal.
  1634. */
  1635. template<typename _RealType>
  1636. inline bool
  1637. operator!=(const k_distribution<_RealType>& __d1,
  1638. const k_distribution<_RealType>& __d2)
  1639. { return !(__d1 == __d2); }
  1640. /**
  1641. * @brief An arcsine continuous distribution for random numbers.
  1642. *
  1643. * The formula for the arcsine probability density function is
  1644. * @f[
  1645. * p(x|a,b) = \frac{1}{\pi \sqrt{(x - a)(b - x)}}
  1646. * @f]
  1647. * where @f$x >= a@f$ and @f$x <= b@f$.
  1648. *
  1649. * <table border=1 cellpadding=10 cellspacing=0>
  1650. * <caption align=top>Distribution Statistics</caption>
  1651. * <tr><td>Mean</td><td>@f$ (a + b) / 2 @f$</td></tr>
  1652. * <tr><td>Variance</td><td>@f$ (b - a)^2 / 8 @f$</td></tr>
  1653. * <tr><td>Range</td><td>@f$[a, b]@f$</td></tr>
  1654. * </table>
  1655. */
  1656. template<typename _RealType = double>
  1657. class
  1658. arcsine_distribution
  1659. {
  1660. static_assert(std::is_floating_point<_RealType>::value,
  1661. "template argument not a floating point type");
  1662. public:
  1663. /** The type of the range of the distribution. */
  1664. typedef _RealType result_type;
  1665. /** Parameter type. */
  1666. struct param_type
  1667. {
  1668. typedef arcsine_distribution<result_type> distribution_type;
  1669. param_type(result_type __a = result_type(0),
  1670. result_type __b = result_type(1))
  1671. : _M_a(__a), _M_b(__b)
  1672. {
  1673. __glibcxx_assert(_M_a <= _M_b);
  1674. }
  1675. result_type
  1676. a() const
  1677. { return _M_a; }
  1678. result_type
  1679. b() const
  1680. { return _M_b; }
  1681. friend bool
  1682. operator==(const param_type& __p1, const param_type& __p2)
  1683. { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
  1684. friend bool
  1685. operator!=(const param_type& __p1, const param_type& __p2)
  1686. { return !(__p1 == __p2); }
  1687. private:
  1688. void _M_initialize();
  1689. result_type _M_a;
  1690. result_type _M_b;
  1691. };
  1692. /**
  1693. * @brief Constructors.
  1694. */
  1695. explicit
  1696. arcsine_distribution(result_type __a = result_type(0),
  1697. result_type __b = result_type(1))
  1698. : _M_param(__a, __b),
  1699. _M_ud(-1.5707963267948966192313216916397514L,
  1700. +1.5707963267948966192313216916397514L)
  1701. { }
  1702. explicit
  1703. arcsine_distribution(const param_type& __p)
  1704. : _M_param(__p),
  1705. _M_ud(-1.5707963267948966192313216916397514L,
  1706. +1.5707963267948966192313216916397514L)
  1707. { }
  1708. /**
  1709. * @brief Resets the distribution state.
  1710. */
  1711. void
  1712. reset()
  1713. { _M_ud.reset(); }
  1714. /**
  1715. * @brief Return the parameters of the distribution.
  1716. */
  1717. result_type
  1718. a() const
  1719. { return _M_param.a(); }
  1720. result_type
  1721. b() const
  1722. { return _M_param.b(); }
  1723. /**
  1724. * @brief Returns the parameter set of the distribution.
  1725. */
  1726. param_type
  1727. param() const
  1728. { return _M_param; }
  1729. /**
  1730. * @brief Sets the parameter set of the distribution.
  1731. * @param __param The new parameter set of the distribution.
  1732. */
  1733. void
  1734. param(const param_type& __param)
  1735. { _M_param = __param; }
  1736. /**
  1737. * @brief Returns the greatest lower bound value of the distribution.
  1738. */
  1739. result_type
  1740. min() const
  1741. { return this->a(); }
  1742. /**
  1743. * @brief Returns the least upper bound value of the distribution.
  1744. */
  1745. result_type
  1746. max() const
  1747. { return this->b(); }
  1748. /**
  1749. * @brief Generating functions.
  1750. */
  1751. template<typename _UniformRandomNumberGenerator>
  1752. result_type
  1753. operator()(_UniformRandomNumberGenerator& __urng)
  1754. {
  1755. result_type __x = std::sin(this->_M_ud(__urng));
  1756. return (__x * (this->b() - this->a())
  1757. + this->a() + this->b()) / result_type(2);
  1758. }
  1759. template<typename _UniformRandomNumberGenerator>
  1760. result_type
  1761. operator()(_UniformRandomNumberGenerator& __urng,
  1762. const param_type& __p)
  1763. {
  1764. result_type __x = std::sin(this->_M_ud(__urng));
  1765. return (__x * (__p.b() - __p.a())
  1766. + __p.a() + __p.b()) / result_type(2);
  1767. }
  1768. template<typename _ForwardIterator,
  1769. typename _UniformRandomNumberGenerator>
  1770. void
  1771. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1772. _UniformRandomNumberGenerator& __urng)
  1773. { this->__generate(__f, __t, __urng, _M_param); }
  1774. template<typename _ForwardIterator,
  1775. typename _UniformRandomNumberGenerator>
  1776. void
  1777. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1778. _UniformRandomNumberGenerator& __urng,
  1779. const param_type& __p)
  1780. { this->__generate_impl(__f, __t, __urng, __p); }
  1781. template<typename _UniformRandomNumberGenerator>
  1782. void
  1783. __generate(result_type* __f, result_type* __t,
  1784. _UniformRandomNumberGenerator& __urng,
  1785. const param_type& __p)
  1786. { this->__generate_impl(__f, __t, __urng, __p); }
  1787. /**
  1788. * @brief Return true if two arcsine distributions have
  1789. * the same parameters and the sequences that would
  1790. * be generated are equal.
  1791. */
  1792. friend bool
  1793. operator==(const arcsine_distribution& __d1,
  1794. const arcsine_distribution& __d2)
  1795. { return (__d1._M_param == __d2._M_param
  1796. && __d1._M_ud == __d2._M_ud); }
  1797. /**
  1798. * @brief Inserts a %arcsine_distribution random number distribution
  1799. * @p __x into the output stream @p __os.
  1800. *
  1801. * @param __os An output stream.
  1802. * @param __x A %arcsine_distribution random number distribution.
  1803. *
  1804. * @returns The output stream with the state of @p __x inserted or in
  1805. * an error state.
  1806. */
  1807. template<typename _RealType1, typename _CharT, typename _Traits>
  1808. friend std::basic_ostream<_CharT, _Traits>&
  1809. operator<<(std::basic_ostream<_CharT, _Traits>&,
  1810. const arcsine_distribution<_RealType1>&);
  1811. /**
  1812. * @brief Extracts a %arcsine_distribution random number distribution
  1813. * @p __x from the input stream @p __is.
  1814. *
  1815. * @param __is An input stream.
  1816. * @param __x A %arcsine_distribution random number
  1817. * generator engine.
  1818. *
  1819. * @returns The input stream with @p __x extracted or in an error state.
  1820. */
  1821. template<typename _RealType1, typename _CharT, typename _Traits>
  1822. friend std::basic_istream<_CharT, _Traits>&
  1823. operator>>(std::basic_istream<_CharT, _Traits>&,
  1824. arcsine_distribution<_RealType1>&);
  1825. private:
  1826. template<typename _ForwardIterator,
  1827. typename _UniformRandomNumberGenerator>
  1828. void
  1829. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  1830. _UniformRandomNumberGenerator& __urng,
  1831. const param_type& __p);
  1832. param_type _M_param;
  1833. std::uniform_real_distribution<result_type> _M_ud;
  1834. };
  1835. /**
  1836. * @brief Return true if two arcsine distributions are not equal.
  1837. */
  1838. template<typename _RealType>
  1839. inline bool
  1840. operator!=(const arcsine_distribution<_RealType>& __d1,
  1841. const arcsine_distribution<_RealType>& __d2)
  1842. { return !(__d1 == __d2); }
  1843. /**
  1844. * @brief A Hoyt continuous distribution for random numbers.
  1845. *
  1846. * The formula for the Hoyt probability density function is
  1847. * @f[
  1848. * p(x|q,\omega) = \frac{(1 + q^2)x}{q\omega}
  1849. * \exp\left(-\frac{(1 + q^2)^2 x^2}{4 q^2 \omega}\right)
  1850. * I_0\left(\frac{(1 - q^4) x^2}{4 q^2 \omega}\right)
  1851. * @f]
  1852. * where @f$I_0(z)@f$ is the modified Bessel function of the first kind
  1853. * of order 0 and @f$0 < q < 1@f$.
  1854. *
  1855. * <table border=1 cellpadding=10 cellspacing=0>
  1856. * <caption align=top>Distribution Statistics</caption>
  1857. * <tr><td>Mean</td><td>@f$ \sqrt{\frac{2}{\pi}} \sqrt{\frac{\omega}{1 + q^2}}
  1858. * E(1 - q^2) @f$</td></tr>
  1859. * <tr><td>Variance</td><td>@f$ \omega \left(1 - \frac{2E^2(1 - q^2)}
  1860. * {\pi (1 + q^2)}\right) @f$</td></tr>
  1861. * <tr><td>Range</td><td>@f$[0, \infty)@f$</td></tr>
  1862. * </table>
  1863. * where @f$E(x)@f$ is the elliptic function of the second kind.
  1864. */
  1865. template<typename _RealType = double>
  1866. class
  1867. hoyt_distribution
  1868. {
  1869. static_assert(std::is_floating_point<_RealType>::value,
  1870. "template argument not a floating point type");
  1871. public:
  1872. /** The type of the range of the distribution. */
  1873. typedef _RealType result_type;
  1874. /** Parameter type. */
  1875. struct param_type
  1876. {
  1877. typedef hoyt_distribution<result_type> distribution_type;
  1878. param_type(result_type __q = result_type(0.5L),
  1879. result_type __omega = result_type(1))
  1880. : _M_q(__q), _M_omega(__omega)
  1881. {
  1882. __glibcxx_assert(_M_q > result_type(0));
  1883. __glibcxx_assert(_M_q < result_type(1));
  1884. }
  1885. result_type
  1886. q() const
  1887. { return _M_q; }
  1888. result_type
  1889. omega() const
  1890. { return _M_omega; }
  1891. friend bool
  1892. operator==(const param_type& __p1, const param_type& __p2)
  1893. { return __p1._M_q == __p2._M_q && __p1._M_omega == __p2._M_omega; }
  1894. friend bool
  1895. operator!=(const param_type& __p1, const param_type& __p2)
  1896. { return !(__p1 == __p2); }
  1897. private:
  1898. void _M_initialize();
  1899. result_type _M_q;
  1900. result_type _M_omega;
  1901. };
  1902. /**
  1903. * @brief Constructors.
  1904. */
  1905. explicit
  1906. hoyt_distribution(result_type __q = result_type(0.5L),
  1907. result_type __omega = result_type(1))
  1908. : _M_param(__q, __omega),
  1909. _M_ad(result_type(0.5L) * (result_type(1) + __q * __q),
  1910. result_type(0.5L) * (result_type(1) + __q * __q)
  1911. / (__q * __q)),
  1912. _M_ed(result_type(1))
  1913. { }
  1914. explicit
  1915. hoyt_distribution(const param_type& __p)
  1916. : _M_param(__p),
  1917. _M_ad(result_type(0.5L) * (result_type(1) + __p.q() * __p.q()),
  1918. result_type(0.5L) * (result_type(1) + __p.q() * __p.q())
  1919. / (__p.q() * __p.q())),
  1920. _M_ed(result_type(1))
  1921. { }
  1922. /**
  1923. * @brief Resets the distribution state.
  1924. */
  1925. void
  1926. reset()
  1927. {
  1928. _M_ad.reset();
  1929. _M_ed.reset();
  1930. }
  1931. /**
  1932. * @brief Return the parameters of the distribution.
  1933. */
  1934. result_type
  1935. q() const
  1936. { return _M_param.q(); }
  1937. result_type
  1938. omega() const
  1939. { return _M_param.omega(); }
  1940. /**
  1941. * @brief Returns the parameter set of the distribution.
  1942. */
  1943. param_type
  1944. param() const
  1945. { return _M_param; }
  1946. /**
  1947. * @brief Sets the parameter set of the distribution.
  1948. * @param __param The new parameter set of the distribution.
  1949. */
  1950. void
  1951. param(const param_type& __param)
  1952. { _M_param = __param; }
  1953. /**
  1954. * @brief Returns the greatest lower bound value of the distribution.
  1955. */
  1956. result_type
  1957. min() const
  1958. { return result_type(0); }
  1959. /**
  1960. * @brief Returns the least upper bound value of the distribution.
  1961. */
  1962. result_type
  1963. max() const
  1964. { return std::numeric_limits<result_type>::max(); }
  1965. /**
  1966. * @brief Generating functions.
  1967. */
  1968. template<typename _UniformRandomNumberGenerator>
  1969. result_type
  1970. operator()(_UniformRandomNumberGenerator& __urng);
  1971. template<typename _UniformRandomNumberGenerator>
  1972. result_type
  1973. operator()(_UniformRandomNumberGenerator& __urng,
  1974. const param_type& __p);
  1975. template<typename _ForwardIterator,
  1976. typename _UniformRandomNumberGenerator>
  1977. void
  1978. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1979. _UniformRandomNumberGenerator& __urng)
  1980. { this->__generate(__f, __t, __urng, _M_param); }
  1981. template<typename _ForwardIterator,
  1982. typename _UniformRandomNumberGenerator>
  1983. void
  1984. __generate(_ForwardIterator __f, _ForwardIterator __t,
  1985. _UniformRandomNumberGenerator& __urng,
  1986. const param_type& __p)
  1987. { this->__generate_impl(__f, __t, __urng, __p); }
  1988. template<typename _UniformRandomNumberGenerator>
  1989. void
  1990. __generate(result_type* __f, result_type* __t,
  1991. _UniformRandomNumberGenerator& __urng,
  1992. const param_type& __p)
  1993. { this->__generate_impl(__f, __t, __urng, __p); }
  1994. /**
  1995. * @brief Return true if two Hoyt distributions have
  1996. * the same parameters and the sequences that would
  1997. * be generated are equal.
  1998. */
  1999. friend bool
  2000. operator==(const hoyt_distribution& __d1,
  2001. const hoyt_distribution& __d2)
  2002. { return (__d1._M_param == __d2._M_param
  2003. && __d1._M_ad == __d2._M_ad
  2004. && __d1._M_ed == __d2._M_ed); }
  2005. /**
  2006. * @brief Inserts a %hoyt_distribution random number distribution
  2007. * @p __x into the output stream @p __os.
  2008. *
  2009. * @param __os An output stream.
  2010. * @param __x A %hoyt_distribution random number distribution.
  2011. *
  2012. * @returns The output stream with the state of @p __x inserted or in
  2013. * an error state.
  2014. */
  2015. template<typename _RealType1, typename _CharT, typename _Traits>
  2016. friend std::basic_ostream<_CharT, _Traits>&
  2017. operator<<(std::basic_ostream<_CharT, _Traits>&,
  2018. const hoyt_distribution<_RealType1>&);
  2019. /**
  2020. * @brief Extracts a %hoyt_distribution random number distribution
  2021. * @p __x from the input stream @p __is.
  2022. *
  2023. * @param __is An input stream.
  2024. * @param __x A %hoyt_distribution random number
  2025. * generator engine.
  2026. *
  2027. * @returns The input stream with @p __x extracted or in an error state.
  2028. */
  2029. template<typename _RealType1, typename _CharT, typename _Traits>
  2030. friend std::basic_istream<_CharT, _Traits>&
  2031. operator>>(std::basic_istream<_CharT, _Traits>&,
  2032. hoyt_distribution<_RealType1>&);
  2033. private:
  2034. template<typename _ForwardIterator,
  2035. typename _UniformRandomNumberGenerator>
  2036. void
  2037. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2038. _UniformRandomNumberGenerator& __urng,
  2039. const param_type& __p);
  2040. param_type _M_param;
  2041. __gnu_cxx::arcsine_distribution<result_type> _M_ad;
  2042. std::exponential_distribution<result_type> _M_ed;
  2043. };
  2044. /**
  2045. * @brief Return true if two Hoyt distributions are not equal.
  2046. */
  2047. template<typename _RealType>
  2048. inline bool
  2049. operator!=(const hoyt_distribution<_RealType>& __d1,
  2050. const hoyt_distribution<_RealType>& __d2)
  2051. { return !(__d1 == __d2); }
  2052. /**
  2053. * @brief A triangular distribution for random numbers.
  2054. *
  2055. * The formula for the triangular probability density function is
  2056. * @f[
  2057. * / 0 for x < a
  2058. * p(x|a,b,c) = | \frac{2(x-a)}{(c-a)(b-a)} for a <= x <= b
  2059. * | \frac{2(c-x)}{(c-a)(c-b)} for b < x <= c
  2060. * \ 0 for c < x
  2061. * @f]
  2062. *
  2063. * <table border=1 cellpadding=10 cellspacing=0>
  2064. * <caption align=top>Distribution Statistics</caption>
  2065. * <tr><td>Mean</td><td>@f$ \frac{a+b+c}{2} @f$</td></tr>
  2066. * <tr><td>Variance</td><td>@f$ \frac{a^2+b^2+c^2-ab-ac-bc}
  2067. * {18}@f$</td></tr>
  2068. * <tr><td>Range</td><td>@f$[a, c]@f$</td></tr>
  2069. * </table>
  2070. */
  2071. template<typename _RealType = double>
  2072. class triangular_distribution
  2073. {
  2074. static_assert(std::is_floating_point<_RealType>::value,
  2075. "template argument not a floating point type");
  2076. public:
  2077. /** The type of the range of the distribution. */
  2078. typedef _RealType result_type;
  2079. /** Parameter type. */
  2080. struct param_type
  2081. {
  2082. friend class triangular_distribution<_RealType>;
  2083. explicit
  2084. param_type(_RealType __a = _RealType(0),
  2085. _RealType __b = _RealType(0.5),
  2086. _RealType __c = _RealType(1))
  2087. : _M_a(__a), _M_b(__b), _M_c(__c)
  2088. {
  2089. __glibcxx_assert(_M_a <= _M_b);
  2090. __glibcxx_assert(_M_b <= _M_c);
  2091. __glibcxx_assert(_M_a < _M_c);
  2092. _M_r_ab = (_M_b - _M_a) / (_M_c - _M_a);
  2093. _M_f_ab_ac = (_M_b - _M_a) * (_M_c - _M_a);
  2094. _M_f_bc_ac = (_M_c - _M_b) * (_M_c - _M_a);
  2095. }
  2096. _RealType
  2097. a() const
  2098. { return _M_a; }
  2099. _RealType
  2100. b() const
  2101. { return _M_b; }
  2102. _RealType
  2103. c() const
  2104. { return _M_c; }
  2105. friend bool
  2106. operator==(const param_type& __p1, const param_type& __p2)
  2107. {
  2108. return (__p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b
  2109. && __p1._M_c == __p2._M_c);
  2110. }
  2111. friend bool
  2112. operator!=(const param_type& __p1, const param_type& __p2)
  2113. { return !(__p1 == __p2); }
  2114. private:
  2115. _RealType _M_a;
  2116. _RealType _M_b;
  2117. _RealType _M_c;
  2118. _RealType _M_r_ab;
  2119. _RealType _M_f_ab_ac;
  2120. _RealType _M_f_bc_ac;
  2121. };
  2122. /**
  2123. * @brief Constructs a triangle distribution with parameters
  2124. * @f$ a @f$, @f$ b @f$ and @f$ c @f$.
  2125. */
  2126. explicit
  2127. triangular_distribution(result_type __a = result_type(0),
  2128. result_type __b = result_type(0.5),
  2129. result_type __c = result_type(1))
  2130. : _M_param(__a, __b, __c)
  2131. { }
  2132. explicit
  2133. triangular_distribution(const param_type& __p)
  2134. : _M_param(__p)
  2135. { }
  2136. /**
  2137. * @brief Resets the distribution state.
  2138. */
  2139. void
  2140. reset()
  2141. { }
  2142. /**
  2143. * @brief Returns the @f$ a @f$ of the distribution.
  2144. */
  2145. result_type
  2146. a() const
  2147. { return _M_param.a(); }
  2148. /**
  2149. * @brief Returns the @f$ b @f$ of the distribution.
  2150. */
  2151. result_type
  2152. b() const
  2153. { return _M_param.b(); }
  2154. /**
  2155. * @brief Returns the @f$ c @f$ of the distribution.
  2156. */
  2157. result_type
  2158. c() const
  2159. { return _M_param.c(); }
  2160. /**
  2161. * @brief Returns the parameter set of the distribution.
  2162. */
  2163. param_type
  2164. param() const
  2165. { return _M_param; }
  2166. /**
  2167. * @brief Sets the parameter set of the distribution.
  2168. * @param __param The new parameter set of the distribution.
  2169. */
  2170. void
  2171. param(const param_type& __param)
  2172. { _M_param = __param; }
  2173. /**
  2174. * @brief Returns the greatest lower bound value of the distribution.
  2175. */
  2176. result_type
  2177. min() const
  2178. { return _M_param._M_a; }
  2179. /**
  2180. * @brief Returns the least upper bound value of the distribution.
  2181. */
  2182. result_type
  2183. max() const
  2184. { return _M_param._M_c; }
  2185. /**
  2186. * @brief Generating functions.
  2187. */
  2188. template<typename _UniformRandomNumberGenerator>
  2189. result_type
  2190. operator()(_UniformRandomNumberGenerator& __urng)
  2191. { return this->operator()(__urng, _M_param); }
  2192. template<typename _UniformRandomNumberGenerator>
  2193. result_type
  2194. operator()(_UniformRandomNumberGenerator& __urng,
  2195. const param_type& __p)
  2196. {
  2197. std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  2198. __aurng(__urng);
  2199. result_type __rnd = __aurng();
  2200. if (__rnd <= __p._M_r_ab)
  2201. return __p.a() + std::sqrt(__rnd * __p._M_f_ab_ac);
  2202. else
  2203. return __p.c() - std::sqrt((result_type(1) - __rnd)
  2204. * __p._M_f_bc_ac);
  2205. }
  2206. template<typename _ForwardIterator,
  2207. typename _UniformRandomNumberGenerator>
  2208. void
  2209. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2210. _UniformRandomNumberGenerator& __urng)
  2211. { this->__generate(__f, __t, __urng, _M_param); }
  2212. template<typename _ForwardIterator,
  2213. typename _UniformRandomNumberGenerator>
  2214. void
  2215. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2216. _UniformRandomNumberGenerator& __urng,
  2217. const param_type& __p)
  2218. { this->__generate_impl(__f, __t, __urng, __p); }
  2219. template<typename _UniformRandomNumberGenerator>
  2220. void
  2221. __generate(result_type* __f, result_type* __t,
  2222. _UniformRandomNumberGenerator& __urng,
  2223. const param_type& __p)
  2224. { this->__generate_impl(__f, __t, __urng, __p); }
  2225. /**
  2226. * @brief Return true if two triangle distributions have the same
  2227. * parameters and the sequences that would be generated
  2228. * are equal.
  2229. */
  2230. friend bool
  2231. operator==(const triangular_distribution& __d1,
  2232. const triangular_distribution& __d2)
  2233. { return __d1._M_param == __d2._M_param; }
  2234. /**
  2235. * @brief Inserts a %triangular_distribution random number distribution
  2236. * @p __x into the output stream @p __os.
  2237. *
  2238. * @param __os An output stream.
  2239. * @param __x A %triangular_distribution random number distribution.
  2240. *
  2241. * @returns The output stream with the state of @p __x inserted or in
  2242. * an error state.
  2243. */
  2244. template<typename _RealType1, typename _CharT, typename _Traits>
  2245. friend std::basic_ostream<_CharT, _Traits>&
  2246. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2247. const __gnu_cxx::triangular_distribution<_RealType1>& __x);
  2248. /**
  2249. * @brief Extracts a %triangular_distribution random number distribution
  2250. * @p __x from the input stream @p __is.
  2251. *
  2252. * @param __is An input stream.
  2253. * @param __x A %triangular_distribution random number generator engine.
  2254. *
  2255. * @returns The input stream with @p __x extracted or in an error state.
  2256. */
  2257. template<typename _RealType1, typename _CharT, typename _Traits>
  2258. friend std::basic_istream<_CharT, _Traits>&
  2259. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2260. __gnu_cxx::triangular_distribution<_RealType1>& __x);
  2261. private:
  2262. template<typename _ForwardIterator,
  2263. typename _UniformRandomNumberGenerator>
  2264. void
  2265. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2266. _UniformRandomNumberGenerator& __urng,
  2267. const param_type& __p);
  2268. param_type _M_param;
  2269. };
  2270. /**
  2271. * @brief Return true if two triangle distributions are different.
  2272. */
  2273. template<typename _RealType>
  2274. inline bool
  2275. operator!=(const __gnu_cxx::triangular_distribution<_RealType>& __d1,
  2276. const __gnu_cxx::triangular_distribution<_RealType>& __d2)
  2277. { return !(__d1 == __d2); }
  2278. /**
  2279. * @brief A von Mises distribution for random numbers.
  2280. *
  2281. * The formula for the von Mises probability density function is
  2282. * @f[
  2283. * p(x|\mu,\kappa) = \frac{e^{\kappa \cos(x-\mu)}}
  2284. * {2\pi I_0(\kappa)}
  2285. * @f]
  2286. *
  2287. * The generating functions use the method according to:
  2288. *
  2289. * D. J. Best and N. I. Fisher, 1979. "Efficient Simulation of the
  2290. * von Mises Distribution", Journal of the Royal Statistical Society.
  2291. * Series C (Applied Statistics), Vol. 28, No. 2, pp. 152-157.
  2292. *
  2293. * <table border=1 cellpadding=10 cellspacing=0>
  2294. * <caption align=top>Distribution Statistics</caption>
  2295. * <tr><td>Mean</td><td>@f$ \mu @f$</td></tr>
  2296. * <tr><td>Variance</td><td>@f$ 1-I_1(\kappa)/I_0(\kappa) @f$</td></tr>
  2297. * <tr><td>Range</td><td>@f$[-\pi, \pi]@f$</td></tr>
  2298. * </table>
  2299. */
  2300. template<typename _RealType = double>
  2301. class von_mises_distribution
  2302. {
  2303. static_assert(std::is_floating_point<_RealType>::value,
  2304. "template argument not a floating point type");
  2305. public:
  2306. /** The type of the range of the distribution. */
  2307. typedef _RealType result_type;
  2308. /** Parameter type. */
  2309. struct param_type
  2310. {
  2311. friend class von_mises_distribution<_RealType>;
  2312. explicit
  2313. param_type(_RealType __mu = _RealType(0),
  2314. _RealType __kappa = _RealType(1))
  2315. : _M_mu(__mu), _M_kappa(__kappa)
  2316. {
  2317. const _RealType __pi = __gnu_cxx::__math_constants<_RealType>::__pi;
  2318. __glibcxx_assert(_M_mu >= -__pi && _M_mu <= __pi);
  2319. __glibcxx_assert(_M_kappa >= _RealType(0));
  2320. auto __tau = std::sqrt(_RealType(4) * _M_kappa * _M_kappa
  2321. + _RealType(1)) + _RealType(1);
  2322. auto __rho = ((__tau - std::sqrt(_RealType(2) * __tau))
  2323. / (_RealType(2) * _M_kappa));
  2324. _M_r = (_RealType(1) + __rho * __rho) / (_RealType(2) * __rho);
  2325. }
  2326. _RealType
  2327. mu() const
  2328. { return _M_mu; }
  2329. _RealType
  2330. kappa() const
  2331. { return _M_kappa; }
  2332. friend bool
  2333. operator==(const param_type& __p1, const param_type& __p2)
  2334. { return __p1._M_mu == __p2._M_mu && __p1._M_kappa == __p2._M_kappa; }
  2335. friend bool
  2336. operator!=(const param_type& __p1, const param_type& __p2)
  2337. { return !(__p1 == __p2); }
  2338. private:
  2339. _RealType _M_mu;
  2340. _RealType _M_kappa;
  2341. _RealType _M_r;
  2342. };
  2343. /**
  2344. * @brief Constructs a von Mises distribution with parameters
  2345. * @f$\mu@f$ and @f$\kappa@f$.
  2346. */
  2347. explicit
  2348. von_mises_distribution(result_type __mu = result_type(0),
  2349. result_type __kappa = result_type(1))
  2350. : _M_param(__mu, __kappa)
  2351. { }
  2352. explicit
  2353. von_mises_distribution(const param_type& __p)
  2354. : _M_param(__p)
  2355. { }
  2356. /**
  2357. * @brief Resets the distribution state.
  2358. */
  2359. void
  2360. reset()
  2361. { }
  2362. /**
  2363. * @brief Returns the @f$ \mu @f$ of the distribution.
  2364. */
  2365. result_type
  2366. mu() const
  2367. { return _M_param.mu(); }
  2368. /**
  2369. * @brief Returns the @f$ \kappa @f$ of the distribution.
  2370. */
  2371. result_type
  2372. kappa() const
  2373. { return _M_param.kappa(); }
  2374. /**
  2375. * @brief Returns the parameter set of the distribution.
  2376. */
  2377. param_type
  2378. param() const
  2379. { return _M_param; }
  2380. /**
  2381. * @brief Sets the parameter set of the distribution.
  2382. * @param __param The new parameter set of the distribution.
  2383. */
  2384. void
  2385. param(const param_type& __param)
  2386. { _M_param = __param; }
  2387. /**
  2388. * @brief Returns the greatest lower bound value of the distribution.
  2389. */
  2390. result_type
  2391. min() const
  2392. {
  2393. return -__gnu_cxx::__math_constants<result_type>::__pi;
  2394. }
  2395. /**
  2396. * @brief Returns the least upper bound value of the distribution.
  2397. */
  2398. result_type
  2399. max() const
  2400. {
  2401. return __gnu_cxx::__math_constants<result_type>::__pi;
  2402. }
  2403. /**
  2404. * @brief Generating functions.
  2405. */
  2406. template<typename _UniformRandomNumberGenerator>
  2407. result_type
  2408. operator()(_UniformRandomNumberGenerator& __urng)
  2409. { return this->operator()(__urng, _M_param); }
  2410. template<typename _UniformRandomNumberGenerator>
  2411. result_type
  2412. operator()(_UniformRandomNumberGenerator& __urng,
  2413. const param_type& __p);
  2414. template<typename _ForwardIterator,
  2415. typename _UniformRandomNumberGenerator>
  2416. void
  2417. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2418. _UniformRandomNumberGenerator& __urng)
  2419. { this->__generate(__f, __t, __urng, _M_param); }
  2420. template<typename _ForwardIterator,
  2421. typename _UniformRandomNumberGenerator>
  2422. void
  2423. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2424. _UniformRandomNumberGenerator& __urng,
  2425. const param_type& __p)
  2426. { this->__generate_impl(__f, __t, __urng, __p); }
  2427. template<typename _UniformRandomNumberGenerator>
  2428. void
  2429. __generate(result_type* __f, result_type* __t,
  2430. _UniformRandomNumberGenerator& __urng,
  2431. const param_type& __p)
  2432. { this->__generate_impl(__f, __t, __urng, __p); }
  2433. /**
  2434. * @brief Return true if two von Mises distributions have the same
  2435. * parameters and the sequences that would be generated
  2436. * are equal.
  2437. */
  2438. friend bool
  2439. operator==(const von_mises_distribution& __d1,
  2440. const von_mises_distribution& __d2)
  2441. { return __d1._M_param == __d2._M_param; }
  2442. /**
  2443. * @brief Inserts a %von_mises_distribution random number distribution
  2444. * @p __x into the output stream @p __os.
  2445. *
  2446. * @param __os An output stream.
  2447. * @param __x A %von_mises_distribution random number distribution.
  2448. *
  2449. * @returns The output stream with the state of @p __x inserted or in
  2450. * an error state.
  2451. */
  2452. template<typename _RealType1, typename _CharT, typename _Traits>
  2453. friend std::basic_ostream<_CharT, _Traits>&
  2454. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2455. const __gnu_cxx::von_mises_distribution<_RealType1>& __x);
  2456. /**
  2457. * @brief Extracts a %von_mises_distribution random number distribution
  2458. * @p __x from the input stream @p __is.
  2459. *
  2460. * @param __is An input stream.
  2461. * @param __x A %von_mises_distribution random number generator engine.
  2462. *
  2463. * @returns The input stream with @p __x extracted or in an error state.
  2464. */
  2465. template<typename _RealType1, typename _CharT, typename _Traits>
  2466. friend std::basic_istream<_CharT, _Traits>&
  2467. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2468. __gnu_cxx::von_mises_distribution<_RealType1>& __x);
  2469. private:
  2470. template<typename _ForwardIterator,
  2471. typename _UniformRandomNumberGenerator>
  2472. void
  2473. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2474. _UniformRandomNumberGenerator& __urng,
  2475. const param_type& __p);
  2476. param_type _M_param;
  2477. };
  2478. /**
  2479. * @brief Return true if two von Mises distributions are different.
  2480. */
  2481. template<typename _RealType>
  2482. inline bool
  2483. operator!=(const __gnu_cxx::von_mises_distribution<_RealType>& __d1,
  2484. const __gnu_cxx::von_mises_distribution<_RealType>& __d2)
  2485. { return !(__d1 == __d2); }
  2486. /**
  2487. * @brief A discrete hypergeometric random number distribution.
  2488. *
  2489. * The hypergeometric distribution is a discrete probability distribution
  2490. * that describes the probability of @p k successes in @p n draws @a without
  2491. * replacement from a finite population of size @p N containing exactly @p K
  2492. * successes.
  2493. *
  2494. * The formula for the hypergeometric probability density function is
  2495. * @f[
  2496. * p(k|N,K,n) = \frac{\binom{K}{k} \binom{N-K}{n-k}}{\binom{N}{n}}
  2497. * @f]
  2498. * where @f$N@f$ is the total population of the distribution,
  2499. * @f$K@f$ is the total population of the distribution.
  2500. *
  2501. * <table border=1 cellpadding=10 cellspacing=0>
  2502. * <caption align=top>Distribution Statistics</caption>
  2503. * <tr><td>Mean</td><td>@f$ n\frac{K}{N} @f$</td></tr>
  2504. * <tr><td>Variance</td><td>@f$ n\frac{K}{N}\frac{N-K}{N}\frac{N-n}{N-1}
  2505. * @f$</td></tr>
  2506. * <tr><td>Range</td><td>@f$[max(0, n+K-N), min(K, n)]@f$</td></tr>
  2507. * </table>
  2508. */
  2509. template<typename _UIntType = unsigned int>
  2510. class hypergeometric_distribution
  2511. {
  2512. static_assert(std::is_unsigned<_UIntType>::value, "template argument "
  2513. "substituting _UIntType not an unsigned integral type");
  2514. public:
  2515. /** The type of the range of the distribution. */
  2516. typedef _UIntType result_type;
  2517. /** Parameter type. */
  2518. struct param_type
  2519. {
  2520. typedef hypergeometric_distribution<_UIntType> distribution_type;
  2521. friend class hypergeometric_distribution<_UIntType>;
  2522. explicit
  2523. param_type(result_type __N = 10, result_type __K = 5,
  2524. result_type __n = 1)
  2525. : _M_N{__N}, _M_K{__K}, _M_n{__n}
  2526. {
  2527. __glibcxx_assert(_M_N >= _M_K);
  2528. __glibcxx_assert(_M_N >= _M_n);
  2529. }
  2530. result_type
  2531. total_size() const
  2532. { return _M_N; }
  2533. result_type
  2534. successful_size() const
  2535. { return _M_K; }
  2536. result_type
  2537. unsuccessful_size() const
  2538. { return _M_N - _M_K; }
  2539. result_type
  2540. total_draws() const
  2541. { return _M_n; }
  2542. friend bool
  2543. operator==(const param_type& __p1, const param_type& __p2)
  2544. { return (__p1._M_N == __p2._M_N)
  2545. && (__p1._M_K == __p2._M_K)
  2546. && (__p1._M_n == __p2._M_n); }
  2547. friend bool
  2548. operator!=(const param_type& __p1, const param_type& __p2)
  2549. { return !(__p1 == __p2); }
  2550. private:
  2551. result_type _M_N;
  2552. result_type _M_K;
  2553. result_type _M_n;
  2554. };
  2555. // constructors and member function
  2556. explicit
  2557. hypergeometric_distribution(result_type __N = 10, result_type __K = 5,
  2558. result_type __n = 1)
  2559. : _M_param{__N, __K, __n}
  2560. { }
  2561. explicit
  2562. hypergeometric_distribution(const param_type& __p)
  2563. : _M_param{__p}
  2564. { }
  2565. /**
  2566. * @brief Resets the distribution state.
  2567. */
  2568. void
  2569. reset()
  2570. { }
  2571. /**
  2572. * @brief Returns the distribution parameter @p N,
  2573. * the total number of items.
  2574. */
  2575. result_type
  2576. total_size() const
  2577. { return this->_M_param.total_size(); }
  2578. /**
  2579. * @brief Returns the distribution parameter @p K,
  2580. * the total number of successful items.
  2581. */
  2582. result_type
  2583. successful_size() const
  2584. { return this->_M_param.successful_size(); }
  2585. /**
  2586. * @brief Returns the total number of unsuccessful items @f$ N - K @f$.
  2587. */
  2588. result_type
  2589. unsuccessful_size() const
  2590. { return this->_M_param.unsuccessful_size(); }
  2591. /**
  2592. * @brief Returns the distribution parameter @p n,
  2593. * the total number of draws.
  2594. */
  2595. result_type
  2596. total_draws() const
  2597. { return this->_M_param.total_draws(); }
  2598. /**
  2599. * @brief Returns the parameter set of the distribution.
  2600. */
  2601. param_type
  2602. param() const
  2603. { return this->_M_param; }
  2604. /**
  2605. * @brief Sets the parameter set of the distribution.
  2606. * @param __param The new parameter set of the distribution.
  2607. */
  2608. void
  2609. param(const param_type& __param)
  2610. { this->_M_param = __param; }
  2611. /**
  2612. * @brief Returns the greatest lower bound value of the distribution.
  2613. */
  2614. result_type
  2615. min() const
  2616. {
  2617. using _IntType = typename std::make_signed<result_type>::type;
  2618. return static_cast<result_type>(std::max(static_cast<_IntType>(0),
  2619. static_cast<_IntType>(this->total_draws()
  2620. - this->unsuccessful_size())));
  2621. }
  2622. /**
  2623. * @brief Returns the least upper bound value of the distribution.
  2624. */
  2625. result_type
  2626. max() const
  2627. { return std::min(this->successful_size(), this->total_draws()); }
  2628. /**
  2629. * @brief Generating functions.
  2630. */
  2631. template<typename _UniformRandomNumberGenerator>
  2632. result_type
  2633. operator()(_UniformRandomNumberGenerator& __urng)
  2634. { return this->operator()(__urng, this->_M_param); }
  2635. template<typename _UniformRandomNumberGenerator>
  2636. result_type
  2637. operator()(_UniformRandomNumberGenerator& __urng,
  2638. const param_type& __p);
  2639. template<typename _ForwardIterator,
  2640. typename _UniformRandomNumberGenerator>
  2641. void
  2642. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2643. _UniformRandomNumberGenerator& __urng)
  2644. { this->__generate(__f, __t, __urng, this->_M_param); }
  2645. template<typename _ForwardIterator,
  2646. typename _UniformRandomNumberGenerator>
  2647. void
  2648. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2649. _UniformRandomNumberGenerator& __urng,
  2650. const param_type& __p)
  2651. { this->__generate_impl(__f, __t, __urng, __p); }
  2652. template<typename _UniformRandomNumberGenerator>
  2653. void
  2654. __generate(result_type* __f, result_type* __t,
  2655. _UniformRandomNumberGenerator& __urng,
  2656. const param_type& __p)
  2657. { this->__generate_impl(__f, __t, __urng, __p); }
  2658. /**
  2659. * @brief Return true if two hypergeometric distributions have the same
  2660. * parameters and the sequences that would be generated
  2661. * are equal.
  2662. */
  2663. friend bool
  2664. operator==(const hypergeometric_distribution& __d1,
  2665. const hypergeometric_distribution& __d2)
  2666. { return __d1._M_param == __d2._M_param; }
  2667. /**
  2668. * @brief Inserts a %hypergeometric_distribution random number
  2669. * distribution @p __x into the output stream @p __os.
  2670. *
  2671. * @param __os An output stream.
  2672. * @param __x A %hypergeometric_distribution random number
  2673. * distribution.
  2674. *
  2675. * @returns The output stream with the state of @p __x inserted or in
  2676. * an error state.
  2677. */
  2678. template<typename _UIntType1, typename _CharT, typename _Traits>
  2679. friend std::basic_ostream<_CharT, _Traits>&
  2680. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  2681. const __gnu_cxx::hypergeometric_distribution<_UIntType1>&
  2682. __x);
  2683. /**
  2684. * @brief Extracts a %hypergeometric_distribution random number
  2685. * distribution @p __x from the input stream @p __is.
  2686. *
  2687. * @param __is An input stream.
  2688. * @param __x A %hypergeometric_distribution random number generator
  2689. * distribution.
  2690. *
  2691. * @returns The input stream with @p __x extracted or in an error
  2692. * state.
  2693. */
  2694. template<typename _UIntType1, typename _CharT, typename _Traits>
  2695. friend std::basic_istream<_CharT, _Traits>&
  2696. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  2697. __gnu_cxx::hypergeometric_distribution<_UIntType1>& __x);
  2698. private:
  2699. template<typename _ForwardIterator,
  2700. typename _UniformRandomNumberGenerator>
  2701. void
  2702. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2703. _UniformRandomNumberGenerator& __urng,
  2704. const param_type& __p);
  2705. param_type _M_param;
  2706. };
  2707. /**
  2708. * @brief Return true if two hypergeometric distributions are different.
  2709. */
  2710. template<typename _UIntType>
  2711. inline bool
  2712. operator!=(const __gnu_cxx::hypergeometric_distribution<_UIntType>& __d1,
  2713. const __gnu_cxx::hypergeometric_distribution<_UIntType>& __d2)
  2714. { return !(__d1 == __d2); }
  2715. /**
  2716. * @brief A logistic continuous distribution for random numbers.
  2717. *
  2718. * The formula for the logistic probability density function is
  2719. * @f[
  2720. * p(x|\a,\b) = \frac{e^{(x - a)/b}}{b[1 + e^{(x - a)/b}]^2}
  2721. * @f]
  2722. * where @f$b > 0@f$.
  2723. *
  2724. * The formula for the logistic probability function is
  2725. * @f[
  2726. * cdf(x|\a,\b) = \frac{e^{(x - a)/b}}{1 + e^{(x - a)/b}}
  2727. * @f]
  2728. * where @f$b > 0@f$.
  2729. *
  2730. * <table border=1 cellpadding=10 cellspacing=0>
  2731. * <caption align=top>Distribution Statistics</caption>
  2732. * <tr><td>Mean</td><td>@f$a@f$</td></tr>
  2733. * <tr><td>Variance</td><td>@f$b^2\pi^2/3@f$</td></tr>
  2734. * <tr><td>Range</td><td>@f$[0, \infty)@f$</td></tr>
  2735. * </table>
  2736. */
  2737. template<typename _RealType = double>
  2738. class
  2739. logistic_distribution
  2740. {
  2741. static_assert(std::is_floating_point<_RealType>::value,
  2742. "template argument not a floating point type");
  2743. public:
  2744. /** The type of the range of the distribution. */
  2745. typedef _RealType result_type;
  2746. /** Parameter type. */
  2747. struct param_type
  2748. {
  2749. typedef logistic_distribution<result_type> distribution_type;
  2750. param_type(result_type __a = result_type(0),
  2751. result_type __b = result_type(1))
  2752. : _M_a(__a), _M_b(__b)
  2753. {
  2754. __glibcxx_assert(_M_b > result_type(0));
  2755. }
  2756. result_type
  2757. a() const
  2758. { return _M_a; }
  2759. result_type
  2760. b() const
  2761. { return _M_b; }
  2762. friend bool
  2763. operator==(const param_type& __p1, const param_type& __p2)
  2764. { return __p1._M_a == __p2._M_a && __p1._M_b == __p2._M_b; }
  2765. friend bool
  2766. operator!=(const param_type& __p1, const param_type& __p2)
  2767. { return !(__p1 == __p2); }
  2768. private:
  2769. void _M_initialize();
  2770. result_type _M_a;
  2771. result_type _M_b;
  2772. };
  2773. /**
  2774. * @brief Constructors.
  2775. */
  2776. explicit
  2777. logistic_distribution(result_type __a = result_type(0),
  2778. result_type __b = result_type(1))
  2779. : _M_param(__a, __b)
  2780. { }
  2781. explicit
  2782. logistic_distribution(const param_type& __p)
  2783. : _M_param(__p)
  2784. { }
  2785. /**
  2786. * @brief Resets the distribution state.
  2787. */
  2788. void
  2789. reset()
  2790. { }
  2791. /**
  2792. * @brief Return the parameters of the distribution.
  2793. */
  2794. result_type
  2795. a() const
  2796. { return _M_param.a(); }
  2797. result_type
  2798. b() const
  2799. { return _M_param.b(); }
  2800. /**
  2801. * @brief Returns the parameter set of the distribution.
  2802. */
  2803. param_type
  2804. param() const
  2805. { return _M_param; }
  2806. /**
  2807. * @brief Sets the parameter set of the distribution.
  2808. * @param __param The new parameter set of the distribution.
  2809. */
  2810. void
  2811. param(const param_type& __param)
  2812. { _M_param = __param; }
  2813. /**
  2814. * @brief Returns the greatest lower bound value of the distribution.
  2815. */
  2816. result_type
  2817. min() const
  2818. { return -std::numeric_limits<result_type>::max(); }
  2819. /**
  2820. * @brief Returns the least upper bound value of the distribution.
  2821. */
  2822. result_type
  2823. max() const
  2824. { return std::numeric_limits<result_type>::max(); }
  2825. /**
  2826. * @brief Generating functions.
  2827. */
  2828. template<typename _UniformRandomNumberGenerator>
  2829. result_type
  2830. operator()(_UniformRandomNumberGenerator& __urng)
  2831. { return this->operator()(__urng, this->_M_param); }
  2832. template<typename _UniformRandomNumberGenerator>
  2833. result_type
  2834. operator()(_UniformRandomNumberGenerator&,
  2835. const param_type&);
  2836. template<typename _ForwardIterator,
  2837. typename _UniformRandomNumberGenerator>
  2838. void
  2839. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2840. _UniformRandomNumberGenerator& __urng)
  2841. { this->__generate(__f, __t, __urng, this->param()); }
  2842. template<typename _ForwardIterator,
  2843. typename _UniformRandomNumberGenerator>
  2844. void
  2845. __generate(_ForwardIterator __f, _ForwardIterator __t,
  2846. _UniformRandomNumberGenerator& __urng,
  2847. const param_type& __p)
  2848. { this->__generate_impl(__f, __t, __urng, __p); }
  2849. template<typename _UniformRandomNumberGenerator>
  2850. void
  2851. __generate(result_type* __f, result_type* __t,
  2852. _UniformRandomNumberGenerator& __urng,
  2853. const param_type& __p)
  2854. { this->__generate_impl(__f, __t, __urng, __p); }
  2855. /**
  2856. * @brief Return true if two logistic distributions have
  2857. * the same parameters and the sequences that would
  2858. * be generated are equal.
  2859. */
  2860. template<typename _RealType1>
  2861. friend bool
  2862. operator==(const logistic_distribution<_RealType1>& __d1,
  2863. const logistic_distribution<_RealType1>& __d2)
  2864. { return __d1.param() == __d2.param(); }
  2865. /**
  2866. * @brief Inserts a %logistic_distribution random number distribution
  2867. * @p __x into the output stream @p __os.
  2868. *
  2869. * @param __os An output stream.
  2870. * @param __x A %logistic_distribution random number distribution.
  2871. *
  2872. * @returns The output stream with the state of @p __x inserted or in
  2873. * an error state.
  2874. */
  2875. template<typename _RealType1, typename _CharT, typename _Traits>
  2876. friend std::basic_ostream<_CharT, _Traits>&
  2877. operator<<(std::basic_ostream<_CharT, _Traits>&,
  2878. const logistic_distribution<_RealType1>&);
  2879. /**
  2880. * @brief Extracts a %logistic_distribution random number distribution
  2881. * @p __x from the input stream @p __is.
  2882. *
  2883. * @param __is An input stream.
  2884. * @param __x A %logistic_distribution random number
  2885. * generator engine.
  2886. *
  2887. * @returns The input stream with @p __x extracted or in an error state.
  2888. */
  2889. template<typename _RealType1, typename _CharT, typename _Traits>
  2890. friend std::basic_istream<_CharT, _Traits>&
  2891. operator>>(std::basic_istream<_CharT, _Traits>&,
  2892. logistic_distribution<_RealType1>&);
  2893. private:
  2894. template<typename _ForwardIterator,
  2895. typename _UniformRandomNumberGenerator>
  2896. void
  2897. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  2898. _UniformRandomNumberGenerator& __urng,
  2899. const param_type& __p);
  2900. param_type _M_param;
  2901. };
  2902. /**
  2903. * @brief Return true if two logistic distributions are not equal.
  2904. */
  2905. template<typename _RealType1>
  2906. inline bool
  2907. operator!=(const logistic_distribution<_RealType1>& __d1,
  2908. const logistic_distribution<_RealType1>& __d2)
  2909. { return !(__d1 == __d2); }
  2910. /**
  2911. * @brief A distribution for random coordinates on a unit sphere.
  2912. *
  2913. * The method used in the generation function is attributed by Donald Knuth
  2914. * to G. W. Brown, Modern Mathematics for the Engineer (1956).
  2915. */
  2916. template<std::size_t _Dimen, typename _RealType = double>
  2917. class uniform_on_sphere_distribution
  2918. {
  2919. static_assert(std::is_floating_point<_RealType>::value,
  2920. "template argument not a floating point type");
  2921. static_assert(_Dimen != 0, "dimension is zero");
  2922. public:
  2923. /** The type of the range of the distribution. */
  2924. typedef std::array<_RealType, _Dimen> result_type;
  2925. /** Parameter type. */
  2926. struct param_type
  2927. {
  2928. explicit
  2929. param_type()
  2930. { }
  2931. friend bool
  2932. operator==(const param_type&, const param_type&)
  2933. { return true; }
  2934. friend bool
  2935. operator!=(const param_type&, const param_type&)
  2936. { return false; }
  2937. };
  2938. /**
  2939. * @brief Constructs a uniform on sphere distribution.
  2940. */
  2941. explicit
  2942. uniform_on_sphere_distribution()
  2943. : _M_param(), _M_nd()
  2944. { }
  2945. explicit
  2946. uniform_on_sphere_distribution(const param_type& __p)
  2947. : _M_param(__p), _M_nd()
  2948. { }
  2949. /**
  2950. * @brief Resets the distribution state.
  2951. */
  2952. void
  2953. reset()
  2954. { _M_nd.reset(); }
  2955. /**
  2956. * @brief Returns the parameter set of the distribution.
  2957. */
  2958. param_type
  2959. param() const
  2960. { return _M_param; }
  2961. /**
  2962. * @brief Sets the parameter set of the distribution.
  2963. * @param __param The new parameter set of the distribution.
  2964. */
  2965. void
  2966. param(const param_type& __param)
  2967. { _M_param = __param; }
  2968. /**
  2969. * @brief Returns the greatest lower bound value of the distribution.
  2970. * This function makes no sense for this distribution.
  2971. */
  2972. result_type
  2973. min() const
  2974. {
  2975. result_type __res;
  2976. __res.fill(0);
  2977. return __res;
  2978. }
  2979. /**
  2980. * @brief Returns the least upper bound value of the distribution.
  2981. * This function makes no sense for this distribution.
  2982. */
  2983. result_type
  2984. max() const
  2985. {
  2986. result_type __res;
  2987. __res.fill(0);
  2988. return __res;
  2989. }
  2990. /**
  2991. * @brief Generating functions.
  2992. */
  2993. template<typename _UniformRandomNumberGenerator>
  2994. result_type
  2995. operator()(_UniformRandomNumberGenerator& __urng)
  2996. { return this->operator()(__urng, _M_param); }
  2997. template<typename _UniformRandomNumberGenerator>
  2998. result_type
  2999. operator()(_UniformRandomNumberGenerator& __urng,
  3000. const param_type& __p);
  3001. template<typename _ForwardIterator,
  3002. typename _UniformRandomNumberGenerator>
  3003. void
  3004. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3005. _UniformRandomNumberGenerator& __urng)
  3006. { this->__generate(__f, __t, __urng, this->param()); }
  3007. template<typename _ForwardIterator,
  3008. typename _UniformRandomNumberGenerator>
  3009. void
  3010. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3011. _UniformRandomNumberGenerator& __urng,
  3012. const param_type& __p)
  3013. { this->__generate_impl(__f, __t, __urng, __p); }
  3014. template<typename _UniformRandomNumberGenerator>
  3015. void
  3016. __generate(result_type* __f, result_type* __t,
  3017. _UniformRandomNumberGenerator& __urng,
  3018. const param_type& __p)
  3019. { this->__generate_impl(__f, __t, __urng, __p); }
  3020. /**
  3021. * @brief Return true if two uniform on sphere distributions have
  3022. * the same parameters and the sequences that would be
  3023. * generated are equal.
  3024. */
  3025. friend bool
  3026. operator==(const uniform_on_sphere_distribution& __d1,
  3027. const uniform_on_sphere_distribution& __d2)
  3028. { return __d1._M_nd == __d2._M_nd; }
  3029. /**
  3030. * @brief Inserts a %uniform_on_sphere_distribution random number
  3031. * distribution @p __x into the output stream @p __os.
  3032. *
  3033. * @param __os An output stream.
  3034. * @param __x A %uniform_on_sphere_distribution random number
  3035. * distribution.
  3036. *
  3037. * @returns The output stream with the state of @p __x inserted or in
  3038. * an error state.
  3039. */
  3040. template<size_t _Dimen1, typename _RealType1, typename _CharT,
  3041. typename _Traits>
  3042. friend std::basic_ostream<_CharT, _Traits>&
  3043. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3044. const __gnu_cxx::uniform_on_sphere_distribution<_Dimen1,
  3045. _RealType1>&
  3046. __x);
  3047. /**
  3048. * @brief Extracts a %uniform_on_sphere_distribution random number
  3049. * distribution
  3050. * @p __x from the input stream @p __is.
  3051. *
  3052. * @param __is An input stream.
  3053. * @param __x A %uniform_on_sphere_distribution random number
  3054. * generator engine.
  3055. *
  3056. * @returns The input stream with @p __x extracted or in an error state.
  3057. */
  3058. template<std::size_t _Dimen1, typename _RealType1, typename _CharT,
  3059. typename _Traits>
  3060. friend std::basic_istream<_CharT, _Traits>&
  3061. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3062. __gnu_cxx::uniform_on_sphere_distribution<_Dimen1,
  3063. _RealType1>& __x);
  3064. private:
  3065. template<typename _ForwardIterator,
  3066. typename _UniformRandomNumberGenerator>
  3067. void
  3068. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3069. _UniformRandomNumberGenerator& __urng,
  3070. const param_type& __p);
  3071. param_type _M_param;
  3072. std::normal_distribution<_RealType> _M_nd;
  3073. };
  3074. /**
  3075. * @brief Return true if two uniform on sphere distributions are different.
  3076. */
  3077. template<std::size_t _Dimen, typename _RealType>
  3078. inline bool
  3079. operator!=(const __gnu_cxx::uniform_on_sphere_distribution<_Dimen,
  3080. _RealType>& __d1,
  3081. const __gnu_cxx::uniform_on_sphere_distribution<_Dimen,
  3082. _RealType>& __d2)
  3083. { return !(__d1 == __d2); }
  3084. /**
  3085. * @brief A distribution for random coordinates inside a unit sphere.
  3086. */
  3087. template<std::size_t _Dimen, typename _RealType = double>
  3088. class uniform_inside_sphere_distribution
  3089. {
  3090. static_assert(std::is_floating_point<_RealType>::value,
  3091. "template argument not a floating point type");
  3092. static_assert(_Dimen != 0, "dimension is zero");
  3093. public:
  3094. /** The type of the range of the distribution. */
  3095. using result_type = std::array<_RealType, _Dimen>;
  3096. /** Parameter type. */
  3097. struct param_type
  3098. {
  3099. using distribution_type
  3100. = uniform_inside_sphere_distribution<_Dimen, _RealType>;
  3101. friend class uniform_inside_sphere_distribution<_Dimen, _RealType>;
  3102. explicit
  3103. param_type(_RealType __radius = _RealType(1))
  3104. : _M_radius(__radius)
  3105. {
  3106. __glibcxx_assert(_M_radius > _RealType(0));
  3107. }
  3108. _RealType
  3109. radius() const
  3110. { return _M_radius; }
  3111. friend bool
  3112. operator==(const param_type& __p1, const param_type& __p2)
  3113. { return __p1._M_radius == __p2._M_radius; }
  3114. friend bool
  3115. operator!=(const param_type& __p1, const param_type& __p2)
  3116. { return !(__p1 == __p2); }
  3117. private:
  3118. _RealType _M_radius;
  3119. };
  3120. /**
  3121. * @brief Constructors.
  3122. */
  3123. explicit
  3124. uniform_inside_sphere_distribution(_RealType __radius = _RealType(1))
  3125. : _M_param(__radius), _M_uosd()
  3126. { }
  3127. explicit
  3128. uniform_inside_sphere_distribution(const param_type& __p)
  3129. : _M_param(__p), _M_uosd()
  3130. { }
  3131. /**
  3132. * @brief Resets the distribution state.
  3133. */
  3134. void
  3135. reset()
  3136. { _M_uosd.reset(); }
  3137. /**
  3138. * @brief Returns the @f$radius@f$ of the distribution.
  3139. */
  3140. _RealType
  3141. radius() const
  3142. { return _M_param.radius(); }
  3143. /**
  3144. * @brief Returns the parameter set of the distribution.
  3145. */
  3146. param_type
  3147. param() const
  3148. { return _M_param; }
  3149. /**
  3150. * @brief Sets the parameter set of the distribution.
  3151. * @param __param The new parameter set of the distribution.
  3152. */
  3153. void
  3154. param(const param_type& __param)
  3155. { _M_param = __param; }
  3156. /**
  3157. * @brief Returns the greatest lower bound value of the distribution.
  3158. * This function makes no sense for this distribution.
  3159. */
  3160. result_type
  3161. min() const
  3162. {
  3163. result_type __res;
  3164. __res.fill(0);
  3165. return __res;
  3166. }
  3167. /**
  3168. * @brief Returns the least upper bound value of the distribution.
  3169. * This function makes no sense for this distribution.
  3170. */
  3171. result_type
  3172. max() const
  3173. {
  3174. result_type __res;
  3175. __res.fill(0);
  3176. return __res;
  3177. }
  3178. /**
  3179. * @brief Generating functions.
  3180. */
  3181. template<typename _UniformRandomNumberGenerator>
  3182. result_type
  3183. operator()(_UniformRandomNumberGenerator& __urng)
  3184. { return this->operator()(__urng, _M_param); }
  3185. template<typename _UniformRandomNumberGenerator>
  3186. result_type
  3187. operator()(_UniformRandomNumberGenerator& __urng,
  3188. const param_type& __p);
  3189. template<typename _ForwardIterator,
  3190. typename _UniformRandomNumberGenerator>
  3191. void
  3192. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3193. _UniformRandomNumberGenerator& __urng)
  3194. { this->__generate(__f, __t, __urng, this->param()); }
  3195. template<typename _ForwardIterator,
  3196. typename _UniformRandomNumberGenerator>
  3197. void
  3198. __generate(_ForwardIterator __f, _ForwardIterator __t,
  3199. _UniformRandomNumberGenerator& __urng,
  3200. const param_type& __p)
  3201. { this->__generate_impl(__f, __t, __urng, __p); }
  3202. template<typename _UniformRandomNumberGenerator>
  3203. void
  3204. __generate(result_type* __f, result_type* __t,
  3205. _UniformRandomNumberGenerator& __urng,
  3206. const param_type& __p)
  3207. { this->__generate_impl(__f, __t, __urng, __p); }
  3208. /**
  3209. * @brief Return true if two uniform on sphere distributions have
  3210. * the same parameters and the sequences that would be
  3211. * generated are equal.
  3212. */
  3213. friend bool
  3214. operator==(const uniform_inside_sphere_distribution& __d1,
  3215. const uniform_inside_sphere_distribution& __d2)
  3216. { return __d1._M_param == __d2._M_param && __d1._M_uosd == __d2._M_uosd; }
  3217. /**
  3218. * @brief Inserts a %uniform_inside_sphere_distribution random number
  3219. * distribution @p __x into the output stream @p __os.
  3220. *
  3221. * @param __os An output stream.
  3222. * @param __x A %uniform_inside_sphere_distribution random number
  3223. * distribution.
  3224. *
  3225. * @returns The output stream with the state of @p __x inserted or in
  3226. * an error state.
  3227. */
  3228. template<size_t _Dimen1, typename _RealType1, typename _CharT,
  3229. typename _Traits>
  3230. friend std::basic_ostream<_CharT, _Traits>&
  3231. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  3232. const __gnu_cxx::uniform_inside_sphere_distribution<_Dimen1,
  3233. _RealType1>&
  3234. );
  3235. /**
  3236. * @brief Extracts a %uniform_inside_sphere_distribution random number
  3237. * distribution
  3238. * @p __x from the input stream @p __is.
  3239. *
  3240. * @param __is An input stream.
  3241. * @param __x A %uniform_inside_sphere_distribution random number
  3242. * generator engine.
  3243. *
  3244. * @returns The input stream with @p __x extracted or in an error state.
  3245. */
  3246. template<std::size_t _Dimen1, typename _RealType1, typename _CharT,
  3247. typename _Traits>
  3248. friend std::basic_istream<_CharT, _Traits>&
  3249. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  3250. __gnu_cxx::uniform_inside_sphere_distribution<_Dimen1,
  3251. _RealType1>&);
  3252. private:
  3253. template<typename _ForwardIterator,
  3254. typename _UniformRandomNumberGenerator>
  3255. void
  3256. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  3257. _UniformRandomNumberGenerator& __urng,
  3258. const param_type& __p);
  3259. param_type _M_param;
  3260. uniform_on_sphere_distribution<_Dimen, _RealType> _M_uosd;
  3261. };
  3262. /**
  3263. * @brief Return true if two uniform on sphere distributions are different.
  3264. */
  3265. template<std::size_t _Dimen, typename _RealType>
  3266. inline bool
  3267. operator!=(const __gnu_cxx::uniform_inside_sphere_distribution<_Dimen,
  3268. _RealType>& __d1,
  3269. const __gnu_cxx::uniform_inside_sphere_distribution<_Dimen,
  3270. _RealType>& __d2)
  3271. { return !(__d1 == __d2); }
  3272. _GLIBCXX_END_NAMESPACE_VERSION
  3273. } // namespace __gnu_cxx
  3274. #include <ext/opt_random.h>
  3275. #include <ext/random.tcc>
  3276. #endif // _GLIBCXX_USE_C99_STDINT_TR1 && UINT32_C
  3277. #endif // C++11
  3278. #endif // _EXT_RANDOM