random.tcc 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  1. // Random number extensions -*- C++ -*-
  2. // Copyright (C) 2012-2019 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.tcc
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{ext/random}
  23. */
  24. #ifndef _EXT_RANDOM_TCC
  25. #define _EXT_RANDOM_TCC 1
  26. #pragma GCC system_header
  27. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  28. {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  31. template<typename _UIntType, size_t __m,
  32. size_t __pos1, size_t __sl1, size_t __sl2,
  33. size_t __sr1, size_t __sr2,
  34. uint32_t __msk1, uint32_t __msk2,
  35. uint32_t __msk3, uint32_t __msk4,
  36. uint32_t __parity1, uint32_t __parity2,
  37. uint32_t __parity3, uint32_t __parity4>
  38. void simd_fast_mersenne_twister_engine<_UIntType, __m,
  39. __pos1, __sl1, __sl2, __sr1, __sr2,
  40. __msk1, __msk2, __msk3, __msk4,
  41. __parity1, __parity2, __parity3,
  42. __parity4>::
  43. seed(_UIntType __seed)
  44. {
  45. _M_state32[0] = static_cast<uint32_t>(__seed);
  46. for (size_t __i = 1; __i < _M_nstate32; ++__i)
  47. _M_state32[__i] = (1812433253UL
  48. * (_M_state32[__i - 1] ^ (_M_state32[__i - 1] >> 30))
  49. + __i);
  50. _M_pos = state_size;
  51. _M_period_certification();
  52. }
  53. namespace {
  54. inline uint32_t _Func1(uint32_t __x)
  55. {
  56. return (__x ^ (__x >> 27)) * UINT32_C(1664525);
  57. }
  58. inline uint32_t _Func2(uint32_t __x)
  59. {
  60. return (__x ^ (__x >> 27)) * UINT32_C(1566083941);
  61. }
  62. }
  63. template<typename _UIntType, size_t __m,
  64. size_t __pos1, size_t __sl1, size_t __sl2,
  65. size_t __sr1, size_t __sr2,
  66. uint32_t __msk1, uint32_t __msk2,
  67. uint32_t __msk3, uint32_t __msk4,
  68. uint32_t __parity1, uint32_t __parity2,
  69. uint32_t __parity3, uint32_t __parity4>
  70. template<typename _Sseq>
  71. auto
  72. simd_fast_mersenne_twister_engine<_UIntType, __m,
  73. __pos1, __sl1, __sl2, __sr1, __sr2,
  74. __msk1, __msk2, __msk3, __msk4,
  75. __parity1, __parity2, __parity3,
  76. __parity4>::
  77. seed(_Sseq& __q)
  78. -> _If_seed_seq<_Sseq>
  79. {
  80. size_t __lag;
  81. if (_M_nstate32 >= 623)
  82. __lag = 11;
  83. else if (_M_nstate32 >= 68)
  84. __lag = 7;
  85. else if (_M_nstate32 >= 39)
  86. __lag = 5;
  87. else
  88. __lag = 3;
  89. const size_t __mid = (_M_nstate32 - __lag) / 2;
  90. std::fill(_M_state32, _M_state32 + _M_nstate32, UINT32_C(0x8b8b8b8b));
  91. uint32_t __arr[_M_nstate32];
  92. __q.generate(__arr + 0, __arr + _M_nstate32);
  93. uint32_t __r = _Func1(_M_state32[0] ^ _M_state32[__mid]
  94. ^ _M_state32[_M_nstate32 - 1]);
  95. _M_state32[__mid] += __r;
  96. __r += _M_nstate32;
  97. _M_state32[__mid + __lag] += __r;
  98. _M_state32[0] = __r;
  99. for (size_t __i = 1, __j = 0; __j < _M_nstate32; ++__j)
  100. {
  101. __r = _Func1(_M_state32[__i]
  102. ^ _M_state32[(__i + __mid) % _M_nstate32]
  103. ^ _M_state32[(__i + _M_nstate32 - 1) % _M_nstate32]);
  104. _M_state32[(__i + __mid) % _M_nstate32] += __r;
  105. __r += __arr[__j] + __i;
  106. _M_state32[(__i + __mid + __lag) % _M_nstate32] += __r;
  107. _M_state32[__i] = __r;
  108. __i = (__i + 1) % _M_nstate32;
  109. }
  110. for (size_t __j = 0; __j < _M_nstate32; ++__j)
  111. {
  112. const size_t __i = (__j + 1) % _M_nstate32;
  113. __r = _Func2(_M_state32[__i]
  114. + _M_state32[(__i + __mid) % _M_nstate32]
  115. + _M_state32[(__i + _M_nstate32 - 1) % _M_nstate32]);
  116. _M_state32[(__i + __mid) % _M_nstate32] ^= __r;
  117. __r -= __i;
  118. _M_state32[(__i + __mid + __lag) % _M_nstate32] ^= __r;
  119. _M_state32[__i] = __r;
  120. }
  121. _M_pos = state_size;
  122. _M_period_certification();
  123. }
  124. template<typename _UIntType, size_t __m,
  125. size_t __pos1, size_t __sl1, size_t __sl2,
  126. size_t __sr1, size_t __sr2,
  127. uint32_t __msk1, uint32_t __msk2,
  128. uint32_t __msk3, uint32_t __msk4,
  129. uint32_t __parity1, uint32_t __parity2,
  130. uint32_t __parity3, uint32_t __parity4>
  131. void simd_fast_mersenne_twister_engine<_UIntType, __m,
  132. __pos1, __sl1, __sl2, __sr1, __sr2,
  133. __msk1, __msk2, __msk3, __msk4,
  134. __parity1, __parity2, __parity3,
  135. __parity4>::
  136. _M_period_certification(void)
  137. {
  138. static const uint32_t __parity[4] = { __parity1, __parity2,
  139. __parity3, __parity4 };
  140. uint32_t __inner = 0;
  141. for (size_t __i = 0; __i < 4; ++__i)
  142. if (__parity[__i] != 0)
  143. __inner ^= _M_state32[__i] & __parity[__i];
  144. if (__builtin_parity(__inner) & 1)
  145. return;
  146. for (size_t __i = 0; __i < 4; ++__i)
  147. if (__parity[__i] != 0)
  148. {
  149. _M_state32[__i] ^= 1 << (__builtin_ffs(__parity[__i]) - 1);
  150. return;
  151. }
  152. __builtin_unreachable();
  153. }
  154. template<typename _UIntType, size_t __m,
  155. size_t __pos1, size_t __sl1, size_t __sl2,
  156. size_t __sr1, size_t __sr2,
  157. uint32_t __msk1, uint32_t __msk2,
  158. uint32_t __msk3, uint32_t __msk4,
  159. uint32_t __parity1, uint32_t __parity2,
  160. uint32_t __parity3, uint32_t __parity4>
  161. void simd_fast_mersenne_twister_engine<_UIntType, __m,
  162. __pos1, __sl1, __sl2, __sr1, __sr2,
  163. __msk1, __msk2, __msk3, __msk4,
  164. __parity1, __parity2, __parity3,
  165. __parity4>::
  166. discard(unsigned long long __z)
  167. {
  168. while (__z > state_size - _M_pos)
  169. {
  170. __z -= state_size - _M_pos;
  171. _M_gen_rand();
  172. }
  173. _M_pos += __z;
  174. }
  175. #ifndef _GLIBCXX_OPT_HAVE_RANDOM_SFMT_GEN_READ
  176. namespace {
  177. template<size_t __shift>
  178. inline void __rshift(uint32_t *__out, const uint32_t *__in)
  179. {
  180. uint64_t __th = ((static_cast<uint64_t>(__in[3]) << 32)
  181. | static_cast<uint64_t>(__in[2]));
  182. uint64_t __tl = ((static_cast<uint64_t>(__in[1]) << 32)
  183. | static_cast<uint64_t>(__in[0]));
  184. uint64_t __oh = __th >> (__shift * 8);
  185. uint64_t __ol = __tl >> (__shift * 8);
  186. __ol |= __th << (64 - __shift * 8);
  187. __out[1] = static_cast<uint32_t>(__ol >> 32);
  188. __out[0] = static_cast<uint32_t>(__ol);
  189. __out[3] = static_cast<uint32_t>(__oh >> 32);
  190. __out[2] = static_cast<uint32_t>(__oh);
  191. }
  192. template<size_t __shift>
  193. inline void __lshift(uint32_t *__out, const uint32_t *__in)
  194. {
  195. uint64_t __th = ((static_cast<uint64_t>(__in[3]) << 32)
  196. | static_cast<uint64_t>(__in[2]));
  197. uint64_t __tl = ((static_cast<uint64_t>(__in[1]) << 32)
  198. | static_cast<uint64_t>(__in[0]));
  199. uint64_t __oh = __th << (__shift * 8);
  200. uint64_t __ol = __tl << (__shift * 8);
  201. __oh |= __tl >> (64 - __shift * 8);
  202. __out[1] = static_cast<uint32_t>(__ol >> 32);
  203. __out[0] = static_cast<uint32_t>(__ol);
  204. __out[3] = static_cast<uint32_t>(__oh >> 32);
  205. __out[2] = static_cast<uint32_t>(__oh);
  206. }
  207. template<size_t __sl1, size_t __sl2, size_t __sr1, size_t __sr2,
  208. uint32_t __msk1, uint32_t __msk2, uint32_t __msk3, uint32_t __msk4>
  209. inline void __recursion(uint32_t *__r,
  210. const uint32_t *__a, const uint32_t *__b,
  211. const uint32_t *__c, const uint32_t *__d)
  212. {
  213. uint32_t __x[4];
  214. uint32_t __y[4];
  215. __lshift<__sl2>(__x, __a);
  216. __rshift<__sr2>(__y, __c);
  217. __r[0] = (__a[0] ^ __x[0] ^ ((__b[0] >> __sr1) & __msk1)
  218. ^ __y[0] ^ (__d[0] << __sl1));
  219. __r[1] = (__a[1] ^ __x[1] ^ ((__b[1] >> __sr1) & __msk2)
  220. ^ __y[1] ^ (__d[1] << __sl1));
  221. __r[2] = (__a[2] ^ __x[2] ^ ((__b[2] >> __sr1) & __msk3)
  222. ^ __y[2] ^ (__d[2] << __sl1));
  223. __r[3] = (__a[3] ^ __x[3] ^ ((__b[3] >> __sr1) & __msk4)
  224. ^ __y[3] ^ (__d[3] << __sl1));
  225. }
  226. }
  227. template<typename _UIntType, size_t __m,
  228. size_t __pos1, size_t __sl1, size_t __sl2,
  229. size_t __sr1, size_t __sr2,
  230. uint32_t __msk1, uint32_t __msk2,
  231. uint32_t __msk3, uint32_t __msk4,
  232. uint32_t __parity1, uint32_t __parity2,
  233. uint32_t __parity3, uint32_t __parity4>
  234. void simd_fast_mersenne_twister_engine<_UIntType, __m,
  235. __pos1, __sl1, __sl2, __sr1, __sr2,
  236. __msk1, __msk2, __msk3, __msk4,
  237. __parity1, __parity2, __parity3,
  238. __parity4>::
  239. _M_gen_rand(void)
  240. {
  241. const uint32_t *__r1 = &_M_state32[_M_nstate32 - 8];
  242. const uint32_t *__r2 = &_M_state32[_M_nstate32 - 4];
  243. static constexpr size_t __pos1_32 = __pos1 * 4;
  244. size_t __i;
  245. for (__i = 0; __i < _M_nstate32 - __pos1_32; __i += 4)
  246. {
  247. __recursion<__sl1, __sl2, __sr1, __sr2,
  248. __msk1, __msk2, __msk3, __msk4>
  249. (&_M_state32[__i], &_M_state32[__i],
  250. &_M_state32[__i + __pos1_32], __r1, __r2);
  251. __r1 = __r2;
  252. __r2 = &_M_state32[__i];
  253. }
  254. for (; __i < _M_nstate32; __i += 4)
  255. {
  256. __recursion<__sl1, __sl2, __sr1, __sr2,
  257. __msk1, __msk2, __msk3, __msk4>
  258. (&_M_state32[__i], &_M_state32[__i],
  259. &_M_state32[__i + __pos1_32 - _M_nstate32], __r1, __r2);
  260. __r1 = __r2;
  261. __r2 = &_M_state32[__i];
  262. }
  263. _M_pos = 0;
  264. }
  265. #endif
  266. #ifndef _GLIBCXX_OPT_HAVE_RANDOM_SFMT_OPERATOREQUAL
  267. template<typename _UIntType, size_t __m,
  268. size_t __pos1, size_t __sl1, size_t __sl2,
  269. size_t __sr1, size_t __sr2,
  270. uint32_t __msk1, uint32_t __msk2,
  271. uint32_t __msk3, uint32_t __msk4,
  272. uint32_t __parity1, uint32_t __parity2,
  273. uint32_t __parity3, uint32_t __parity4>
  274. bool
  275. operator==(const __gnu_cxx::simd_fast_mersenne_twister_engine<_UIntType,
  276. __m, __pos1, __sl1, __sl2, __sr1, __sr2,
  277. __msk1, __msk2, __msk3, __msk4,
  278. __parity1, __parity2, __parity3, __parity4>& __lhs,
  279. const __gnu_cxx::simd_fast_mersenne_twister_engine<_UIntType,
  280. __m, __pos1, __sl1, __sl2, __sr1, __sr2,
  281. __msk1, __msk2, __msk3, __msk4,
  282. __parity1, __parity2, __parity3, __parity4>& __rhs)
  283. {
  284. typedef __gnu_cxx::simd_fast_mersenne_twister_engine<_UIntType,
  285. __m, __pos1, __sl1, __sl2, __sr1, __sr2,
  286. __msk1, __msk2, __msk3, __msk4,
  287. __parity1, __parity2, __parity3, __parity4> __engine;
  288. return (std::equal(__lhs._M_stateT,
  289. __lhs._M_stateT + __engine::state_size,
  290. __rhs._M_stateT)
  291. && __lhs._M_pos == __rhs._M_pos);
  292. }
  293. #endif
  294. template<typename _UIntType, size_t __m,
  295. size_t __pos1, size_t __sl1, size_t __sl2,
  296. size_t __sr1, size_t __sr2,
  297. uint32_t __msk1, uint32_t __msk2,
  298. uint32_t __msk3, uint32_t __msk4,
  299. uint32_t __parity1, uint32_t __parity2,
  300. uint32_t __parity3, uint32_t __parity4,
  301. typename _CharT, typename _Traits>
  302. std::basic_ostream<_CharT, _Traits>&
  303. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  304. const __gnu_cxx::simd_fast_mersenne_twister_engine<_UIntType,
  305. __m, __pos1, __sl1, __sl2, __sr1, __sr2,
  306. __msk1, __msk2, __msk3, __msk4,
  307. __parity1, __parity2, __parity3, __parity4>& __x)
  308. {
  309. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  310. typedef typename __ostream_type::ios_base __ios_base;
  311. const typename __ios_base::fmtflags __flags = __os.flags();
  312. const _CharT __fill = __os.fill();
  313. const _CharT __space = __os.widen(' ');
  314. __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left);
  315. __os.fill(__space);
  316. for (size_t __i = 0; __i < __x._M_nstate32; ++__i)
  317. __os << __x._M_state32[__i] << __space;
  318. __os << __x._M_pos;
  319. __os.flags(__flags);
  320. __os.fill(__fill);
  321. return __os;
  322. }
  323. template<typename _UIntType, size_t __m,
  324. size_t __pos1, size_t __sl1, size_t __sl2,
  325. size_t __sr1, size_t __sr2,
  326. uint32_t __msk1, uint32_t __msk2,
  327. uint32_t __msk3, uint32_t __msk4,
  328. uint32_t __parity1, uint32_t __parity2,
  329. uint32_t __parity3, uint32_t __parity4,
  330. typename _CharT, typename _Traits>
  331. std::basic_istream<_CharT, _Traits>&
  332. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  333. __gnu_cxx::simd_fast_mersenne_twister_engine<_UIntType,
  334. __m, __pos1, __sl1, __sl2, __sr1, __sr2,
  335. __msk1, __msk2, __msk3, __msk4,
  336. __parity1, __parity2, __parity3, __parity4>& __x)
  337. {
  338. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  339. typedef typename __istream_type::ios_base __ios_base;
  340. const typename __ios_base::fmtflags __flags = __is.flags();
  341. __is.flags(__ios_base::dec | __ios_base::skipws);
  342. for (size_t __i = 0; __i < __x._M_nstate32; ++__i)
  343. __is >> __x._M_state32[__i];
  344. __is >> __x._M_pos;
  345. __is.flags(__flags);
  346. return __is;
  347. }
  348. #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  349. /**
  350. * Iteration method due to M.D. J<o:>hnk.
  351. *
  352. * M.D. J<o:>hnk, Erzeugung von betaverteilten und gammaverteilten
  353. * Zufallszahlen, Metrika, Volume 8, 1964
  354. */
  355. template<typename _RealType>
  356. template<typename _UniformRandomNumberGenerator>
  357. typename beta_distribution<_RealType>::result_type
  358. beta_distribution<_RealType>::
  359. operator()(_UniformRandomNumberGenerator& __urng,
  360. const param_type& __param)
  361. {
  362. std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  363. __aurng(__urng);
  364. result_type __x, __y;
  365. do
  366. {
  367. __x = std::exp(std::log(__aurng()) / __param.alpha());
  368. __y = std::exp(std::log(__aurng()) / __param.beta());
  369. }
  370. while (__x + __y > result_type(1));
  371. return __x / (__x + __y);
  372. }
  373. template<typename _RealType>
  374. template<typename _OutputIterator,
  375. typename _UniformRandomNumberGenerator>
  376. void
  377. beta_distribution<_RealType>::
  378. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  379. _UniformRandomNumberGenerator& __urng,
  380. const param_type& __param)
  381. {
  382. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  383. result_type>)
  384. std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  385. __aurng(__urng);
  386. while (__f != __t)
  387. {
  388. result_type __x, __y;
  389. do
  390. {
  391. __x = std::exp(std::log(__aurng()) / __param.alpha());
  392. __y = std::exp(std::log(__aurng()) / __param.beta());
  393. }
  394. while (__x + __y > result_type(1));
  395. *__f++ = __x / (__x + __y);
  396. }
  397. }
  398. template<typename _RealType, typename _CharT, typename _Traits>
  399. std::basic_ostream<_CharT, _Traits>&
  400. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  401. const __gnu_cxx::beta_distribution<_RealType>& __x)
  402. {
  403. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  404. typedef typename __ostream_type::ios_base __ios_base;
  405. const typename __ios_base::fmtflags __flags = __os.flags();
  406. const _CharT __fill = __os.fill();
  407. const std::streamsize __precision = __os.precision();
  408. const _CharT __space = __os.widen(' ');
  409. __os.flags(__ios_base::scientific | __ios_base::left);
  410. __os.fill(__space);
  411. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  412. __os << __x.alpha() << __space << __x.beta();
  413. __os.flags(__flags);
  414. __os.fill(__fill);
  415. __os.precision(__precision);
  416. return __os;
  417. }
  418. template<typename _RealType, typename _CharT, typename _Traits>
  419. std::basic_istream<_CharT, _Traits>&
  420. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  421. __gnu_cxx::beta_distribution<_RealType>& __x)
  422. {
  423. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  424. typedef typename __istream_type::ios_base __ios_base;
  425. const typename __ios_base::fmtflags __flags = __is.flags();
  426. __is.flags(__ios_base::dec | __ios_base::skipws);
  427. _RealType __alpha_val, __beta_val;
  428. __is >> __alpha_val >> __beta_val;
  429. __x.param(typename __gnu_cxx::beta_distribution<_RealType>::
  430. param_type(__alpha_val, __beta_val));
  431. __is.flags(__flags);
  432. return __is;
  433. }
  434. template<std::size_t _Dimen, typename _RealType>
  435. template<typename _InputIterator1, typename _InputIterator2>
  436. void
  437. normal_mv_distribution<_Dimen, _RealType>::param_type::
  438. _M_init_full(_InputIterator1 __meanbegin, _InputIterator1 __meanend,
  439. _InputIterator2 __varcovbegin, _InputIterator2 __varcovend)
  440. {
  441. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  442. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  443. std::fill(std::copy(__meanbegin, __meanend, _M_mean.begin()),
  444. _M_mean.end(), _RealType(0));
  445. // Perform the Cholesky decomposition
  446. auto __w = _M_t.begin();
  447. for (size_t __j = 0; __j < _Dimen; ++__j)
  448. {
  449. _RealType __sum = _RealType(0);
  450. auto __slitbegin = __w;
  451. auto __cit = _M_t.begin();
  452. for (size_t __i = 0; __i < __j; ++__i)
  453. {
  454. auto __slit = __slitbegin;
  455. _RealType __s = *__varcovbegin++;
  456. for (size_t __k = 0; __k < __i; ++__k)
  457. __s -= *__slit++ * *__cit++;
  458. *__w++ = __s /= *__cit++;
  459. __sum += __s * __s;
  460. }
  461. __sum = *__varcovbegin - __sum;
  462. if (__builtin_expect(__sum <= _RealType(0), 0))
  463. std::__throw_runtime_error(__N("normal_mv_distribution::"
  464. "param_type::_M_init_full"));
  465. *__w++ = std::sqrt(__sum);
  466. std::advance(__varcovbegin, _Dimen - __j);
  467. }
  468. }
  469. template<std::size_t _Dimen, typename _RealType>
  470. template<typename _InputIterator1, typename _InputIterator2>
  471. void
  472. normal_mv_distribution<_Dimen, _RealType>::param_type::
  473. _M_init_lower(_InputIterator1 __meanbegin, _InputIterator1 __meanend,
  474. _InputIterator2 __varcovbegin, _InputIterator2 __varcovend)
  475. {
  476. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  477. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  478. std::fill(std::copy(__meanbegin, __meanend, _M_mean.begin()),
  479. _M_mean.end(), _RealType(0));
  480. // Perform the Cholesky decomposition
  481. auto __w = _M_t.begin();
  482. for (size_t __j = 0; __j < _Dimen; ++__j)
  483. {
  484. _RealType __sum = _RealType(0);
  485. auto __slitbegin = __w;
  486. auto __cit = _M_t.begin();
  487. for (size_t __i = 0; __i < __j; ++__i)
  488. {
  489. auto __slit = __slitbegin;
  490. _RealType __s = *__varcovbegin++;
  491. for (size_t __k = 0; __k < __i; ++__k)
  492. __s -= *__slit++ * *__cit++;
  493. *__w++ = __s /= *__cit++;
  494. __sum += __s * __s;
  495. }
  496. __sum = *__varcovbegin++ - __sum;
  497. if (__builtin_expect(__sum <= _RealType(0), 0))
  498. std::__throw_runtime_error(__N("normal_mv_distribution::"
  499. "param_type::_M_init_full"));
  500. *__w++ = std::sqrt(__sum);
  501. }
  502. }
  503. template<std::size_t _Dimen, typename _RealType>
  504. template<typename _InputIterator1, typename _InputIterator2>
  505. void
  506. normal_mv_distribution<_Dimen, _RealType>::param_type::
  507. _M_init_diagonal(_InputIterator1 __meanbegin, _InputIterator1 __meanend,
  508. _InputIterator2 __varbegin, _InputIterator2 __varend)
  509. {
  510. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  511. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  512. std::fill(std::copy(__meanbegin, __meanend, _M_mean.begin()),
  513. _M_mean.end(), _RealType(0));
  514. auto __w = _M_t.begin();
  515. size_t __step = 0;
  516. while (__varbegin != __varend)
  517. {
  518. std::fill_n(__w, __step, _RealType(0));
  519. __w += __step++;
  520. if (__builtin_expect(*__varbegin < _RealType(0), 0))
  521. std::__throw_runtime_error(__N("normal_mv_distribution::"
  522. "param_type::_M_init_diagonal"));
  523. *__w++ = std::sqrt(*__varbegin++);
  524. }
  525. }
  526. template<std::size_t _Dimen, typename _RealType>
  527. template<typename _UniformRandomNumberGenerator>
  528. typename normal_mv_distribution<_Dimen, _RealType>::result_type
  529. normal_mv_distribution<_Dimen, _RealType>::
  530. operator()(_UniformRandomNumberGenerator& __urng,
  531. const param_type& __param)
  532. {
  533. result_type __ret;
  534. _M_nd.__generate(__ret.begin(), __ret.end(), __urng);
  535. auto __t_it = __param._M_t.crbegin();
  536. for (size_t __i = _Dimen; __i > 0; --__i)
  537. {
  538. _RealType __sum = _RealType(0);
  539. for (size_t __j = __i; __j > 0; --__j)
  540. __sum += __ret[__j - 1] * *__t_it++;
  541. __ret[__i - 1] = __sum;
  542. }
  543. return __ret;
  544. }
  545. template<std::size_t _Dimen, typename _RealType>
  546. template<typename _ForwardIterator, typename _UniformRandomNumberGenerator>
  547. void
  548. normal_mv_distribution<_Dimen, _RealType>::
  549. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  550. _UniformRandomNumberGenerator& __urng,
  551. const param_type& __param)
  552. {
  553. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  554. _ForwardIterator>)
  555. while (__f != __t)
  556. *__f++ = this->operator()(__urng, __param);
  557. }
  558. template<size_t _Dimen, typename _RealType>
  559. bool
  560. operator==(const __gnu_cxx::normal_mv_distribution<_Dimen, _RealType>&
  561. __d1,
  562. const __gnu_cxx::normal_mv_distribution<_Dimen, _RealType>&
  563. __d2)
  564. {
  565. return __d1._M_param == __d2._M_param && __d1._M_nd == __d2._M_nd;
  566. }
  567. template<size_t _Dimen, typename _RealType, typename _CharT, typename _Traits>
  568. std::basic_ostream<_CharT, _Traits>&
  569. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  570. const __gnu_cxx::normal_mv_distribution<_Dimen, _RealType>& __x)
  571. {
  572. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  573. typedef typename __ostream_type::ios_base __ios_base;
  574. const typename __ios_base::fmtflags __flags = __os.flags();
  575. const _CharT __fill = __os.fill();
  576. const std::streamsize __precision = __os.precision();
  577. const _CharT __space = __os.widen(' ');
  578. __os.flags(__ios_base::scientific | __ios_base::left);
  579. __os.fill(__space);
  580. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  581. auto __mean = __x._M_param.mean();
  582. for (auto __it : __mean)
  583. __os << __it << __space;
  584. auto __t = __x._M_param.varcov();
  585. for (auto __it : __t)
  586. __os << __it << __space;
  587. __os << __x._M_nd;
  588. __os.flags(__flags);
  589. __os.fill(__fill);
  590. __os.precision(__precision);
  591. return __os;
  592. }
  593. template<size_t _Dimen, typename _RealType, typename _CharT, typename _Traits>
  594. std::basic_istream<_CharT, _Traits>&
  595. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  596. __gnu_cxx::normal_mv_distribution<_Dimen, _RealType>& __x)
  597. {
  598. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  599. typedef typename __istream_type::ios_base __ios_base;
  600. const typename __ios_base::fmtflags __flags = __is.flags();
  601. __is.flags(__ios_base::dec | __ios_base::skipws);
  602. std::array<_RealType, _Dimen> __mean;
  603. for (auto& __it : __mean)
  604. __is >> __it;
  605. std::array<_RealType, _Dimen * (_Dimen + 1) / 2> __varcov;
  606. for (auto& __it : __varcov)
  607. __is >> __it;
  608. __is >> __x._M_nd;
  609. __x.param(typename normal_mv_distribution<_Dimen, _RealType>::
  610. param_type(__mean.begin(), __mean.end(),
  611. __varcov.begin(), __varcov.end()));
  612. __is.flags(__flags);
  613. return __is;
  614. }
  615. template<typename _RealType>
  616. template<typename _OutputIterator,
  617. typename _UniformRandomNumberGenerator>
  618. void
  619. rice_distribution<_RealType>::
  620. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  621. _UniformRandomNumberGenerator& __urng,
  622. const param_type& __p)
  623. {
  624. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  625. result_type>)
  626. while (__f != __t)
  627. {
  628. typename std::normal_distribution<result_type>::param_type
  629. __px(__p.nu(), __p.sigma()), __py(result_type(0), __p.sigma());
  630. result_type __x = this->_M_ndx(__px, __urng);
  631. result_type __y = this->_M_ndy(__py, __urng);
  632. #if _GLIBCXX_USE_C99_MATH_TR1
  633. *__f++ = std::hypot(__x, __y);
  634. #else
  635. *__f++ = std::sqrt(__x * __x + __y * __y);
  636. #endif
  637. }
  638. }
  639. template<typename _RealType, typename _CharT, typename _Traits>
  640. std::basic_ostream<_CharT, _Traits>&
  641. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  642. const rice_distribution<_RealType>& __x)
  643. {
  644. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  645. typedef typename __ostream_type::ios_base __ios_base;
  646. const typename __ios_base::fmtflags __flags = __os.flags();
  647. const _CharT __fill = __os.fill();
  648. const std::streamsize __precision = __os.precision();
  649. const _CharT __space = __os.widen(' ');
  650. __os.flags(__ios_base::scientific | __ios_base::left);
  651. __os.fill(__space);
  652. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  653. __os << __x.nu() << __space << __x.sigma();
  654. __os << __space << __x._M_ndx;
  655. __os << __space << __x._M_ndy;
  656. __os.flags(__flags);
  657. __os.fill(__fill);
  658. __os.precision(__precision);
  659. return __os;
  660. }
  661. template<typename _RealType, typename _CharT, typename _Traits>
  662. std::basic_istream<_CharT, _Traits>&
  663. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  664. rice_distribution<_RealType>& __x)
  665. {
  666. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  667. typedef typename __istream_type::ios_base __ios_base;
  668. const typename __ios_base::fmtflags __flags = __is.flags();
  669. __is.flags(__ios_base::dec | __ios_base::skipws);
  670. _RealType __nu_val, __sigma_val;
  671. __is >> __nu_val >> __sigma_val;
  672. __is >> __x._M_ndx;
  673. __is >> __x._M_ndy;
  674. __x.param(typename rice_distribution<_RealType>::
  675. param_type(__nu_val, __sigma_val));
  676. __is.flags(__flags);
  677. return __is;
  678. }
  679. template<typename _RealType>
  680. template<typename _OutputIterator,
  681. typename _UniformRandomNumberGenerator>
  682. void
  683. nakagami_distribution<_RealType>::
  684. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  685. _UniformRandomNumberGenerator& __urng,
  686. const param_type& __p)
  687. {
  688. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  689. result_type>)
  690. typename std::gamma_distribution<result_type>::param_type
  691. __pg(__p.mu(), __p.omega() / __p.mu());
  692. while (__f != __t)
  693. *__f++ = std::sqrt(this->_M_gd(__pg, __urng));
  694. }
  695. template<typename _RealType, typename _CharT, typename _Traits>
  696. std::basic_ostream<_CharT, _Traits>&
  697. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  698. const nakagami_distribution<_RealType>& __x)
  699. {
  700. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  701. typedef typename __ostream_type::ios_base __ios_base;
  702. const typename __ios_base::fmtflags __flags = __os.flags();
  703. const _CharT __fill = __os.fill();
  704. const std::streamsize __precision = __os.precision();
  705. const _CharT __space = __os.widen(' ');
  706. __os.flags(__ios_base::scientific | __ios_base::left);
  707. __os.fill(__space);
  708. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  709. __os << __x.mu() << __space << __x.omega();
  710. __os << __space << __x._M_gd;
  711. __os.flags(__flags);
  712. __os.fill(__fill);
  713. __os.precision(__precision);
  714. return __os;
  715. }
  716. template<typename _RealType, typename _CharT, typename _Traits>
  717. std::basic_istream<_CharT, _Traits>&
  718. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  719. nakagami_distribution<_RealType>& __x)
  720. {
  721. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  722. typedef typename __istream_type::ios_base __ios_base;
  723. const typename __ios_base::fmtflags __flags = __is.flags();
  724. __is.flags(__ios_base::dec | __ios_base::skipws);
  725. _RealType __mu_val, __omega_val;
  726. __is >> __mu_val >> __omega_val;
  727. __is >> __x._M_gd;
  728. __x.param(typename nakagami_distribution<_RealType>::
  729. param_type(__mu_val, __omega_val));
  730. __is.flags(__flags);
  731. return __is;
  732. }
  733. template<typename _RealType>
  734. template<typename _OutputIterator,
  735. typename _UniformRandomNumberGenerator>
  736. void
  737. pareto_distribution<_RealType>::
  738. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  739. _UniformRandomNumberGenerator& __urng,
  740. const param_type& __p)
  741. {
  742. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  743. result_type>)
  744. result_type __mu_val = __p.mu();
  745. result_type __malphinv = -result_type(1) / __p.alpha();
  746. while (__f != __t)
  747. *__f++ = __mu_val * std::pow(this->_M_ud(__urng), __malphinv);
  748. }
  749. template<typename _RealType, typename _CharT, typename _Traits>
  750. std::basic_ostream<_CharT, _Traits>&
  751. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  752. const pareto_distribution<_RealType>& __x)
  753. {
  754. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  755. typedef typename __ostream_type::ios_base __ios_base;
  756. const typename __ios_base::fmtflags __flags = __os.flags();
  757. const _CharT __fill = __os.fill();
  758. const std::streamsize __precision = __os.precision();
  759. const _CharT __space = __os.widen(' ');
  760. __os.flags(__ios_base::scientific | __ios_base::left);
  761. __os.fill(__space);
  762. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  763. __os << __x.alpha() << __space << __x.mu();
  764. __os << __space << __x._M_ud;
  765. __os.flags(__flags);
  766. __os.fill(__fill);
  767. __os.precision(__precision);
  768. return __os;
  769. }
  770. template<typename _RealType, typename _CharT, typename _Traits>
  771. std::basic_istream<_CharT, _Traits>&
  772. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  773. pareto_distribution<_RealType>& __x)
  774. {
  775. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  776. typedef typename __istream_type::ios_base __ios_base;
  777. const typename __ios_base::fmtflags __flags = __is.flags();
  778. __is.flags(__ios_base::dec | __ios_base::skipws);
  779. _RealType __alpha_val, __mu_val;
  780. __is >> __alpha_val >> __mu_val;
  781. __is >> __x._M_ud;
  782. __x.param(typename pareto_distribution<_RealType>::
  783. param_type(__alpha_val, __mu_val));
  784. __is.flags(__flags);
  785. return __is;
  786. }
  787. template<typename _RealType>
  788. template<typename _UniformRandomNumberGenerator>
  789. typename k_distribution<_RealType>::result_type
  790. k_distribution<_RealType>::
  791. operator()(_UniformRandomNumberGenerator& __urng)
  792. {
  793. result_type __x = this->_M_gd1(__urng);
  794. result_type __y = this->_M_gd2(__urng);
  795. return std::sqrt(__x * __y);
  796. }
  797. template<typename _RealType>
  798. template<typename _UniformRandomNumberGenerator>
  799. typename k_distribution<_RealType>::result_type
  800. k_distribution<_RealType>::
  801. operator()(_UniformRandomNumberGenerator& __urng,
  802. const param_type& __p)
  803. {
  804. typename std::gamma_distribution<result_type>::param_type
  805. __p1(__p.lambda(), result_type(1) / __p.lambda()),
  806. __p2(__p.nu(), __p.mu() / __p.nu());
  807. result_type __x = this->_M_gd1(__p1, __urng);
  808. result_type __y = this->_M_gd2(__p2, __urng);
  809. return std::sqrt(__x * __y);
  810. }
  811. template<typename _RealType>
  812. template<typename _OutputIterator,
  813. typename _UniformRandomNumberGenerator>
  814. void
  815. k_distribution<_RealType>::
  816. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  817. _UniformRandomNumberGenerator& __urng,
  818. const param_type& __p)
  819. {
  820. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  821. result_type>)
  822. typename std::gamma_distribution<result_type>::param_type
  823. __p1(__p.lambda(), result_type(1) / __p.lambda()),
  824. __p2(__p.nu(), __p.mu() / __p.nu());
  825. while (__f != __t)
  826. {
  827. result_type __x = this->_M_gd1(__p1, __urng);
  828. result_type __y = this->_M_gd2(__p2, __urng);
  829. *__f++ = std::sqrt(__x * __y);
  830. }
  831. }
  832. template<typename _RealType, typename _CharT, typename _Traits>
  833. std::basic_ostream<_CharT, _Traits>&
  834. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  835. const k_distribution<_RealType>& __x)
  836. {
  837. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  838. typedef typename __ostream_type::ios_base __ios_base;
  839. const typename __ios_base::fmtflags __flags = __os.flags();
  840. const _CharT __fill = __os.fill();
  841. const std::streamsize __precision = __os.precision();
  842. const _CharT __space = __os.widen(' ');
  843. __os.flags(__ios_base::scientific | __ios_base::left);
  844. __os.fill(__space);
  845. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  846. __os << __x.lambda() << __space << __x.mu() << __space << __x.nu();
  847. __os << __space << __x._M_gd1;
  848. __os << __space << __x._M_gd2;
  849. __os.flags(__flags);
  850. __os.fill(__fill);
  851. __os.precision(__precision);
  852. return __os;
  853. }
  854. template<typename _RealType, typename _CharT, typename _Traits>
  855. std::basic_istream<_CharT, _Traits>&
  856. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  857. k_distribution<_RealType>& __x)
  858. {
  859. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  860. typedef typename __istream_type::ios_base __ios_base;
  861. const typename __ios_base::fmtflags __flags = __is.flags();
  862. __is.flags(__ios_base::dec | __ios_base::skipws);
  863. _RealType __lambda_val, __mu_val, __nu_val;
  864. __is >> __lambda_val >> __mu_val >> __nu_val;
  865. __is >> __x._M_gd1;
  866. __is >> __x._M_gd2;
  867. __x.param(typename k_distribution<_RealType>::
  868. param_type(__lambda_val, __mu_val, __nu_val));
  869. __is.flags(__flags);
  870. return __is;
  871. }
  872. template<typename _RealType>
  873. template<typename _OutputIterator,
  874. typename _UniformRandomNumberGenerator>
  875. void
  876. arcsine_distribution<_RealType>::
  877. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  878. _UniformRandomNumberGenerator& __urng,
  879. const param_type& __p)
  880. {
  881. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  882. result_type>)
  883. result_type __dif = __p.b() - __p.a();
  884. result_type __sum = __p.a() + __p.b();
  885. while (__f != __t)
  886. {
  887. result_type __x = std::sin(this->_M_ud(__urng));
  888. *__f++ = (__x * __dif + __sum) / result_type(2);
  889. }
  890. }
  891. template<typename _RealType, typename _CharT, typename _Traits>
  892. std::basic_ostream<_CharT, _Traits>&
  893. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  894. const arcsine_distribution<_RealType>& __x)
  895. {
  896. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  897. typedef typename __ostream_type::ios_base __ios_base;
  898. const typename __ios_base::fmtflags __flags = __os.flags();
  899. const _CharT __fill = __os.fill();
  900. const std::streamsize __precision = __os.precision();
  901. const _CharT __space = __os.widen(' ');
  902. __os.flags(__ios_base::scientific | __ios_base::left);
  903. __os.fill(__space);
  904. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  905. __os << __x.a() << __space << __x.b();
  906. __os << __space << __x._M_ud;
  907. __os.flags(__flags);
  908. __os.fill(__fill);
  909. __os.precision(__precision);
  910. return __os;
  911. }
  912. template<typename _RealType, typename _CharT, typename _Traits>
  913. std::basic_istream<_CharT, _Traits>&
  914. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  915. arcsine_distribution<_RealType>& __x)
  916. {
  917. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  918. typedef typename __istream_type::ios_base __ios_base;
  919. const typename __ios_base::fmtflags __flags = __is.flags();
  920. __is.flags(__ios_base::dec | __ios_base::skipws);
  921. _RealType __a, __b;
  922. __is >> __a >> __b;
  923. __is >> __x._M_ud;
  924. __x.param(typename arcsine_distribution<_RealType>::
  925. param_type(__a, __b));
  926. __is.flags(__flags);
  927. return __is;
  928. }
  929. template<typename _RealType>
  930. template<typename _UniformRandomNumberGenerator>
  931. typename hoyt_distribution<_RealType>::result_type
  932. hoyt_distribution<_RealType>::
  933. operator()(_UniformRandomNumberGenerator& __urng)
  934. {
  935. result_type __x = this->_M_ad(__urng);
  936. result_type __y = this->_M_ed(__urng);
  937. return (result_type(2) * this->q()
  938. / (result_type(1) + this->q() * this->q()))
  939. * std::sqrt(this->omega() * __x * __y);
  940. }
  941. template<typename _RealType>
  942. template<typename _UniformRandomNumberGenerator>
  943. typename hoyt_distribution<_RealType>::result_type
  944. hoyt_distribution<_RealType>::
  945. operator()(_UniformRandomNumberGenerator& __urng,
  946. const param_type& __p)
  947. {
  948. result_type __q2 = __p.q() * __p.q();
  949. result_type __num = result_type(0.5L) * (result_type(1) + __q2);
  950. typename __gnu_cxx::arcsine_distribution<result_type>::param_type
  951. __pa(__num, __num / __q2);
  952. result_type __x = this->_M_ad(__pa, __urng);
  953. result_type __y = this->_M_ed(__urng);
  954. return (result_type(2) * __p.q() / (result_type(1) + __q2))
  955. * std::sqrt(__p.omega() * __x * __y);
  956. }
  957. template<typename _RealType>
  958. template<typename _OutputIterator,
  959. typename _UniformRandomNumberGenerator>
  960. void
  961. hoyt_distribution<_RealType>::
  962. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  963. _UniformRandomNumberGenerator& __urng,
  964. const param_type& __p)
  965. {
  966. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  967. result_type>)
  968. result_type __2q = result_type(2) * __p.q();
  969. result_type __q2 = __p.q() * __p.q();
  970. result_type __q2p1 = result_type(1) + __q2;
  971. result_type __num = result_type(0.5L) * __q2p1;
  972. result_type __omega = __p.omega();
  973. typename __gnu_cxx::arcsine_distribution<result_type>::param_type
  974. __pa(__num, __num / __q2);
  975. while (__f != __t)
  976. {
  977. result_type __x = this->_M_ad(__pa, __urng);
  978. result_type __y = this->_M_ed(__urng);
  979. *__f++ = (__2q / __q2p1) * std::sqrt(__omega * __x * __y);
  980. }
  981. }
  982. template<typename _RealType, typename _CharT, typename _Traits>
  983. std::basic_ostream<_CharT, _Traits>&
  984. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  985. const hoyt_distribution<_RealType>& __x)
  986. {
  987. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  988. typedef typename __ostream_type::ios_base __ios_base;
  989. const typename __ios_base::fmtflags __flags = __os.flags();
  990. const _CharT __fill = __os.fill();
  991. const std::streamsize __precision = __os.precision();
  992. const _CharT __space = __os.widen(' ');
  993. __os.flags(__ios_base::scientific | __ios_base::left);
  994. __os.fill(__space);
  995. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  996. __os << __x.q() << __space << __x.omega();
  997. __os << __space << __x._M_ad;
  998. __os << __space << __x._M_ed;
  999. __os.flags(__flags);
  1000. __os.fill(__fill);
  1001. __os.precision(__precision);
  1002. return __os;
  1003. }
  1004. template<typename _RealType, typename _CharT, typename _Traits>
  1005. std::basic_istream<_CharT, _Traits>&
  1006. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1007. hoyt_distribution<_RealType>& __x)
  1008. {
  1009. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1010. typedef typename __istream_type::ios_base __ios_base;
  1011. const typename __ios_base::fmtflags __flags = __is.flags();
  1012. __is.flags(__ios_base::dec | __ios_base::skipws);
  1013. _RealType __q, __omega;
  1014. __is >> __q >> __omega;
  1015. __is >> __x._M_ad;
  1016. __is >> __x._M_ed;
  1017. __x.param(typename hoyt_distribution<_RealType>::
  1018. param_type(__q, __omega));
  1019. __is.flags(__flags);
  1020. return __is;
  1021. }
  1022. template<typename _RealType>
  1023. template<typename _OutputIterator,
  1024. typename _UniformRandomNumberGenerator>
  1025. void
  1026. triangular_distribution<_RealType>::
  1027. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  1028. _UniformRandomNumberGenerator& __urng,
  1029. const param_type& __param)
  1030. {
  1031. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  1032. result_type>)
  1033. while (__f != __t)
  1034. *__f++ = this->operator()(__urng, __param);
  1035. }
  1036. template<typename _RealType, typename _CharT, typename _Traits>
  1037. std::basic_ostream<_CharT, _Traits>&
  1038. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1039. const __gnu_cxx::triangular_distribution<_RealType>& __x)
  1040. {
  1041. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1042. typedef typename __ostream_type::ios_base __ios_base;
  1043. const typename __ios_base::fmtflags __flags = __os.flags();
  1044. const _CharT __fill = __os.fill();
  1045. const std::streamsize __precision = __os.precision();
  1046. const _CharT __space = __os.widen(' ');
  1047. __os.flags(__ios_base::scientific | __ios_base::left);
  1048. __os.fill(__space);
  1049. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1050. __os << __x.a() << __space << __x.b() << __space << __x.c();
  1051. __os.flags(__flags);
  1052. __os.fill(__fill);
  1053. __os.precision(__precision);
  1054. return __os;
  1055. }
  1056. template<typename _RealType, typename _CharT, typename _Traits>
  1057. std::basic_istream<_CharT, _Traits>&
  1058. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1059. __gnu_cxx::triangular_distribution<_RealType>& __x)
  1060. {
  1061. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1062. typedef typename __istream_type::ios_base __ios_base;
  1063. const typename __ios_base::fmtflags __flags = __is.flags();
  1064. __is.flags(__ios_base::dec | __ios_base::skipws);
  1065. _RealType __a, __b, __c;
  1066. __is >> __a >> __b >> __c;
  1067. __x.param(typename __gnu_cxx::triangular_distribution<_RealType>::
  1068. param_type(__a, __b, __c));
  1069. __is.flags(__flags);
  1070. return __is;
  1071. }
  1072. template<typename _RealType>
  1073. template<typename _UniformRandomNumberGenerator>
  1074. typename von_mises_distribution<_RealType>::result_type
  1075. von_mises_distribution<_RealType>::
  1076. operator()(_UniformRandomNumberGenerator& __urng,
  1077. const param_type& __p)
  1078. {
  1079. const result_type __pi
  1080. = __gnu_cxx::__math_constants<result_type>::__pi;
  1081. std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1082. __aurng(__urng);
  1083. result_type __f;
  1084. while (1)
  1085. {
  1086. result_type __rnd = std::cos(__pi * __aurng());
  1087. __f = (result_type(1) + __p._M_r * __rnd) / (__p._M_r + __rnd);
  1088. result_type __c = __p._M_kappa * (__p._M_r - __f);
  1089. result_type __rnd2 = __aurng();
  1090. if (__c * (result_type(2) - __c) > __rnd2)
  1091. break;
  1092. if (std::log(__c / __rnd2) >= __c - result_type(1))
  1093. break;
  1094. }
  1095. result_type __res = std::acos(__f);
  1096. #if _GLIBCXX_USE_C99_MATH_TR1
  1097. __res = std::copysign(__res, __aurng() - result_type(0.5));
  1098. #else
  1099. if (__aurng() < result_type(0.5))
  1100. __res = -__res;
  1101. #endif
  1102. __res += __p._M_mu;
  1103. if (__res > __pi)
  1104. __res -= result_type(2) * __pi;
  1105. else if (__res < -__pi)
  1106. __res += result_type(2) * __pi;
  1107. return __res;
  1108. }
  1109. template<typename _RealType>
  1110. template<typename _OutputIterator,
  1111. typename _UniformRandomNumberGenerator>
  1112. void
  1113. von_mises_distribution<_RealType>::
  1114. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  1115. _UniformRandomNumberGenerator& __urng,
  1116. const param_type& __param)
  1117. {
  1118. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  1119. result_type>)
  1120. while (__f != __t)
  1121. *__f++ = this->operator()(__urng, __param);
  1122. }
  1123. template<typename _RealType, typename _CharT, typename _Traits>
  1124. std::basic_ostream<_CharT, _Traits>&
  1125. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1126. const __gnu_cxx::von_mises_distribution<_RealType>& __x)
  1127. {
  1128. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1129. typedef typename __ostream_type::ios_base __ios_base;
  1130. const typename __ios_base::fmtflags __flags = __os.flags();
  1131. const _CharT __fill = __os.fill();
  1132. const std::streamsize __precision = __os.precision();
  1133. const _CharT __space = __os.widen(' ');
  1134. __os.flags(__ios_base::scientific | __ios_base::left);
  1135. __os.fill(__space);
  1136. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1137. __os << __x.mu() << __space << __x.kappa();
  1138. __os.flags(__flags);
  1139. __os.fill(__fill);
  1140. __os.precision(__precision);
  1141. return __os;
  1142. }
  1143. template<typename _RealType, typename _CharT, typename _Traits>
  1144. std::basic_istream<_CharT, _Traits>&
  1145. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1146. __gnu_cxx::von_mises_distribution<_RealType>& __x)
  1147. {
  1148. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1149. typedef typename __istream_type::ios_base __ios_base;
  1150. const typename __ios_base::fmtflags __flags = __is.flags();
  1151. __is.flags(__ios_base::dec | __ios_base::skipws);
  1152. _RealType __mu, __kappa;
  1153. __is >> __mu >> __kappa;
  1154. __x.param(typename __gnu_cxx::von_mises_distribution<_RealType>::
  1155. param_type(__mu, __kappa));
  1156. __is.flags(__flags);
  1157. return __is;
  1158. }
  1159. template<typename _UIntType>
  1160. template<typename _UniformRandomNumberGenerator>
  1161. typename hypergeometric_distribution<_UIntType>::result_type
  1162. hypergeometric_distribution<_UIntType>::
  1163. operator()(_UniformRandomNumberGenerator& __urng,
  1164. const param_type& __param)
  1165. {
  1166. std::__detail::_Adaptor<_UniformRandomNumberGenerator, double>
  1167. __aurng(__urng);
  1168. result_type __a = __param.successful_size();
  1169. result_type __b = __param.total_size();
  1170. result_type __k = 0;
  1171. if (__param.total_draws() < __param.total_size() / 2)
  1172. {
  1173. for (result_type __i = 0; __i < __param.total_draws(); ++__i)
  1174. {
  1175. if (__b * __aurng() < __a)
  1176. {
  1177. ++__k;
  1178. if (__k == __param.successful_size())
  1179. return __k;
  1180. --__a;
  1181. }
  1182. --__b;
  1183. }
  1184. return __k;
  1185. }
  1186. else
  1187. {
  1188. for (result_type __i = 0; __i < __param.unsuccessful_size(); ++__i)
  1189. {
  1190. if (__b * __aurng() < __a)
  1191. {
  1192. ++__k;
  1193. if (__k == __param.successful_size())
  1194. return __param.successful_size() - __k;
  1195. --__a;
  1196. }
  1197. --__b;
  1198. }
  1199. return __param.successful_size() - __k;
  1200. }
  1201. }
  1202. template<typename _UIntType>
  1203. template<typename _OutputIterator,
  1204. typename _UniformRandomNumberGenerator>
  1205. void
  1206. hypergeometric_distribution<_UIntType>::
  1207. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  1208. _UniformRandomNumberGenerator& __urng,
  1209. const param_type& __param)
  1210. {
  1211. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  1212. result_type>)
  1213. while (__f != __t)
  1214. *__f++ = this->operator()(__urng);
  1215. }
  1216. template<typename _UIntType, typename _CharT, typename _Traits>
  1217. std::basic_ostream<_CharT, _Traits>&
  1218. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1219. const __gnu_cxx::hypergeometric_distribution<_UIntType>& __x)
  1220. {
  1221. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1222. typedef typename __ostream_type::ios_base __ios_base;
  1223. const typename __ios_base::fmtflags __flags = __os.flags();
  1224. const _CharT __fill = __os.fill();
  1225. const std::streamsize __precision = __os.precision();
  1226. const _CharT __space = __os.widen(' ');
  1227. __os.flags(__ios_base::scientific | __ios_base::left);
  1228. __os.fill(__space);
  1229. __os.precision(std::numeric_limits<_UIntType>::max_digits10);
  1230. __os << __x.total_size() << __space << __x.successful_size() << __space
  1231. << __x.total_draws();
  1232. __os.flags(__flags);
  1233. __os.fill(__fill);
  1234. __os.precision(__precision);
  1235. return __os;
  1236. }
  1237. template<typename _UIntType, typename _CharT, typename _Traits>
  1238. std::basic_istream<_CharT, _Traits>&
  1239. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1240. __gnu_cxx::hypergeometric_distribution<_UIntType>& __x)
  1241. {
  1242. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1243. typedef typename __istream_type::ios_base __ios_base;
  1244. const typename __ios_base::fmtflags __flags = __is.flags();
  1245. __is.flags(__ios_base::dec | __ios_base::skipws);
  1246. _UIntType __total_size, __successful_size, __total_draws;
  1247. __is >> __total_size >> __successful_size >> __total_draws;
  1248. __x.param(typename __gnu_cxx::hypergeometric_distribution<_UIntType>::
  1249. param_type(__total_size, __successful_size, __total_draws));
  1250. __is.flags(__flags);
  1251. return __is;
  1252. }
  1253. template<typename _RealType>
  1254. template<typename _UniformRandomNumberGenerator>
  1255. typename logistic_distribution<_RealType>::result_type
  1256. logistic_distribution<_RealType>::
  1257. operator()(_UniformRandomNumberGenerator& __urng,
  1258. const param_type& __p)
  1259. {
  1260. std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1261. __aurng(__urng);
  1262. result_type __arg = result_type(1);
  1263. while (__arg == result_type(1) || __arg == result_type(0))
  1264. __arg = __aurng();
  1265. return __p.a()
  1266. + __p.b() * std::log(__arg / (result_type(1) - __arg));
  1267. }
  1268. template<typename _RealType>
  1269. template<typename _OutputIterator,
  1270. typename _UniformRandomNumberGenerator>
  1271. void
  1272. logistic_distribution<_RealType>::
  1273. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  1274. _UniformRandomNumberGenerator& __urng,
  1275. const param_type& __p)
  1276. {
  1277. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  1278. result_type>)
  1279. std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1280. __aurng(__urng);
  1281. while (__f != __t)
  1282. {
  1283. result_type __arg = result_type(1);
  1284. while (__arg == result_type(1) || __arg == result_type(0))
  1285. __arg = __aurng();
  1286. *__f++ = __p.a()
  1287. + __p.b() * std::log(__arg / (result_type(1) - __arg));
  1288. }
  1289. }
  1290. template<typename _RealType, typename _CharT, typename _Traits>
  1291. std::basic_ostream<_CharT, _Traits>&
  1292. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1293. const logistic_distribution<_RealType>& __x)
  1294. {
  1295. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1296. typedef typename __ostream_type::ios_base __ios_base;
  1297. const typename __ios_base::fmtflags __flags = __os.flags();
  1298. const _CharT __fill = __os.fill();
  1299. const std::streamsize __precision = __os.precision();
  1300. const _CharT __space = __os.widen(' ');
  1301. __os.flags(__ios_base::scientific | __ios_base::left);
  1302. __os.fill(__space);
  1303. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1304. __os << __x.a() << __space << __x.b();
  1305. __os.flags(__flags);
  1306. __os.fill(__fill);
  1307. __os.precision(__precision);
  1308. return __os;
  1309. }
  1310. template<typename _RealType, typename _CharT, typename _Traits>
  1311. std::basic_istream<_CharT, _Traits>&
  1312. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1313. logistic_distribution<_RealType>& __x)
  1314. {
  1315. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1316. typedef typename __istream_type::ios_base __ios_base;
  1317. const typename __ios_base::fmtflags __flags = __is.flags();
  1318. __is.flags(__ios_base::dec | __ios_base::skipws);
  1319. _RealType __a, __b;
  1320. __is >> __a >> __b;
  1321. __x.param(typename logistic_distribution<_RealType>::
  1322. param_type(__a, __b));
  1323. __is.flags(__flags);
  1324. return __is;
  1325. }
  1326. namespace {
  1327. // Helper class for the uniform_on_sphere_distribution generation
  1328. // function.
  1329. template<std::size_t _Dimen, typename _RealType>
  1330. class uniform_on_sphere_helper
  1331. {
  1332. typedef typename uniform_on_sphere_distribution<_Dimen, _RealType>::
  1333. result_type result_type;
  1334. public:
  1335. template<typename _NormalDistribution,
  1336. typename _UniformRandomNumberGenerator>
  1337. result_type operator()(_NormalDistribution& __nd,
  1338. _UniformRandomNumberGenerator& __urng)
  1339. {
  1340. result_type __ret;
  1341. typename result_type::value_type __norm;
  1342. do
  1343. {
  1344. auto __sum = _RealType(0);
  1345. std::generate(__ret.begin(), __ret.end(),
  1346. [&__nd, &__urng, &__sum](){
  1347. _RealType __t = __nd(__urng);
  1348. __sum += __t * __t;
  1349. return __t; });
  1350. __norm = std::sqrt(__sum);
  1351. }
  1352. while (__norm == _RealType(0) || ! __builtin_isfinite(__norm));
  1353. std::transform(__ret.begin(), __ret.end(), __ret.begin(),
  1354. [__norm](_RealType __val){ return __val / __norm; });
  1355. return __ret;
  1356. }
  1357. };
  1358. template<typename _RealType>
  1359. class uniform_on_sphere_helper<2, _RealType>
  1360. {
  1361. typedef typename uniform_on_sphere_distribution<2, _RealType>::
  1362. result_type result_type;
  1363. public:
  1364. template<typename _NormalDistribution,
  1365. typename _UniformRandomNumberGenerator>
  1366. result_type operator()(_NormalDistribution&,
  1367. _UniformRandomNumberGenerator& __urng)
  1368. {
  1369. result_type __ret;
  1370. _RealType __sq;
  1371. std::__detail::_Adaptor<_UniformRandomNumberGenerator,
  1372. _RealType> __aurng(__urng);
  1373. do
  1374. {
  1375. __ret[0] = _RealType(2) * __aurng() - _RealType(1);
  1376. __ret[1] = _RealType(2) * __aurng() - _RealType(1);
  1377. __sq = __ret[0] * __ret[0] + __ret[1] * __ret[1];
  1378. }
  1379. while (__sq == _RealType(0) || __sq > _RealType(1));
  1380. #if _GLIBCXX_USE_C99_MATH_TR1
  1381. // Yes, we do not just use sqrt(__sq) because hypot() is more
  1382. // accurate.
  1383. auto __norm = std::hypot(__ret[0], __ret[1]);
  1384. #else
  1385. auto __norm = std::sqrt(__sq);
  1386. #endif
  1387. __ret[0] /= __norm;
  1388. __ret[1] /= __norm;
  1389. return __ret;
  1390. }
  1391. };
  1392. }
  1393. template<std::size_t _Dimen, typename _RealType>
  1394. template<typename _UniformRandomNumberGenerator>
  1395. typename uniform_on_sphere_distribution<_Dimen, _RealType>::result_type
  1396. uniform_on_sphere_distribution<_Dimen, _RealType>::
  1397. operator()(_UniformRandomNumberGenerator& __urng,
  1398. const param_type& __p)
  1399. {
  1400. uniform_on_sphere_helper<_Dimen, _RealType> __helper;
  1401. return __helper(_M_nd, __urng);
  1402. }
  1403. template<std::size_t _Dimen, typename _RealType>
  1404. template<typename _OutputIterator,
  1405. typename _UniformRandomNumberGenerator>
  1406. void
  1407. uniform_on_sphere_distribution<_Dimen, _RealType>::
  1408. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  1409. _UniformRandomNumberGenerator& __urng,
  1410. const param_type& __param)
  1411. {
  1412. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  1413. result_type>)
  1414. while (__f != __t)
  1415. *__f++ = this->operator()(__urng, __param);
  1416. }
  1417. template<std::size_t _Dimen, typename _RealType, typename _CharT,
  1418. typename _Traits>
  1419. std::basic_ostream<_CharT, _Traits>&
  1420. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1421. const __gnu_cxx::uniform_on_sphere_distribution<_Dimen,
  1422. _RealType>& __x)
  1423. {
  1424. return __os << __x._M_nd;
  1425. }
  1426. template<std::size_t _Dimen, typename _RealType, typename _CharT,
  1427. typename _Traits>
  1428. std::basic_istream<_CharT, _Traits>&
  1429. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1430. __gnu_cxx::uniform_on_sphere_distribution<_Dimen,
  1431. _RealType>& __x)
  1432. {
  1433. return __is >> __x._M_nd;
  1434. }
  1435. namespace {
  1436. // Helper class for the uniform_inside_sphere_distribution generation
  1437. // function.
  1438. template<std::size_t _Dimen, bool _SmallDimen, typename _RealType>
  1439. class uniform_inside_sphere_helper;
  1440. template<std::size_t _Dimen, typename _RealType>
  1441. class uniform_inside_sphere_helper<_Dimen, false, _RealType>
  1442. {
  1443. using result_type
  1444. = typename uniform_inside_sphere_distribution<_Dimen, _RealType>::
  1445. result_type;
  1446. public:
  1447. template<typename _UniformOnSphereDistribution,
  1448. typename _UniformRandomNumberGenerator>
  1449. result_type
  1450. operator()(_UniformOnSphereDistribution& __uosd,
  1451. _UniformRandomNumberGenerator& __urng,
  1452. _RealType __radius)
  1453. {
  1454. std::__detail::_Adaptor<_UniformRandomNumberGenerator,
  1455. _RealType> __aurng(__urng);
  1456. _RealType __pow = 1 / _RealType(_Dimen);
  1457. _RealType __urt = __radius * std::pow(__aurng(), __pow);
  1458. result_type __ret = __uosd(__aurng);
  1459. std::transform(__ret.begin(), __ret.end(), __ret.begin(),
  1460. [__urt](_RealType __val)
  1461. { return __val * __urt; });
  1462. return __ret;
  1463. }
  1464. };
  1465. // Helper class for the uniform_inside_sphere_distribution generation
  1466. // function specialized for small dimensions.
  1467. template<std::size_t _Dimen, typename _RealType>
  1468. class uniform_inside_sphere_helper<_Dimen, true, _RealType>
  1469. {
  1470. using result_type
  1471. = typename uniform_inside_sphere_distribution<_Dimen, _RealType>::
  1472. result_type;
  1473. public:
  1474. template<typename _UniformOnSphereDistribution,
  1475. typename _UniformRandomNumberGenerator>
  1476. result_type
  1477. operator()(_UniformOnSphereDistribution&,
  1478. _UniformRandomNumberGenerator& __urng,
  1479. _RealType __radius)
  1480. {
  1481. result_type __ret;
  1482. _RealType __sq;
  1483. _RealType __radsq = __radius * __radius;
  1484. std::__detail::_Adaptor<_UniformRandomNumberGenerator,
  1485. _RealType> __aurng(__urng);
  1486. do
  1487. {
  1488. __sq = _RealType(0);
  1489. for (int i = 0; i < _Dimen; ++i)
  1490. {
  1491. __ret[i] = _RealType(2) * __aurng() - _RealType(1);
  1492. __sq += __ret[i] * __ret[i];
  1493. }
  1494. }
  1495. while (__sq > _RealType(1));
  1496. for (int i = 0; i < _Dimen; ++i)
  1497. __ret[i] *= __radius;
  1498. return __ret;
  1499. }
  1500. };
  1501. } // namespace
  1502. //
  1503. // Experiments have shown that rejection is more efficient than transform
  1504. // for dimensions less than 8.
  1505. //
  1506. template<std::size_t _Dimen, typename _RealType>
  1507. template<typename _UniformRandomNumberGenerator>
  1508. typename uniform_inside_sphere_distribution<_Dimen, _RealType>::result_type
  1509. uniform_inside_sphere_distribution<_Dimen, _RealType>::
  1510. operator()(_UniformRandomNumberGenerator& __urng,
  1511. const param_type& __p)
  1512. {
  1513. uniform_inside_sphere_helper<_Dimen, _Dimen < 8, _RealType> __helper;
  1514. return __helper(_M_uosd, __urng, __p.radius());
  1515. }
  1516. template<std::size_t _Dimen, typename _RealType>
  1517. template<typename _OutputIterator,
  1518. typename _UniformRandomNumberGenerator>
  1519. void
  1520. uniform_inside_sphere_distribution<_Dimen, _RealType>::
  1521. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  1522. _UniformRandomNumberGenerator& __urng,
  1523. const param_type& __param)
  1524. {
  1525. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
  1526. result_type>)
  1527. while (__f != __t)
  1528. *__f++ = this->operator()(__urng, __param);
  1529. }
  1530. template<std::size_t _Dimen, typename _RealType, typename _CharT,
  1531. typename _Traits>
  1532. std::basic_ostream<_CharT, _Traits>&
  1533. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1534. const __gnu_cxx::uniform_inside_sphere_distribution<_Dimen,
  1535. _RealType>& __x)
  1536. {
  1537. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1538. typedef typename __ostream_type::ios_base __ios_base;
  1539. const typename __ios_base::fmtflags __flags = __os.flags();
  1540. const _CharT __fill = __os.fill();
  1541. const std::streamsize __precision = __os.precision();
  1542. const _CharT __space = __os.widen(' ');
  1543. __os.flags(__ios_base::scientific | __ios_base::left);
  1544. __os.fill(__space);
  1545. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1546. __os << __x.radius() << __space << __x._M_uosd;
  1547. __os.flags(__flags);
  1548. __os.fill(__fill);
  1549. __os.precision(__precision);
  1550. return __os;
  1551. }
  1552. template<std::size_t _Dimen, typename _RealType, typename _CharT,
  1553. typename _Traits>
  1554. std::basic_istream<_CharT, _Traits>&
  1555. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1556. __gnu_cxx::uniform_inside_sphere_distribution<_Dimen,
  1557. _RealType>& __x)
  1558. {
  1559. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1560. typedef typename __istream_type::ios_base __ios_base;
  1561. const typename __ios_base::fmtflags __flags = __is.flags();
  1562. __is.flags(__ios_base::dec | __ios_base::skipws);
  1563. _RealType __radius_val;
  1564. __is >> __radius_val >> __x._M_uosd;
  1565. __x.param(typename uniform_inside_sphere_distribution<_Dimen, _RealType>::
  1566. param_type(__radius_val));
  1567. __is.flags(__flags);
  1568. return __is;
  1569. }
  1570. _GLIBCXX_END_NAMESPACE_VERSION
  1571. } // namespace __gnu_cxx
  1572. #endif // _EXT_RANDOM_TCC