random 101 KB

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