random.tcc 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. // Random number extensions -*- C++ -*-
  2. // Copyright (C) 2012-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/random.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. std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  383. __aurng(__urng);
  384. while (__f != __t)
  385. {
  386. result_type __x, __y;
  387. do
  388. {
  389. __x = std::exp(std::log(__aurng()) / __param.alpha());
  390. __y = std::exp(std::log(__aurng()) / __param.beta());
  391. }
  392. while (__x + __y > result_type(1));
  393. *__f++ = __x / (__x + __y);
  394. }
  395. }
  396. template<typename _RealType, typename _CharT, typename _Traits>
  397. std::basic_ostream<_CharT, _Traits>&
  398. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  399. const __gnu_cxx::beta_distribution<_RealType>& __x)
  400. {
  401. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  402. typedef typename __ostream_type::ios_base __ios_base;
  403. const typename __ios_base::fmtflags __flags = __os.flags();
  404. const _CharT __fill = __os.fill();
  405. const std::streamsize __precision = __os.precision();
  406. const _CharT __space = __os.widen(' ');
  407. __os.flags(__ios_base::scientific | __ios_base::left);
  408. __os.fill(__space);
  409. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  410. __os << __x.alpha() << __space << __x.beta();
  411. __os.flags(__flags);
  412. __os.fill(__fill);
  413. __os.precision(__precision);
  414. return __os;
  415. }
  416. template<typename _RealType, typename _CharT, typename _Traits>
  417. std::basic_istream<_CharT, _Traits>&
  418. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  419. __gnu_cxx::beta_distribution<_RealType>& __x)
  420. {
  421. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  422. typedef typename __istream_type::ios_base __ios_base;
  423. const typename __ios_base::fmtflags __flags = __is.flags();
  424. __is.flags(__ios_base::dec | __ios_base::skipws);
  425. _RealType __alpha_val, __beta_val;
  426. __is >> __alpha_val >> __beta_val;
  427. __x.param(typename __gnu_cxx::beta_distribution<_RealType>::
  428. param_type(__alpha_val, __beta_val));
  429. __is.flags(__flags);
  430. return __is;
  431. }
  432. template<std::size_t _Dimen, typename _RealType>
  433. template<typename _InputIterator1, typename _InputIterator2>
  434. void
  435. normal_mv_distribution<_Dimen, _RealType>::param_type::
  436. _M_init_full(_InputIterator1 __meanbegin, _InputIterator1 __meanend,
  437. _InputIterator2 __varcovbegin, _InputIterator2 __varcovend)
  438. {
  439. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  440. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  441. std::fill(std::copy(__meanbegin, __meanend, _M_mean.begin()),
  442. _M_mean.end(), _RealType(0));
  443. // Perform the Cholesky decomposition
  444. auto __w = _M_t.begin();
  445. for (size_t __j = 0; __j < _Dimen; ++__j)
  446. {
  447. _RealType __sum = _RealType(0);
  448. auto __slitbegin = __w;
  449. auto __cit = _M_t.begin();
  450. for (size_t __i = 0; __i < __j; ++__i)
  451. {
  452. auto __slit = __slitbegin;
  453. _RealType __s = *__varcovbegin++;
  454. for (size_t __k = 0; __k < __i; ++__k)
  455. __s -= *__slit++ * *__cit++;
  456. *__w++ = __s /= *__cit++;
  457. __sum += __s * __s;
  458. }
  459. __sum = *__varcovbegin - __sum;
  460. if (__builtin_expect(__sum <= _RealType(0), 0))
  461. std::__throw_runtime_error(__N("normal_mv_distribution::"
  462. "param_type::_M_init_full"));
  463. *__w++ = std::sqrt(__sum);
  464. std::advance(__varcovbegin, _Dimen - __j);
  465. }
  466. }
  467. template<std::size_t _Dimen, typename _RealType>
  468. template<typename _InputIterator1, typename _InputIterator2>
  469. void
  470. normal_mv_distribution<_Dimen, _RealType>::param_type::
  471. _M_init_lower(_InputIterator1 __meanbegin, _InputIterator1 __meanend,
  472. _InputIterator2 __varcovbegin, _InputIterator2 __varcovend)
  473. {
  474. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  475. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  476. std::fill(std::copy(__meanbegin, __meanend, _M_mean.begin()),
  477. _M_mean.end(), _RealType(0));
  478. // Perform the Cholesky decomposition
  479. auto __w = _M_t.begin();
  480. for (size_t __j = 0; __j < _Dimen; ++__j)
  481. {
  482. _RealType __sum = _RealType(0);
  483. auto __slitbegin = __w;
  484. auto __cit = _M_t.begin();
  485. for (size_t __i = 0; __i < __j; ++__i)
  486. {
  487. auto __slit = __slitbegin;
  488. _RealType __s = *__varcovbegin++;
  489. for (size_t __k = 0; __k < __i; ++__k)
  490. __s -= *__slit++ * *__cit++;
  491. *__w++ = __s /= *__cit++;
  492. __sum += __s * __s;
  493. }
  494. __sum = *__varcovbegin++ - __sum;
  495. if (__builtin_expect(__sum <= _RealType(0), 0))
  496. std::__throw_runtime_error(__N("normal_mv_distribution::"
  497. "param_type::_M_init_full"));
  498. *__w++ = std::sqrt(__sum);
  499. }
  500. }
  501. template<std::size_t _Dimen, typename _RealType>
  502. template<typename _InputIterator1, typename _InputIterator2>
  503. void
  504. normal_mv_distribution<_Dimen, _RealType>::param_type::
  505. _M_init_diagonal(_InputIterator1 __meanbegin, _InputIterator1 __meanend,
  506. _InputIterator2 __varbegin, _InputIterator2 __varend)
  507. {
  508. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
  509. __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
  510. std::fill(std::copy(__meanbegin, __meanend, _M_mean.begin()),
  511. _M_mean.end(), _RealType(0));
  512. auto __w = _M_t.begin();
  513. size_t __step = 0;
  514. while (__varbegin != __varend)
  515. {
  516. std::fill_n(__w, __step, _RealType(0));
  517. __w += __step++;
  518. if (__builtin_expect(*__varbegin < _RealType(0), 0))
  519. std::__throw_runtime_error(__N("normal_mv_distribution::"
  520. "param_type::_M_init_diagonal"));
  521. *__w++ = std::sqrt(*__varbegin++);
  522. }
  523. }
  524. template<std::size_t _Dimen, typename _RealType>
  525. template<typename _UniformRandomNumberGenerator>
  526. typename normal_mv_distribution<_Dimen, _RealType>::result_type
  527. normal_mv_distribution<_Dimen, _RealType>::
  528. operator()(_UniformRandomNumberGenerator& __urng,
  529. const param_type& __param)
  530. {
  531. result_type __ret;
  532. _M_nd.__generate(__ret.begin(), __ret.end(), __urng);
  533. auto __t_it = __param._M_t.crbegin();
  534. for (size_t __i = _Dimen; __i > 0; --__i)
  535. {
  536. _RealType __sum = _RealType(0);
  537. for (size_t __j = __i; __j > 0; --__j)
  538. __sum += __ret[__j - 1] * *__t_it++;
  539. __ret[__i - 1] = __sum;
  540. }
  541. return __ret;
  542. }
  543. template<std::size_t _Dimen, typename _RealType>
  544. template<typename _ForwardIterator, typename _UniformRandomNumberGenerator>
  545. void
  546. normal_mv_distribution<_Dimen, _RealType>::
  547. __generate_impl(_ForwardIterator __f, _ForwardIterator __t,
  548. _UniformRandomNumberGenerator& __urng,
  549. const param_type& __param)
  550. {
  551. __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
  552. _ForwardIterator>)
  553. while (__f != __t)
  554. *__f++ = this->operator()(__urng, __param);
  555. }
  556. template<size_t _Dimen, typename _RealType>
  557. bool
  558. operator==(const __gnu_cxx::normal_mv_distribution<_Dimen, _RealType>&
  559. __d1,
  560. const __gnu_cxx::normal_mv_distribution<_Dimen, _RealType>&
  561. __d2)
  562. {
  563. return __d1._M_param == __d2._M_param && __d1._M_nd == __d2._M_nd;
  564. }
  565. template<size_t _Dimen, typename _RealType, typename _CharT, typename _Traits>
  566. std::basic_ostream<_CharT, _Traits>&
  567. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  568. const __gnu_cxx::normal_mv_distribution<_Dimen, _RealType>& __x)
  569. {
  570. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  571. typedef typename __ostream_type::ios_base __ios_base;
  572. const typename __ios_base::fmtflags __flags = __os.flags();
  573. const _CharT __fill = __os.fill();
  574. const std::streamsize __precision = __os.precision();
  575. const _CharT __space = __os.widen(' ');
  576. __os.flags(__ios_base::scientific | __ios_base::left);
  577. __os.fill(__space);
  578. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  579. auto __mean = __x._M_param.mean();
  580. for (auto __it : __mean)
  581. __os << __it << __space;
  582. auto __t = __x._M_param.varcov();
  583. for (auto __it : __t)
  584. __os << __it << __space;
  585. __os << __x._M_nd;
  586. __os.flags(__flags);
  587. __os.fill(__fill);
  588. __os.precision(__precision);
  589. return __os;
  590. }
  591. template<size_t _Dimen, typename _RealType, typename _CharT, typename _Traits>
  592. std::basic_istream<_CharT, _Traits>&
  593. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  594. __gnu_cxx::normal_mv_distribution<_Dimen, _RealType>& __x)
  595. {
  596. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  597. typedef typename __istream_type::ios_base __ios_base;
  598. const typename __ios_base::fmtflags __flags = __is.flags();
  599. __is.flags(__ios_base::dec | __ios_base::skipws);
  600. std::array<_RealType, _Dimen> __mean;
  601. for (auto& __it : __mean)
  602. __is >> __it;
  603. std::array<_RealType, _Dimen * (_Dimen + 1) / 2> __varcov;
  604. for (auto& __it : __varcov)
  605. __is >> __it;
  606. __is >> __x._M_nd;
  607. __x.param(typename normal_mv_distribution<_Dimen, _RealType>::
  608. param_type(__mean.begin(), __mean.end(),
  609. __varcov.begin(), __varcov.end()));
  610. __is.flags(__flags);
  611. return __is;
  612. }
  613. template<typename _RealType>
  614. template<typename _OutputIterator,
  615. typename _UniformRandomNumberGenerator>
  616. void
  617. rice_distribution<_RealType>::
  618. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  619. _UniformRandomNumberGenerator& __urng,
  620. const param_type& __p)
  621. {
  622. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>)
  623. while (__f != __t)
  624. {
  625. typename std::normal_distribution<result_type>::param_type
  626. __px(__p.nu(), __p.sigma()), __py(result_type(0), __p.sigma());
  627. result_type __x = this->_M_ndx(__px, __urng);
  628. result_type __y = this->_M_ndy(__py, __urng);
  629. #if _GLIBCXX_USE_C99_MATH_TR1
  630. *__f++ = std::hypot(__x, __y);
  631. #else
  632. *__f++ = std::sqrt(__x * __x + __y * __y);
  633. #endif
  634. }
  635. }
  636. template<typename _RealType, typename _CharT, typename _Traits>
  637. std::basic_ostream<_CharT, _Traits>&
  638. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  639. const rice_distribution<_RealType>& __x)
  640. {
  641. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  642. typedef typename __ostream_type::ios_base __ios_base;
  643. const typename __ios_base::fmtflags __flags = __os.flags();
  644. const _CharT __fill = __os.fill();
  645. const std::streamsize __precision = __os.precision();
  646. const _CharT __space = __os.widen(' ');
  647. __os.flags(__ios_base::scientific | __ios_base::left);
  648. __os.fill(__space);
  649. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  650. __os << __x.nu() << __space << __x.sigma();
  651. __os << __space << __x._M_ndx;
  652. __os << __space << __x._M_ndy;
  653. __os.flags(__flags);
  654. __os.fill(__fill);
  655. __os.precision(__precision);
  656. return __os;
  657. }
  658. template<typename _RealType, typename _CharT, typename _Traits>
  659. std::basic_istream<_CharT, _Traits>&
  660. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  661. rice_distribution<_RealType>& __x)
  662. {
  663. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  664. typedef typename __istream_type::ios_base __ios_base;
  665. const typename __ios_base::fmtflags __flags = __is.flags();
  666. __is.flags(__ios_base::dec | __ios_base::skipws);
  667. _RealType __nu_val, __sigma_val;
  668. __is >> __nu_val >> __sigma_val;
  669. __is >> __x._M_ndx;
  670. __is >> __x._M_ndy;
  671. __x.param(typename rice_distribution<_RealType>::
  672. param_type(__nu_val, __sigma_val));
  673. __is.flags(__flags);
  674. return __is;
  675. }
  676. template<typename _RealType>
  677. template<typename _OutputIterator,
  678. typename _UniformRandomNumberGenerator>
  679. void
  680. nakagami_distribution<_RealType>::
  681. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  682. _UniformRandomNumberGenerator& __urng,
  683. const param_type& __p)
  684. {
  685. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>)
  686. typename std::gamma_distribution<result_type>::param_type
  687. __pg(__p.mu(), __p.omega() / __p.mu());
  688. while (__f != __t)
  689. *__f++ = std::sqrt(this->_M_gd(__pg, __urng));
  690. }
  691. template<typename _RealType, typename _CharT, typename _Traits>
  692. std::basic_ostream<_CharT, _Traits>&
  693. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  694. const nakagami_distribution<_RealType>& __x)
  695. {
  696. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  697. typedef typename __ostream_type::ios_base __ios_base;
  698. const typename __ios_base::fmtflags __flags = __os.flags();
  699. const _CharT __fill = __os.fill();
  700. const std::streamsize __precision = __os.precision();
  701. const _CharT __space = __os.widen(' ');
  702. __os.flags(__ios_base::scientific | __ios_base::left);
  703. __os.fill(__space);
  704. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  705. __os << __x.mu() << __space << __x.omega();
  706. __os << __space << __x._M_gd;
  707. __os.flags(__flags);
  708. __os.fill(__fill);
  709. __os.precision(__precision);
  710. return __os;
  711. }
  712. template<typename _RealType, typename _CharT, typename _Traits>
  713. std::basic_istream<_CharT, _Traits>&
  714. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  715. nakagami_distribution<_RealType>& __x)
  716. {
  717. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  718. typedef typename __istream_type::ios_base __ios_base;
  719. const typename __ios_base::fmtflags __flags = __is.flags();
  720. __is.flags(__ios_base::dec | __ios_base::skipws);
  721. _RealType __mu_val, __omega_val;
  722. __is >> __mu_val >> __omega_val;
  723. __is >> __x._M_gd;
  724. __x.param(typename nakagami_distribution<_RealType>::
  725. param_type(__mu_val, __omega_val));
  726. __is.flags(__flags);
  727. return __is;
  728. }
  729. template<typename _RealType>
  730. template<typename _OutputIterator,
  731. typename _UniformRandomNumberGenerator>
  732. void
  733. pareto_distribution<_RealType>::
  734. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  735. _UniformRandomNumberGenerator& __urng,
  736. const param_type& __p)
  737. {
  738. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>)
  739. result_type __mu_val = __p.mu();
  740. result_type __malphinv = -result_type(1) / __p.alpha();
  741. while (__f != __t)
  742. *__f++ = __mu_val * std::pow(this->_M_ud(__urng), __malphinv);
  743. }
  744. template<typename _RealType, typename _CharT, typename _Traits>
  745. std::basic_ostream<_CharT, _Traits>&
  746. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  747. const pareto_distribution<_RealType>& __x)
  748. {
  749. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  750. typedef typename __ostream_type::ios_base __ios_base;
  751. const typename __ios_base::fmtflags __flags = __os.flags();
  752. const _CharT __fill = __os.fill();
  753. const std::streamsize __precision = __os.precision();
  754. const _CharT __space = __os.widen(' ');
  755. __os.flags(__ios_base::scientific | __ios_base::left);
  756. __os.fill(__space);
  757. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  758. __os << __x.alpha() << __space << __x.mu();
  759. __os << __space << __x._M_ud;
  760. __os.flags(__flags);
  761. __os.fill(__fill);
  762. __os.precision(__precision);
  763. return __os;
  764. }
  765. template<typename _RealType, typename _CharT, typename _Traits>
  766. std::basic_istream<_CharT, _Traits>&
  767. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  768. pareto_distribution<_RealType>& __x)
  769. {
  770. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  771. typedef typename __istream_type::ios_base __ios_base;
  772. const typename __ios_base::fmtflags __flags = __is.flags();
  773. __is.flags(__ios_base::dec | __ios_base::skipws);
  774. _RealType __alpha_val, __mu_val;
  775. __is >> __alpha_val >> __mu_val;
  776. __is >> __x._M_ud;
  777. __x.param(typename pareto_distribution<_RealType>::
  778. param_type(__alpha_val, __mu_val));
  779. __is.flags(__flags);
  780. return __is;
  781. }
  782. template<typename _RealType>
  783. template<typename _UniformRandomNumberGenerator>
  784. typename k_distribution<_RealType>::result_type
  785. k_distribution<_RealType>::
  786. operator()(_UniformRandomNumberGenerator& __urng)
  787. {
  788. result_type __x = this->_M_gd1(__urng);
  789. result_type __y = this->_M_gd2(__urng);
  790. return std::sqrt(__x * __y);
  791. }
  792. template<typename _RealType>
  793. template<typename _UniformRandomNumberGenerator>
  794. typename k_distribution<_RealType>::result_type
  795. k_distribution<_RealType>::
  796. operator()(_UniformRandomNumberGenerator& __urng,
  797. const param_type& __p)
  798. {
  799. typename std::gamma_distribution<result_type>::param_type
  800. __p1(__p.lambda(), result_type(1) / __p.lambda()),
  801. __p2(__p.nu(), __p.mu() / __p.nu());
  802. result_type __x = this->_M_gd1(__p1, __urng);
  803. result_type __y = this->_M_gd2(__p2, __urng);
  804. return std::sqrt(__x * __y);
  805. }
  806. template<typename _RealType>
  807. template<typename _OutputIterator,
  808. typename _UniformRandomNumberGenerator>
  809. void
  810. k_distribution<_RealType>::
  811. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  812. _UniformRandomNumberGenerator& __urng,
  813. const param_type& __p)
  814. {
  815. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>)
  816. typename std::gamma_distribution<result_type>::param_type
  817. __p1(__p.lambda(), result_type(1) / __p.lambda()),
  818. __p2(__p.nu(), __p.mu() / __p.nu());
  819. while (__f != __t)
  820. {
  821. result_type __x = this->_M_gd1(__p1, __urng);
  822. result_type __y = this->_M_gd2(__p2, __urng);
  823. *__f++ = std::sqrt(__x * __y);
  824. }
  825. }
  826. template<typename _RealType, typename _CharT, typename _Traits>
  827. std::basic_ostream<_CharT, _Traits>&
  828. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  829. const k_distribution<_RealType>& __x)
  830. {
  831. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  832. typedef typename __ostream_type::ios_base __ios_base;
  833. const typename __ios_base::fmtflags __flags = __os.flags();
  834. const _CharT __fill = __os.fill();
  835. const std::streamsize __precision = __os.precision();
  836. const _CharT __space = __os.widen(' ');
  837. __os.flags(__ios_base::scientific | __ios_base::left);
  838. __os.fill(__space);
  839. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  840. __os << __x.lambda() << __space << __x.mu() << __space << __x.nu();
  841. __os << __space << __x._M_gd1;
  842. __os << __space << __x._M_gd2;
  843. __os.flags(__flags);
  844. __os.fill(__fill);
  845. __os.precision(__precision);
  846. return __os;
  847. }
  848. template<typename _RealType, typename _CharT, typename _Traits>
  849. std::basic_istream<_CharT, _Traits>&
  850. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  851. k_distribution<_RealType>& __x)
  852. {
  853. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  854. typedef typename __istream_type::ios_base __ios_base;
  855. const typename __ios_base::fmtflags __flags = __is.flags();
  856. __is.flags(__ios_base::dec | __ios_base::skipws);
  857. _RealType __lambda_val, __mu_val, __nu_val;
  858. __is >> __lambda_val >> __mu_val >> __nu_val;
  859. __is >> __x._M_gd1;
  860. __is >> __x._M_gd2;
  861. __x.param(typename k_distribution<_RealType>::
  862. param_type(__lambda_val, __mu_val, __nu_val));
  863. __is.flags(__flags);
  864. return __is;
  865. }
  866. template<typename _RealType>
  867. template<typename _OutputIterator,
  868. typename _UniformRandomNumberGenerator>
  869. void
  870. arcsine_distribution<_RealType>::
  871. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  872. _UniformRandomNumberGenerator& __urng,
  873. const param_type& __p)
  874. {
  875. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>)
  876. result_type __dif = __p.b() - __p.a();
  877. result_type __sum = __p.a() + __p.b();
  878. while (__f != __t)
  879. {
  880. result_type __x = std::sin(this->_M_ud(__urng));
  881. *__f++ = (__x * __dif + __sum) / result_type(2);
  882. }
  883. }
  884. template<typename _RealType, typename _CharT, typename _Traits>
  885. std::basic_ostream<_CharT, _Traits>&
  886. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  887. const arcsine_distribution<_RealType>& __x)
  888. {
  889. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  890. typedef typename __ostream_type::ios_base __ios_base;
  891. const typename __ios_base::fmtflags __flags = __os.flags();
  892. const _CharT __fill = __os.fill();
  893. const std::streamsize __precision = __os.precision();
  894. const _CharT __space = __os.widen(' ');
  895. __os.flags(__ios_base::scientific | __ios_base::left);
  896. __os.fill(__space);
  897. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  898. __os << __x.a() << __space << __x.b();
  899. __os << __space << __x._M_ud;
  900. __os.flags(__flags);
  901. __os.fill(__fill);
  902. __os.precision(__precision);
  903. return __os;
  904. }
  905. template<typename _RealType, typename _CharT, typename _Traits>
  906. std::basic_istream<_CharT, _Traits>&
  907. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  908. arcsine_distribution<_RealType>& __x)
  909. {
  910. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  911. typedef typename __istream_type::ios_base __ios_base;
  912. const typename __ios_base::fmtflags __flags = __is.flags();
  913. __is.flags(__ios_base::dec | __ios_base::skipws);
  914. _RealType __a, __b;
  915. __is >> __a >> __b;
  916. __is >> __x._M_ud;
  917. __x.param(typename arcsine_distribution<_RealType>::
  918. param_type(__a, __b));
  919. __is.flags(__flags);
  920. return __is;
  921. }
  922. template<typename _RealType>
  923. template<typename _UniformRandomNumberGenerator>
  924. typename hoyt_distribution<_RealType>::result_type
  925. hoyt_distribution<_RealType>::
  926. operator()(_UniformRandomNumberGenerator& __urng)
  927. {
  928. result_type __x = this->_M_ad(__urng);
  929. result_type __y = this->_M_ed(__urng);
  930. return (result_type(2) * this->q()
  931. / (result_type(1) + this->q() * this->q()))
  932. * std::sqrt(this->omega() * __x * __y);
  933. }
  934. template<typename _RealType>
  935. template<typename _UniformRandomNumberGenerator>
  936. typename hoyt_distribution<_RealType>::result_type
  937. hoyt_distribution<_RealType>::
  938. operator()(_UniformRandomNumberGenerator& __urng,
  939. const param_type& __p)
  940. {
  941. result_type __q2 = __p.q() * __p.q();
  942. result_type __num = result_type(0.5L) * (result_type(1) + __q2);
  943. typename __gnu_cxx::arcsine_distribution<result_type>::param_type
  944. __pa(__num, __num / __q2);
  945. result_type __x = this->_M_ad(__pa, __urng);
  946. result_type __y = this->_M_ed(__urng);
  947. return (result_type(2) * __p.q() / (result_type(1) + __q2))
  948. * std::sqrt(__p.omega() * __x * __y);
  949. }
  950. template<typename _RealType>
  951. template<typename _OutputIterator,
  952. typename _UniformRandomNumberGenerator>
  953. void
  954. hoyt_distribution<_RealType>::
  955. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  956. _UniformRandomNumberGenerator& __urng,
  957. const param_type& __p)
  958. {
  959. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>)
  960. result_type __2q = result_type(2) * __p.q();
  961. result_type __q2 = __p.q() * __p.q();
  962. result_type __q2p1 = result_type(1) + __q2;
  963. result_type __num = result_type(0.5L) * __q2p1;
  964. result_type __omega = __p.omega();
  965. typename __gnu_cxx::arcsine_distribution<result_type>::param_type
  966. __pa(__num, __num / __q2);
  967. while (__f != __t)
  968. {
  969. result_type __x = this->_M_ad(__pa, __urng);
  970. result_type __y = this->_M_ed(__urng);
  971. *__f++ = (__2q / __q2p1) * std::sqrt(__omega * __x * __y);
  972. }
  973. }
  974. template<typename _RealType, typename _CharT, typename _Traits>
  975. std::basic_ostream<_CharT, _Traits>&
  976. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  977. const hoyt_distribution<_RealType>& __x)
  978. {
  979. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  980. typedef typename __ostream_type::ios_base __ios_base;
  981. const typename __ios_base::fmtflags __flags = __os.flags();
  982. const _CharT __fill = __os.fill();
  983. const std::streamsize __precision = __os.precision();
  984. const _CharT __space = __os.widen(' ');
  985. __os.flags(__ios_base::scientific | __ios_base::left);
  986. __os.fill(__space);
  987. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  988. __os << __x.q() << __space << __x.omega();
  989. __os << __space << __x._M_ad;
  990. __os << __space << __x._M_ed;
  991. __os.flags(__flags);
  992. __os.fill(__fill);
  993. __os.precision(__precision);
  994. return __os;
  995. }
  996. template<typename _RealType, typename _CharT, typename _Traits>
  997. std::basic_istream<_CharT, _Traits>&
  998. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  999. hoyt_distribution<_RealType>& __x)
  1000. {
  1001. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1002. typedef typename __istream_type::ios_base __ios_base;
  1003. const typename __ios_base::fmtflags __flags = __is.flags();
  1004. __is.flags(__ios_base::dec | __ios_base::skipws);
  1005. _RealType __q, __omega;
  1006. __is >> __q >> __omega;
  1007. __is >> __x._M_ad;
  1008. __is >> __x._M_ed;
  1009. __x.param(typename hoyt_distribution<_RealType>::
  1010. param_type(__q, __omega));
  1011. __is.flags(__flags);
  1012. return __is;
  1013. }
  1014. template<typename _RealType>
  1015. template<typename _OutputIterator,
  1016. typename _UniformRandomNumberGenerator>
  1017. void
  1018. triangular_distribution<_RealType>::
  1019. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  1020. _UniformRandomNumberGenerator& __urng,
  1021. const param_type& __param)
  1022. {
  1023. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>)
  1024. while (__f != __t)
  1025. *__f++ = this->operator()(__urng, __param);
  1026. }
  1027. template<typename _RealType, typename _CharT, typename _Traits>
  1028. std::basic_ostream<_CharT, _Traits>&
  1029. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1030. const __gnu_cxx::triangular_distribution<_RealType>& __x)
  1031. {
  1032. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1033. typedef typename __ostream_type::ios_base __ios_base;
  1034. const typename __ios_base::fmtflags __flags = __os.flags();
  1035. const _CharT __fill = __os.fill();
  1036. const std::streamsize __precision = __os.precision();
  1037. const _CharT __space = __os.widen(' ');
  1038. __os.flags(__ios_base::scientific | __ios_base::left);
  1039. __os.fill(__space);
  1040. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1041. __os << __x.a() << __space << __x.b() << __space << __x.c();
  1042. __os.flags(__flags);
  1043. __os.fill(__fill);
  1044. __os.precision(__precision);
  1045. return __os;
  1046. }
  1047. template<typename _RealType, typename _CharT, typename _Traits>
  1048. std::basic_istream<_CharT, _Traits>&
  1049. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1050. __gnu_cxx::triangular_distribution<_RealType>& __x)
  1051. {
  1052. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1053. typedef typename __istream_type::ios_base __ios_base;
  1054. const typename __ios_base::fmtflags __flags = __is.flags();
  1055. __is.flags(__ios_base::dec | __ios_base::skipws);
  1056. _RealType __a, __b, __c;
  1057. __is >> __a >> __b >> __c;
  1058. __x.param(typename __gnu_cxx::triangular_distribution<_RealType>::
  1059. param_type(__a, __b, __c));
  1060. __is.flags(__flags);
  1061. return __is;
  1062. }
  1063. template<typename _RealType>
  1064. template<typename _UniformRandomNumberGenerator>
  1065. typename von_mises_distribution<_RealType>::result_type
  1066. von_mises_distribution<_RealType>::
  1067. operator()(_UniformRandomNumberGenerator& __urng,
  1068. const param_type& __p)
  1069. {
  1070. const result_type __pi
  1071. = __gnu_cxx::__math_constants<result_type>::__pi;
  1072. std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1073. __aurng(__urng);
  1074. result_type __f;
  1075. while (1)
  1076. {
  1077. result_type __rnd = std::cos(__pi * __aurng());
  1078. __f = (result_type(1) + __p._M_r * __rnd) / (__p._M_r + __rnd);
  1079. result_type __c = __p._M_kappa * (__p._M_r - __f);
  1080. result_type __rnd2 = __aurng();
  1081. if (__c * (result_type(2) - __c) > __rnd2)
  1082. break;
  1083. if (std::log(__c / __rnd2) >= __c - result_type(1))
  1084. break;
  1085. }
  1086. result_type __res = std::acos(__f);
  1087. #if _GLIBCXX_USE_C99_MATH_TR1
  1088. __res = std::copysign(__res, __aurng() - result_type(0.5));
  1089. #else
  1090. if (__aurng() < result_type(0.5))
  1091. __res = -__res;
  1092. #endif
  1093. __res += __p._M_mu;
  1094. if (__res > __pi)
  1095. __res -= result_type(2) * __pi;
  1096. else if (__res < -__pi)
  1097. __res += result_type(2) * __pi;
  1098. return __res;
  1099. }
  1100. template<typename _RealType>
  1101. template<typename _OutputIterator,
  1102. typename _UniformRandomNumberGenerator>
  1103. void
  1104. von_mises_distribution<_RealType>::
  1105. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  1106. _UniformRandomNumberGenerator& __urng,
  1107. const param_type& __param)
  1108. {
  1109. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>)
  1110. while (__f != __t)
  1111. *__f++ = this->operator()(__urng, __param);
  1112. }
  1113. template<typename _RealType, typename _CharT, typename _Traits>
  1114. std::basic_ostream<_CharT, _Traits>&
  1115. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1116. const __gnu_cxx::von_mises_distribution<_RealType>& __x)
  1117. {
  1118. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1119. typedef typename __ostream_type::ios_base __ios_base;
  1120. const typename __ios_base::fmtflags __flags = __os.flags();
  1121. const _CharT __fill = __os.fill();
  1122. const std::streamsize __precision = __os.precision();
  1123. const _CharT __space = __os.widen(' ');
  1124. __os.flags(__ios_base::scientific | __ios_base::left);
  1125. __os.fill(__space);
  1126. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1127. __os << __x.mu() << __space << __x.kappa();
  1128. __os.flags(__flags);
  1129. __os.fill(__fill);
  1130. __os.precision(__precision);
  1131. return __os;
  1132. }
  1133. template<typename _RealType, typename _CharT, typename _Traits>
  1134. std::basic_istream<_CharT, _Traits>&
  1135. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1136. __gnu_cxx::von_mises_distribution<_RealType>& __x)
  1137. {
  1138. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1139. typedef typename __istream_type::ios_base __ios_base;
  1140. const typename __ios_base::fmtflags __flags = __is.flags();
  1141. __is.flags(__ios_base::dec | __ios_base::skipws);
  1142. _RealType __mu, __kappa;
  1143. __is >> __mu >> __kappa;
  1144. __x.param(typename __gnu_cxx::von_mises_distribution<_RealType>::
  1145. param_type(__mu, __kappa));
  1146. __is.flags(__flags);
  1147. return __is;
  1148. }
  1149. template<typename _UIntType>
  1150. template<typename _UniformRandomNumberGenerator>
  1151. typename hypergeometric_distribution<_UIntType>::result_type
  1152. hypergeometric_distribution<_UIntType>::
  1153. operator()(_UniformRandomNumberGenerator& __urng,
  1154. const param_type& __param)
  1155. {
  1156. std::__detail::_Adaptor<_UniformRandomNumberGenerator, double>
  1157. __aurng(__urng);
  1158. result_type __a = __param.successful_size();
  1159. result_type __b = __param.total_size();
  1160. result_type __k = 0;
  1161. if (__param.total_draws() < __param.total_size() / 2)
  1162. {
  1163. for (result_type __i = 0; __i < __param.total_draws(); ++__i)
  1164. {
  1165. if (__b * __aurng() < __a)
  1166. {
  1167. ++__k;
  1168. if (__k == __param.successful_size())
  1169. return __k;
  1170. --__a;
  1171. }
  1172. --__b;
  1173. }
  1174. return __k;
  1175. }
  1176. else
  1177. {
  1178. for (result_type __i = 0; __i < __param.unsuccessful_size(); ++__i)
  1179. {
  1180. if (__b * __aurng() < __a)
  1181. {
  1182. ++__k;
  1183. if (__k == __param.successful_size())
  1184. return __param.successful_size() - __k;
  1185. --__a;
  1186. }
  1187. --__b;
  1188. }
  1189. return __param.successful_size() - __k;
  1190. }
  1191. }
  1192. template<typename _UIntType>
  1193. template<typename _OutputIterator,
  1194. typename _UniformRandomNumberGenerator>
  1195. void
  1196. hypergeometric_distribution<_UIntType>::
  1197. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  1198. _UniformRandomNumberGenerator& __urng,
  1199. const param_type& __param)
  1200. {
  1201. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>)
  1202. while (__f != __t)
  1203. *__f++ = this->operator()(__urng);
  1204. }
  1205. template<typename _UIntType, typename _CharT, typename _Traits>
  1206. std::basic_ostream<_CharT, _Traits>&
  1207. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1208. const __gnu_cxx::hypergeometric_distribution<_UIntType>& __x)
  1209. {
  1210. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1211. typedef typename __ostream_type::ios_base __ios_base;
  1212. const typename __ios_base::fmtflags __flags = __os.flags();
  1213. const _CharT __fill = __os.fill();
  1214. const std::streamsize __precision = __os.precision();
  1215. const _CharT __space = __os.widen(' ');
  1216. __os.flags(__ios_base::scientific | __ios_base::left);
  1217. __os.fill(__space);
  1218. __os.precision(std::numeric_limits<_UIntType>::max_digits10);
  1219. __os << __x.total_size() << __space << __x.successful_size() << __space
  1220. << __x.total_draws();
  1221. __os.flags(__flags);
  1222. __os.fill(__fill);
  1223. __os.precision(__precision);
  1224. return __os;
  1225. }
  1226. template<typename _UIntType, typename _CharT, typename _Traits>
  1227. std::basic_istream<_CharT, _Traits>&
  1228. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1229. __gnu_cxx::hypergeometric_distribution<_UIntType>& __x)
  1230. {
  1231. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1232. typedef typename __istream_type::ios_base __ios_base;
  1233. const typename __ios_base::fmtflags __flags = __is.flags();
  1234. __is.flags(__ios_base::dec | __ios_base::skipws);
  1235. _UIntType __total_size, __successful_size, __total_draws;
  1236. __is >> __total_size >> __successful_size >> __total_draws;
  1237. __x.param(typename __gnu_cxx::hypergeometric_distribution<_UIntType>::
  1238. param_type(__total_size, __successful_size, __total_draws));
  1239. __is.flags(__flags);
  1240. return __is;
  1241. }
  1242. template<typename _RealType>
  1243. template<typename _UniformRandomNumberGenerator>
  1244. typename logistic_distribution<_RealType>::result_type
  1245. logistic_distribution<_RealType>::
  1246. operator()(_UniformRandomNumberGenerator& __urng,
  1247. const param_type& __p)
  1248. {
  1249. std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1250. __aurng(__urng);
  1251. result_type __arg = result_type(1);
  1252. while (__arg == result_type(1) || __arg == result_type(0))
  1253. __arg = __aurng();
  1254. return __p.a()
  1255. + __p.b() * std::log(__arg / (result_type(1) - __arg));
  1256. }
  1257. template<typename _RealType>
  1258. template<typename _OutputIterator,
  1259. typename _UniformRandomNumberGenerator>
  1260. void
  1261. logistic_distribution<_RealType>::
  1262. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  1263. _UniformRandomNumberGenerator& __urng,
  1264. const param_type& __p)
  1265. {
  1266. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>)
  1267. std::__detail::_Adaptor<_UniformRandomNumberGenerator, result_type>
  1268. __aurng(__urng);
  1269. while (__f != __t)
  1270. {
  1271. result_type __arg = result_type(1);
  1272. while (__arg == result_type(1) || __arg == result_type(0))
  1273. __arg = __aurng();
  1274. *__f++ = __p.a()
  1275. + __p.b() * std::log(__arg / (result_type(1) - __arg));
  1276. }
  1277. }
  1278. template<typename _RealType, typename _CharT, typename _Traits>
  1279. std::basic_ostream<_CharT, _Traits>&
  1280. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1281. const logistic_distribution<_RealType>& __x)
  1282. {
  1283. typedef std::basic_ostream<_CharT, _Traits> __ostream_type;
  1284. typedef typename __ostream_type::ios_base __ios_base;
  1285. const typename __ios_base::fmtflags __flags = __os.flags();
  1286. const _CharT __fill = __os.fill();
  1287. const std::streamsize __precision = __os.precision();
  1288. const _CharT __space = __os.widen(' ');
  1289. __os.flags(__ios_base::scientific | __ios_base::left);
  1290. __os.fill(__space);
  1291. __os.precision(std::numeric_limits<_RealType>::max_digits10);
  1292. __os << __x.a() << __space << __x.b();
  1293. __os.flags(__flags);
  1294. __os.fill(__fill);
  1295. __os.precision(__precision);
  1296. return __os;
  1297. }
  1298. template<typename _RealType, typename _CharT, typename _Traits>
  1299. std::basic_istream<_CharT, _Traits>&
  1300. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1301. logistic_distribution<_RealType>& __x)
  1302. {
  1303. typedef std::basic_istream<_CharT, _Traits> __istream_type;
  1304. typedef typename __istream_type::ios_base __ios_base;
  1305. const typename __ios_base::fmtflags __flags = __is.flags();
  1306. __is.flags(__ios_base::dec | __ios_base::skipws);
  1307. _RealType __a, __b;
  1308. __is >> __a >> __b;
  1309. __x.param(typename logistic_distribution<_RealType>::
  1310. param_type(__a, __b));
  1311. __is.flags(__flags);
  1312. return __is;
  1313. }
  1314. namespace {
  1315. // Helper class for the uniform_on_sphere_distribution generation
  1316. // function.
  1317. template<std::size_t _Dimen, typename _RealType>
  1318. class uniform_on_sphere_helper
  1319. {
  1320. typedef typename uniform_on_sphere_distribution<_Dimen, _RealType>::
  1321. result_type result_type;
  1322. public:
  1323. template<typename _NormalDistribution,
  1324. typename _UniformRandomNumberGenerator>
  1325. result_type operator()(_NormalDistribution& __nd,
  1326. _UniformRandomNumberGenerator& __urng)
  1327. {
  1328. result_type __ret;
  1329. typename result_type::value_type __norm;
  1330. do
  1331. {
  1332. auto __sum = _RealType(0);
  1333. std::generate(__ret.begin(), __ret.end(),
  1334. [&__nd, &__urng, &__sum](){
  1335. _RealType __t = __nd(__urng);
  1336. __sum += __t * __t;
  1337. return __t; });
  1338. __norm = std::sqrt(__sum);
  1339. }
  1340. while (__norm == _RealType(0) || ! std::isfinite(__norm));
  1341. std::transform(__ret.begin(), __ret.end(), __ret.begin(),
  1342. [__norm](_RealType __val){ return __val / __norm; });
  1343. return __ret;
  1344. }
  1345. };
  1346. template<typename _RealType>
  1347. class uniform_on_sphere_helper<2, _RealType>
  1348. {
  1349. typedef typename uniform_on_sphere_distribution<2, _RealType>::
  1350. result_type result_type;
  1351. public:
  1352. template<typename _NormalDistribution,
  1353. typename _UniformRandomNumberGenerator>
  1354. result_type operator()(_NormalDistribution&,
  1355. _UniformRandomNumberGenerator& __urng)
  1356. {
  1357. result_type __ret;
  1358. _RealType __sq;
  1359. std::__detail::_Adaptor<_UniformRandomNumberGenerator,
  1360. _RealType> __aurng(__urng);
  1361. do
  1362. {
  1363. __ret[0] = _RealType(2) * __aurng() - _RealType(1);
  1364. __ret[1] = _RealType(2) * __aurng() - _RealType(1);
  1365. __sq = __ret[0] * __ret[0] + __ret[1] * __ret[1];
  1366. }
  1367. while (__sq == _RealType(0) || __sq > _RealType(1));
  1368. #if _GLIBCXX_USE_C99_MATH_TR1
  1369. // Yes, we do not just use sqrt(__sq) because hypot() is more
  1370. // accurate.
  1371. auto __norm = std::hypot(__ret[0], __ret[1]);
  1372. #else
  1373. auto __norm = std::sqrt(__sq);
  1374. #endif
  1375. __ret[0] /= __norm;
  1376. __ret[1] /= __norm;
  1377. return __ret;
  1378. }
  1379. };
  1380. }
  1381. template<std::size_t _Dimen, typename _RealType>
  1382. template<typename _UniformRandomNumberGenerator>
  1383. typename uniform_on_sphere_distribution<_Dimen, _RealType>::result_type
  1384. uniform_on_sphere_distribution<_Dimen, _RealType>::
  1385. operator()(_UniformRandomNumberGenerator& __urng,
  1386. const param_type& __p)
  1387. {
  1388. uniform_on_sphere_helper<_Dimen, _RealType> __helper;
  1389. return __helper(_M_nd, __urng);
  1390. }
  1391. template<std::size_t _Dimen, typename _RealType>
  1392. template<typename _OutputIterator,
  1393. typename _UniformRandomNumberGenerator>
  1394. void
  1395. uniform_on_sphere_distribution<_Dimen, _RealType>::
  1396. __generate_impl(_OutputIterator __f, _OutputIterator __t,
  1397. _UniformRandomNumberGenerator& __urng,
  1398. const param_type& __param)
  1399. {
  1400. __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator>)
  1401. while (__f != __t)
  1402. *__f++ = this->operator()(__urng, __param);
  1403. }
  1404. template<std::size_t _Dimen, typename _RealType, typename _CharT,
  1405. typename _Traits>
  1406. std::basic_ostream<_CharT, _Traits>&
  1407. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  1408. const __gnu_cxx::uniform_on_sphere_distribution<_Dimen,
  1409. _RealType>& __x)
  1410. {
  1411. return __os << __x._M_nd;
  1412. }
  1413. template<std::size_t _Dimen, typename _RealType, typename _CharT,
  1414. typename _Traits>
  1415. std::basic_istream<_CharT, _Traits>&
  1416. operator>>(std::basic_istream<_CharT, _Traits>& __is,
  1417. __gnu_cxx::uniform_on_sphere_distribution<_Dimen,
  1418. _RealType>& __x)
  1419. {
  1420. return __is >> __x._M_nd;
  1421. }
  1422. _GLIBCXX_END_NAMESPACE_VERSION
  1423. } // namespace
  1424. #endif // _EXT_RANDOM_TCC