random.tcc 59 KB

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