]>
Commit | Line | Data |
---|---|---|
8e79468d BK |
1 | // random number generation (out of line) -*- C++ -*- |
2 | ||
a5544970 | 3 | // Copyright (C) 2009-2019 Free Software Foundation, Inc. |
8e79468d BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
8e79468d BK |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
8e79468d | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
8e79468d BK |
24 | |
25 | /** @file bits/random.tcc | |
26 | * This is an internal header file, included by other library headers. | |
f910786b | 27 | * Do not attempt to use it directly. @headername{random} |
8e79468d BK |
28 | */ |
29 | ||
06f29237 PC |
30 | #ifndef _RANDOM_TCC |
31 | #define _RANDOM_TCC 1 | |
32 | ||
247d8075 | 33 | #include <numeric> // std::accumulate and std::partial_sum |
8e79468d | 34 | |
12ffa228 BK |
35 | namespace std _GLIBCXX_VISIBILITY(default) |
36 | { | |
4a15d842 FD |
37 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
38 | ||
8e79468d BK |
39 | /* |
40 | * (Further) implementation-space details. | |
41 | */ | |
42 | namespace __detail | |
43 | { | |
cf48c255 MG |
44 | // General case for x = (ax + c) mod m -- use Schrage's algorithm |
45 | // to avoid integer overflow. | |
8e79468d BK |
46 | // |
47 | // Preconditions: a > 0, m > 0. | |
48 | // | |
cf48c255 | 49 | // Note: only works correctly for __m % __a < __m / __a. |
b94f4bef | 50 | template<typename _Tp, _Tp __m, _Tp __a, _Tp __c> |
cf48c255 MG |
51 | _Tp |
52 | _Mod<_Tp, __m, __a, __c, false, true>:: | |
53 | __calc(_Tp __x) | |
8e79468d | 54 | { |
cf48c255 MG |
55 | if (__a == 1) |
56 | __x %= __m; | |
57 | else | |
58 | { | |
59 | static const _Tp __q = __m / __a; | |
60 | static const _Tp __r = __m % __a; | |
61 | ||
62 | _Tp __t1 = __a * (__x % __q); | |
63 | _Tp __t2 = __r * (__x / __q); | |
64 | if (__t1 >= __t2) | |
65 | __x = __t1 - __t2; | |
66 | else | |
67 | __x = __m - __t2 + __t1; | |
68 | } | |
69 | ||
70 | if (__c != 0) | |
71 | { | |
72 | const _Tp __d = __m - __x; | |
73 | if (__d > __c) | |
74 | __x += __c; | |
75 | else | |
76 | __x = __c - __d; | |
77 | } | |
78 | return __x; | |
79 | } | |
247d8075 | 80 | |
afcd05a7 | 81 | template<typename _InputIterator, typename _OutputIterator, |
60f3a59f | 82 | typename _Tp> |
afcd05a7 | 83 | _OutputIterator |
60f3a59f PC |
84 | __normalize(_InputIterator __first, _InputIterator __last, |
85 | _OutputIterator __result, const _Tp& __factor) | |
afcd05a7 PC |
86 | { |
87 | for (; __first != __last; ++__first, ++__result) | |
60f3a59f | 88 | *__result = *__first / __factor; |
afcd05a7 PC |
89 | return __result; |
90 | } | |
12ffa228 | 91 | |
8e79468d BK |
92 | } // namespace __detail |
93 | ||
300ea283 | 94 | template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> |
94a86be0 | 95 | constexpr _UIntType |
300ea283 PC |
96 | linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier; |
97 | ||
98 | template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | |
94a86be0 | 99 | constexpr _UIntType |
300ea283 PC |
100 | linear_congruential_engine<_UIntType, __a, __c, __m>::increment; |
101 | ||
102 | template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | |
94a86be0 | 103 | constexpr _UIntType |
300ea283 PC |
104 | linear_congruential_engine<_UIntType, __a, __c, __m>::modulus; |
105 | ||
106 | template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | |
94a86be0 | 107 | constexpr _UIntType |
300ea283 PC |
108 | linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed; |
109 | ||
8e79468d | 110 | /** |
96a9203b | 111 | * Seeds the LCR with integral value @p __s, adjusted so that the |
8e79468d BK |
112 | * ring identity is never a member of the convergence set. |
113 | */ | |
114 | template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | |
115 | void | |
116 | linear_congruential_engine<_UIntType, __a, __c, __m>:: | |
96a9203b | 117 | seed(result_type __s) |
8e79468d | 118 | { |
b94f4bef PC |
119 | if ((__detail::__mod<_UIntType, __m>(__c) == 0) |
120 | && (__detail::__mod<_UIntType, __m>(__s) == 0)) | |
121 | _M_x = 1; | |
8e79468d | 122 | else |
b94f4bef | 123 | _M_x = __detail::__mod<_UIntType, __m>(__s); |
8e79468d BK |
124 | } |
125 | ||
126 | /** | |
96a9203b | 127 | * Seeds the LCR engine with a value generated by @p __q. |
8e79468d BK |
128 | */ |
129 | template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m> | |
05eeebfe | 130 | template<typename _Sseq> |
5a7960da | 131 | auto |
15ecdcc6 PC |
132 | linear_congruential_engine<_UIntType, __a, __c, __m>:: |
133 | seed(_Sseq& __q) | |
5a7960da | 134 | -> _If_seed_seq<_Sseq> |
15ecdcc6 PC |
135 | { |
136 | const _UIntType __k0 = __m == 0 ? std::numeric_limits<_UIntType>::digits | |
137 | : std::__lg(__m); | |
138 | const _UIntType __k = (__k0 + 31) / 32; | |
139 | uint_least32_t __arr[__k + 3]; | |
140 | __q.generate(__arr + 0, __arr + __k + 3); | |
141 | _UIntType __factor = 1u; | |
142 | _UIntType __sum = 0u; | |
143 | for (size_t __j = 0; __j < __k; ++__j) | |
144 | { | |
145 | __sum += __arr[__j + 3] * __factor; | |
146 | __factor *= __detail::_Shift<_UIntType, 32>::__value; | |
147 | } | |
148 | seed(__sum); | |
149 | } | |
8e79468d | 150 | |
8e79468d BK |
151 | template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m, |
152 | typename _CharT, typename _Traits> | |
153 | std::basic_ostream<_CharT, _Traits>& | |
154 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
155 | const linear_congruential_engine<_UIntType, | |
156 | __a, __c, __m>& __lcr) | |
157 | { | |
158 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
159 | typedef typename __ostream_type::ios_base __ios_base; | |
160 | ||
161 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
162 | const _CharT __fill = __os.fill(); | |
95fe602e | 163 | __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left); |
8e79468d BK |
164 | __os.fill(__os.widen(' ')); |
165 | ||
166 | __os << __lcr._M_x; | |
167 | ||
168 | __os.flags(__flags); | |
169 | __os.fill(__fill); | |
170 | return __os; | |
171 | } | |
172 | ||
173 | template<typename _UIntType, _UIntType __a, _UIntType __c, _UIntType __m, | |
174 | typename _CharT, typename _Traits> | |
175 | std::basic_istream<_CharT, _Traits>& | |
176 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
177 | linear_congruential_engine<_UIntType, __a, __c, __m>& __lcr) | |
178 | { | |
179 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
180 | typedef typename __istream_type::ios_base __ios_base; | |
181 | ||
182 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
183 | __is.flags(__ios_base::dec); | |
184 | ||
185 | __is >> __lcr._M_x; | |
186 | ||
187 | __is.flags(__flags); | |
188 | return __is; | |
189 | } | |
190 | ||
191 | ||
300ea283 PC |
192 | template<typename _UIntType, |
193 | size_t __w, size_t __n, size_t __m, size_t __r, | |
194 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
195 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
196 | _UIntType __f> | |
94a86be0 | 197 | constexpr size_t |
300ea283 PC |
198 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
199 | __s, __b, __t, __c, __l, __f>::word_size; | |
200 | ||
201 | template<typename _UIntType, | |
202 | size_t __w, size_t __n, size_t __m, size_t __r, | |
203 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
204 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
205 | _UIntType __f> | |
94a86be0 | 206 | constexpr size_t |
300ea283 PC |
207 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
208 | __s, __b, __t, __c, __l, __f>::state_size; | |
209 | ||
210 | template<typename _UIntType, | |
211 | size_t __w, size_t __n, size_t __m, size_t __r, | |
212 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
213 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
214 | _UIntType __f> | |
94a86be0 | 215 | constexpr size_t |
300ea283 PC |
216 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
217 | __s, __b, __t, __c, __l, __f>::shift_size; | |
218 | ||
219 | template<typename _UIntType, | |
220 | size_t __w, size_t __n, size_t __m, size_t __r, | |
221 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
222 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
223 | _UIntType __f> | |
94a86be0 | 224 | constexpr size_t |
300ea283 PC |
225 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
226 | __s, __b, __t, __c, __l, __f>::mask_bits; | |
227 | ||
228 | template<typename _UIntType, | |
229 | size_t __w, size_t __n, size_t __m, size_t __r, | |
230 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
231 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
232 | _UIntType __f> | |
94a86be0 | 233 | constexpr _UIntType |
300ea283 PC |
234 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
235 | __s, __b, __t, __c, __l, __f>::xor_mask; | |
236 | ||
237 | template<typename _UIntType, | |
238 | size_t __w, size_t __n, size_t __m, size_t __r, | |
239 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
240 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
241 | _UIntType __f> | |
94a86be0 | 242 | constexpr size_t |
300ea283 PC |
243 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
244 | __s, __b, __t, __c, __l, __f>::tempering_u; | |
245 | ||
246 | template<typename _UIntType, | |
247 | size_t __w, size_t __n, size_t __m, size_t __r, | |
248 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
249 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
250 | _UIntType __f> | |
94a86be0 | 251 | constexpr _UIntType |
300ea283 PC |
252 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
253 | __s, __b, __t, __c, __l, __f>::tempering_d; | |
254 | ||
255 | template<typename _UIntType, | |
256 | size_t __w, size_t __n, size_t __m, size_t __r, | |
257 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
258 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
259 | _UIntType __f> | |
94a86be0 | 260 | constexpr size_t |
300ea283 PC |
261 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
262 | __s, __b, __t, __c, __l, __f>::tempering_s; | |
263 | ||
264 | template<typename _UIntType, | |
265 | size_t __w, size_t __n, size_t __m, size_t __r, | |
266 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
267 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
268 | _UIntType __f> | |
94a86be0 | 269 | constexpr _UIntType |
300ea283 PC |
270 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
271 | __s, __b, __t, __c, __l, __f>::tempering_b; | |
272 | ||
273 | template<typename _UIntType, | |
274 | size_t __w, size_t __n, size_t __m, size_t __r, | |
275 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
276 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
277 | _UIntType __f> | |
94a86be0 | 278 | constexpr size_t |
300ea283 PC |
279 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
280 | __s, __b, __t, __c, __l, __f>::tempering_t; | |
281 | ||
282 | template<typename _UIntType, | |
283 | size_t __w, size_t __n, size_t __m, size_t __r, | |
284 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
285 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
286 | _UIntType __f> | |
94a86be0 | 287 | constexpr _UIntType |
300ea283 PC |
288 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
289 | __s, __b, __t, __c, __l, __f>::tempering_c; | |
290 | ||
291 | template<typename _UIntType, | |
292 | size_t __w, size_t __n, size_t __m, size_t __r, | |
293 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
294 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
295 | _UIntType __f> | |
94a86be0 | 296 | constexpr size_t |
300ea283 PC |
297 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
298 | __s, __b, __t, __c, __l, __f>::tempering_l; | |
299 | ||
300 | template<typename _UIntType, | |
301 | size_t __w, size_t __n, size_t __m, size_t __r, | |
302 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
303 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
304 | _UIntType __f> | |
94a86be0 | 305 | constexpr _UIntType |
300ea283 PC |
306 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
307 | __s, __b, __t, __c, __l, __f>:: | |
308 | initialization_multiplier; | |
309 | ||
310 | template<typename _UIntType, | |
311 | size_t __w, size_t __n, size_t __m, size_t __r, | |
312 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
313 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
314 | _UIntType __f> | |
94a86be0 | 315 | constexpr _UIntType |
300ea283 PC |
316 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
317 | __s, __b, __t, __c, __l, __f>::default_seed; | |
318 | ||
8e79468d BK |
319 | template<typename _UIntType, |
320 | size_t __w, size_t __n, size_t __m, size_t __r, | |
321 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
322 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
323 | _UIntType __f> | |
324 | void | |
325 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, | |
326 | __s, __b, __t, __c, __l, __f>:: | |
327 | seed(result_type __sd) | |
328 | { | |
b94f4bef | 329 | _M_x[0] = __detail::__mod<_UIntType, |
8e79468d BK |
330 | __detail::_Shift<_UIntType, __w>::__value>(__sd); |
331 | ||
332 | for (size_t __i = 1; __i < state_size; ++__i) | |
333 | { | |
334 | _UIntType __x = _M_x[__i - 1]; | |
335 | __x ^= __x >> (__w - 2); | |
336 | __x *= __f; | |
b94f4bef PC |
337 | __x += __detail::__mod<_UIntType, __n>(__i); |
338 | _M_x[__i] = __detail::__mod<_UIntType, | |
8e79468d BK |
339 | __detail::_Shift<_UIntType, __w>::__value>(__x); |
340 | } | |
341 | _M_p = state_size; | |
342 | } | |
343 | ||
344 | template<typename _UIntType, | |
345 | size_t __w, size_t __n, size_t __m, size_t __r, | |
346 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
347 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
348 | _UIntType __f> | |
05eeebfe | 349 | template<typename _Sseq> |
5a7960da | 350 | auto |
15ecdcc6 | 351 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, |
8e79468d | 352 | __s, __b, __t, __c, __l, __f>:: |
15ecdcc6 | 353 | seed(_Sseq& __q) |
5a7960da | 354 | -> _If_seed_seq<_Sseq> |
15ecdcc6 PC |
355 | { |
356 | const _UIntType __upper_mask = (~_UIntType()) << __r; | |
357 | const size_t __k = (__w + 31) / 32; | |
358 | uint_least32_t __arr[__n * __k]; | |
359 | __q.generate(__arr + 0, __arr + __n * __k); | |
360 | ||
361 | bool __zero = true; | |
362 | for (size_t __i = 0; __i < state_size; ++__i) | |
363 | { | |
364 | _UIntType __factor = 1u; | |
365 | _UIntType __sum = 0u; | |
366 | for (size_t __j = 0; __j < __k; ++__j) | |
367 | { | |
368 | __sum += __arr[__k * __i + __j] * __factor; | |
369 | __factor *= __detail::_Shift<_UIntType, 32>::__value; | |
370 | } | |
371 | _M_x[__i] = __detail::__mod<_UIntType, | |
372 | __detail::_Shift<_UIntType, __w>::__value>(__sum); | |
373 | ||
374 | if (__zero) | |
375 | { | |
376 | if (__i == 0) | |
377 | { | |
378 | if ((_M_x[0] & __upper_mask) != 0u) | |
379 | __zero = false; | |
380 | } | |
381 | else if (_M_x[__i] != 0u) | |
382 | __zero = false; | |
383 | } | |
384 | } | |
8e79468d | 385 | if (__zero) |
b94f4bef | 386 | _M_x[0] = __detail::_Shift<_UIntType, __w - 1>::__value; |
935ec36f | 387 | _M_p = state_size; |
15ecdcc6 | 388 | } |
8e79468d | 389 | |
b668e41a UD |
390 | template<typename _UIntType, size_t __w, |
391 | size_t __n, size_t __m, size_t __r, | |
392 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
393 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
394 | _UIntType __f> | |
395 | void | |
396 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, | |
397 | __s, __b, __t, __c, __l, __f>:: | |
398 | _M_gen_rand(void) | |
399 | { | |
400 | const _UIntType __upper_mask = (~_UIntType()) << __r; | |
401 | const _UIntType __lower_mask = ~__upper_mask; | |
402 | ||
403 | for (size_t __k = 0; __k < (__n - __m); ++__k) | |
404 | { | |
405 | _UIntType __y = ((_M_x[__k] & __upper_mask) | |
406 | | (_M_x[__k + 1] & __lower_mask)); | |
407 | _M_x[__k] = (_M_x[__k + __m] ^ (__y >> 1) | |
408 | ^ ((__y & 0x01) ? __a : 0)); | |
409 | } | |
410 | ||
411 | for (size_t __k = (__n - __m); __k < (__n - 1); ++__k) | |
412 | { | |
413 | _UIntType __y = ((_M_x[__k] & __upper_mask) | |
414 | | (_M_x[__k + 1] & __lower_mask)); | |
415 | _M_x[__k] = (_M_x[__k + (__m - __n)] ^ (__y >> 1) | |
416 | ^ ((__y & 0x01) ? __a : 0)); | |
417 | } | |
418 | ||
419 | _UIntType __y = ((_M_x[__n - 1] & __upper_mask) | |
420 | | (_M_x[0] & __lower_mask)); | |
421 | _M_x[__n - 1] = (_M_x[__m - 1] ^ (__y >> 1) | |
422 | ^ ((__y & 0x01) ? __a : 0)); | |
423 | _M_p = 0; | |
424 | } | |
425 | ||
426 | template<typename _UIntType, size_t __w, | |
427 | size_t __n, size_t __m, size_t __r, | |
428 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
429 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
430 | _UIntType __f> | |
431 | void | |
432 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, | |
433 | __s, __b, __t, __c, __l, __f>:: | |
434 | discard(unsigned long long __z) | |
435 | { | |
436 | while (__z > state_size - _M_p) | |
437 | { | |
438 | __z -= state_size - _M_p; | |
439 | _M_gen_rand(); | |
440 | } | |
441 | _M_p += __z; | |
442 | } | |
443 | ||
8e79468d BK |
444 | template<typename _UIntType, size_t __w, |
445 | size_t __n, size_t __m, size_t __r, | |
446 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
447 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
448 | _UIntType __f> | |
449 | typename | |
450 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, | |
451 | __s, __b, __t, __c, __l, __f>::result_type | |
452 | mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, | |
453 | __s, __b, __t, __c, __l, __f>:: | |
454 | operator()() | |
455 | { | |
456 | // Reload the vector - cost is O(n) amortized over n calls. | |
457 | if (_M_p >= state_size) | |
b668e41a | 458 | _M_gen_rand(); |
8e79468d BK |
459 | |
460 | // Calculate o(x(i)). | |
461 | result_type __z = _M_x[_M_p++]; | |
462 | __z ^= (__z >> __u) & __d; | |
463 | __z ^= (__z << __s) & __b; | |
464 | __z ^= (__z << __t) & __c; | |
465 | __z ^= (__z >> __l); | |
466 | ||
467 | return __z; | |
468 | } | |
469 | ||
470 | template<typename _UIntType, size_t __w, | |
471 | size_t __n, size_t __m, size_t __r, | |
472 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
473 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
474 | _UIntType __f, typename _CharT, typename _Traits> | |
475 | std::basic_ostream<_CharT, _Traits>& | |
476 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
477 | const mersenne_twister_engine<_UIntType, __w, __n, __m, | |
478 | __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x) | |
479 | { | |
480 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
481 | typedef typename __ostream_type::ios_base __ios_base; | |
482 | ||
483 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
484 | const _CharT __fill = __os.fill(); | |
485 | const _CharT __space = __os.widen(' '); | |
95fe602e | 486 | __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left); |
8e79468d BK |
487 | __os.fill(__space); |
488 | ||
31645179 | 489 | for (size_t __i = 0; __i < __n; ++__i) |
8e79468d | 490 | __os << __x._M_x[__i] << __space; |
31645179 | 491 | __os << __x._M_p; |
8e79468d BK |
492 | |
493 | __os.flags(__flags); | |
494 | __os.fill(__fill); | |
495 | return __os; | |
496 | } | |
497 | ||
498 | template<typename _UIntType, size_t __w, | |
499 | size_t __n, size_t __m, size_t __r, | |
500 | _UIntType __a, size_t __u, _UIntType __d, size_t __s, | |
501 | _UIntType __b, size_t __t, _UIntType __c, size_t __l, | |
502 | _UIntType __f, typename _CharT, typename _Traits> | |
503 | std::basic_istream<_CharT, _Traits>& | |
504 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
505 | mersenne_twister_engine<_UIntType, __w, __n, __m, | |
506 | __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>& __x) | |
507 | { | |
508 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
509 | typedef typename __istream_type::ios_base __ios_base; | |
510 | ||
511 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
512 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
513 | ||
514 | for (size_t __i = 0; __i < __n; ++__i) | |
515 | __is >> __x._M_x[__i]; | |
31645179 | 516 | __is >> __x._M_p; |
8e79468d BK |
517 | |
518 | __is.flags(__flags); | |
519 | return __is; | |
520 | } | |
521 | ||
522 | ||
300ea283 | 523 | template<typename _UIntType, size_t __w, size_t __s, size_t __r> |
94a86be0 | 524 | constexpr size_t |
300ea283 PC |
525 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size; |
526 | ||
527 | template<typename _UIntType, size_t __w, size_t __s, size_t __r> | |
94a86be0 | 528 | constexpr size_t |
300ea283 PC |
529 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag; |
530 | ||
531 | template<typename _UIntType, size_t __w, size_t __s, size_t __r> | |
94a86be0 | 532 | constexpr size_t |
300ea283 PC |
533 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag; |
534 | ||
535 | template<typename _UIntType, size_t __w, size_t __s, size_t __r> | |
94a86be0 | 536 | constexpr _UIntType |
300ea283 PC |
537 | subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed; |
538 | ||
8e79468d BK |
539 | template<typename _UIntType, size_t __w, size_t __s, size_t __r> |
540 | void | |
541 | subtract_with_carry_engine<_UIntType, __w, __s, __r>:: | |
542 | seed(result_type __value) | |
543 | { | |
b94f4bef PC |
544 | std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> |
545 | __lcg(__value == 0u ? default_seed : __value); | |
8e79468d | 546 | |
b94f4bef | 547 | const size_t __n = (__w + 31) / 32; |
8e79468d BK |
548 | |
549 | for (size_t __i = 0; __i < long_lag; ++__i) | |
550 | { | |
b94f4bef PC |
551 | _UIntType __sum = 0u; |
552 | _UIntType __factor = 1u; | |
8e79468d BK |
553 | for (size_t __j = 0; __j < __n; ++__j) |
554 | { | |
b94f4bef PC |
555 | __sum += __detail::__mod<uint_least32_t, |
556 | __detail::_Shift<uint_least32_t, 32>::__value> | |
8e79468d BK |
557 | (__lcg()) * __factor; |
558 | __factor *= __detail::_Shift<_UIntType, 32>::__value; | |
559 | } | |
b94f4bef | 560 | _M_x[__i] = __detail::__mod<_UIntType, |
96a9203b | 561 | __detail::_Shift<_UIntType, __w>::__value>(__sum); |
8e79468d BK |
562 | } |
563 | _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0; | |
564 | _M_p = 0; | |
565 | } | |
566 | ||
567 | template<typename _UIntType, size_t __w, size_t __s, size_t __r> | |
05eeebfe | 568 | template<typename _Sseq> |
5a7960da | 569 | auto |
15ecdcc6 PC |
570 | subtract_with_carry_engine<_UIntType, __w, __s, __r>:: |
571 | seed(_Sseq& __q) | |
5a7960da | 572 | -> _If_seed_seq<_Sseq> |
15ecdcc6 PC |
573 | { |
574 | const size_t __k = (__w + 31) / 32; | |
575 | uint_least32_t __arr[__r * __k]; | |
576 | __q.generate(__arr + 0, __arr + __r * __k); | |
577 | ||
578 | for (size_t __i = 0; __i < long_lag; ++__i) | |
579 | { | |
580 | _UIntType __sum = 0u; | |
581 | _UIntType __factor = 1u; | |
582 | for (size_t __j = 0; __j < __k; ++__j) | |
583 | { | |
584 | __sum += __arr[__k * __i + __j] * __factor; | |
585 | __factor *= __detail::_Shift<_UIntType, 32>::__value; | |
586 | } | |
587 | _M_x[__i] = __detail::__mod<_UIntType, | |
588 | __detail::_Shift<_UIntType, __w>::__value>(__sum); | |
589 | } | |
590 | _M_carry = (_M_x[long_lag - 1] == 0) ? 1 : 0; | |
591 | _M_p = 0; | |
592 | } | |
8e79468d BK |
593 | |
594 | template<typename _UIntType, size_t __w, size_t __s, size_t __r> | |
595 | typename subtract_with_carry_engine<_UIntType, __w, __s, __r>:: | |
596 | result_type | |
597 | subtract_with_carry_engine<_UIntType, __w, __s, __r>:: | |
598 | operator()() | |
599 | { | |
600 | // Derive short lag index from current index. | |
601 | long __ps = _M_p - short_lag; | |
602 | if (__ps < 0) | |
603 | __ps += long_lag; | |
604 | ||
605 | // Calculate new x(i) without overflow or division. | |
606 | // NB: Thanks to the requirements for _UIntType, _M_x[_M_p] + _M_carry | |
607 | // cannot overflow. | |
608 | _UIntType __xi; | |
609 | if (_M_x[__ps] >= _M_x[_M_p] + _M_carry) | |
610 | { | |
611 | __xi = _M_x[__ps] - _M_x[_M_p] - _M_carry; | |
612 | _M_carry = 0; | |
613 | } | |
614 | else | |
615 | { | |
96a9203b PC |
616 | __xi = (__detail::_Shift<_UIntType, __w>::__value |
617 | - _M_x[_M_p] - _M_carry + _M_x[__ps]); | |
8e79468d BK |
618 | _M_carry = 1; |
619 | } | |
620 | _M_x[_M_p] = __xi; | |
621 | ||
622 | // Adjust current index to loop around in ring buffer. | |
623 | if (++_M_p >= long_lag) | |
624 | _M_p = 0; | |
625 | ||
626 | return __xi; | |
627 | } | |
628 | ||
629 | template<typename _UIntType, size_t __w, size_t __s, size_t __r, | |
630 | typename _CharT, typename _Traits> | |
631 | std::basic_ostream<_CharT, _Traits>& | |
632 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
633 | const subtract_with_carry_engine<_UIntType, | |
634 | __w, __s, __r>& __x) | |
635 | { | |
636 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
637 | typedef typename __ostream_type::ios_base __ios_base; | |
638 | ||
639 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
640 | const _CharT __fill = __os.fill(); | |
641 | const _CharT __space = __os.widen(' '); | |
95fe602e | 642 | __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left); |
8e79468d BK |
643 | __os.fill(__space); |
644 | ||
645 | for (size_t __i = 0; __i < __r; ++__i) | |
646 | __os << __x._M_x[__i] << __space; | |
31645179 | 647 | __os << __x._M_carry << __space << __x._M_p; |
8e79468d BK |
648 | |
649 | __os.flags(__flags); | |
650 | __os.fill(__fill); | |
651 | return __os; | |
652 | } | |
653 | ||
654 | template<typename _UIntType, size_t __w, size_t __s, size_t __r, | |
655 | typename _CharT, typename _Traits> | |
656 | std::basic_istream<_CharT, _Traits>& | |
657 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
658 | subtract_with_carry_engine<_UIntType, __w, __s, __r>& __x) | |
659 | { | |
660 | typedef std::basic_ostream<_CharT, _Traits> __istream_type; | |
661 | typedef typename __istream_type::ios_base __ios_base; | |
662 | ||
663 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
664 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
665 | ||
666 | for (size_t __i = 0; __i < __r; ++__i) | |
667 | __is >> __x._M_x[__i]; | |
668 | __is >> __x._M_carry; | |
31645179 | 669 | __is >> __x._M_p; |
8e79468d BK |
670 | |
671 | __is.flags(__flags); | |
672 | return __is; | |
673 | } | |
674 | ||
675 | ||
300ea283 | 676 | template<typename _RandomNumberEngine, size_t __p, size_t __r> |
94a86be0 | 677 | constexpr size_t |
300ea283 PC |
678 | discard_block_engine<_RandomNumberEngine, __p, __r>::block_size; |
679 | ||
680 | template<typename _RandomNumberEngine, size_t __p, size_t __r> | |
94a86be0 | 681 | constexpr size_t |
300ea283 PC |
682 | discard_block_engine<_RandomNumberEngine, __p, __r>::used_block; |
683 | ||
8e79468d BK |
684 | template<typename _RandomNumberEngine, size_t __p, size_t __r> |
685 | typename discard_block_engine<_RandomNumberEngine, | |
686 | __p, __r>::result_type | |
687 | discard_block_engine<_RandomNumberEngine, __p, __r>:: | |
688 | operator()() | |
689 | { | |
690 | if (_M_n >= used_block) | |
691 | { | |
692 | _M_b.discard(block_size - _M_n); | |
693 | _M_n = 0; | |
694 | } | |
695 | ++_M_n; | |
696 | return _M_b(); | |
697 | } | |
698 | ||
699 | template<typename _RandomNumberEngine, size_t __p, size_t __r, | |
700 | typename _CharT, typename _Traits> | |
701 | std::basic_ostream<_CharT, _Traits>& | |
702 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
703 | const discard_block_engine<_RandomNumberEngine, | |
704 | __p, __r>& __x) | |
705 | { | |
706 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
707 | typedef typename __ostream_type::ios_base __ios_base; | |
708 | ||
709 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
710 | const _CharT __fill = __os.fill(); | |
711 | const _CharT __space = __os.widen(' '); | |
95fe602e | 712 | __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left); |
8e79468d BK |
713 | __os.fill(__space); |
714 | ||
715 | __os << __x.base() << __space << __x._M_n; | |
716 | ||
717 | __os.flags(__flags); | |
718 | __os.fill(__fill); | |
719 | return __os; | |
720 | } | |
721 | ||
722 | template<typename _RandomNumberEngine, size_t __p, size_t __r, | |
723 | typename _CharT, typename _Traits> | |
724 | std::basic_istream<_CharT, _Traits>& | |
725 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
726 | discard_block_engine<_RandomNumberEngine, __p, __r>& __x) | |
727 | { | |
728 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
729 | typedef typename __istream_type::ios_base __ios_base; | |
730 | ||
731 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
732 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
733 | ||
734 | __is >> __x._M_b >> __x._M_n; | |
735 | ||
736 | __is.flags(__flags); | |
737 | return __is; | |
738 | } | |
739 | ||
740 | ||
741 | template<typename _RandomNumberEngine, size_t __w, typename _UIntType> | |
742 | typename independent_bits_engine<_RandomNumberEngine, __w, _UIntType>:: | |
743 | result_type | |
744 | independent_bits_engine<_RandomNumberEngine, __w, _UIntType>:: | |
745 | operator()() | |
746 | { | |
f84ca6e7 PC |
747 | typedef typename _RandomNumberEngine::result_type _Eresult_type; |
748 | const _Eresult_type __r | |
749 | = (_M_b.max() - _M_b.min() < std::numeric_limits<_Eresult_type>::max() | |
750 | ? _M_b.max() - _M_b.min() + 1 : 0); | |
751 | const unsigned __edig = std::numeric_limits<_Eresult_type>::digits; | |
752 | const unsigned __m = __r ? std::__lg(__r) : __edig; | |
753 | ||
754 | typedef typename std::common_type<_Eresult_type, result_type>::type | |
755 | __ctype; | |
756 | const unsigned __cdig = std::numeric_limits<__ctype>::digits; | |
757 | ||
758 | unsigned __n, __n0; | |
759 | __ctype __s0, __s1, __y0, __y1; | |
760 | ||
8e79468d BK |
761 | for (size_t __i = 0; __i < 2; ++__i) |
762 | { | |
763 | __n = (__w + __m - 1) / __m + __i; | |
764 | __n0 = __n - __w % __n; | |
f84ca6e7 PC |
765 | const unsigned __w0 = __w / __n; // __w0 <= __m |
766 | ||
767 | __s0 = 0; | |
768 | __s1 = 0; | |
769 | if (__w0 < __cdig) | |
770 | { | |
771 | __s0 = __ctype(1) << __w0; | |
772 | __s1 = __s0 << 1; | |
773 | } | |
774 | ||
775 | __y0 = 0; | |
776 | __y1 = 0; | |
777 | if (__r) | |
778 | { | |
779 | __y0 = __s0 * (__r / __s0); | |
780 | if (__s1) | |
781 | __y1 = __s1 * (__r / __s1); | |
782 | ||
783 | if (__r - __y0 <= __y0 / __n) | |
784 | break; | |
785 | } | |
786 | else | |
8e79468d BK |
787 | break; |
788 | } | |
789 | ||
790 | result_type __sum = 0; | |
791 | for (size_t __k = 0; __k < __n0; ++__k) | |
792 | { | |
f84ca6e7 | 793 | __ctype __u; |
8e79468d | 794 | do |
6855fe45 | 795 | __u = _M_b() - _M_b.min(); |
f84ca6e7 PC |
796 | while (__y0 && __u >= __y0); |
797 | __sum = __s0 * __sum + (__s0 ? __u % __s0 : __u); | |
8e79468d BK |
798 | } |
799 | for (size_t __k = __n0; __k < __n; ++__k) | |
800 | { | |
f84ca6e7 | 801 | __ctype __u; |
8e79468d | 802 | do |
6855fe45 | 803 | __u = _M_b() - _M_b.min(); |
f84ca6e7 PC |
804 | while (__y1 && __u >= __y1); |
805 | __sum = __s1 * __sum + (__s1 ? __u % __s1 : __u); | |
8e79468d BK |
806 | } |
807 | return __sum; | |
808 | } | |
809 | ||
810 | ||
300ea283 | 811 | template<typename _RandomNumberEngine, size_t __k> |
94a86be0 | 812 | constexpr size_t |
300ea283 PC |
813 | shuffle_order_engine<_RandomNumberEngine, __k>::table_size; |
814 | ||
8e79468d BK |
815 | template<typename _RandomNumberEngine, size_t __k> |
816 | typename shuffle_order_engine<_RandomNumberEngine, __k>::result_type | |
817 | shuffle_order_engine<_RandomNumberEngine, __k>:: | |
818 | operator()() | |
819 | { | |
96a9203b PC |
820 | size_t __j = __k * ((_M_y - _M_b.min()) |
821 | / (_M_b.max() - _M_b.min() + 1.0L)); | |
8e79468d BK |
822 | _M_y = _M_v[__j]; |
823 | _M_v[__j] = _M_b(); | |
824 | ||
825 | return _M_y; | |
826 | } | |
827 | ||
828 | template<typename _RandomNumberEngine, size_t __k, | |
829 | typename _CharT, typename _Traits> | |
830 | std::basic_ostream<_CharT, _Traits>& | |
831 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
832 | const shuffle_order_engine<_RandomNumberEngine, __k>& __x) | |
833 | { | |
834 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
835 | typedef typename __ostream_type::ios_base __ios_base; | |
836 | ||
837 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
838 | const _CharT __fill = __os.fill(); | |
839 | const _CharT __space = __os.widen(' '); | |
95fe602e | 840 | __os.flags(__ios_base::dec | __ios_base::fixed | __ios_base::left); |
8e79468d BK |
841 | __os.fill(__space); |
842 | ||
843 | __os << __x.base(); | |
844 | for (size_t __i = 0; __i < __k; ++__i) | |
845 | __os << __space << __x._M_v[__i]; | |
846 | __os << __space << __x._M_y; | |
847 | ||
848 | __os.flags(__flags); | |
849 | __os.fill(__fill); | |
850 | return __os; | |
851 | } | |
852 | ||
853 | template<typename _RandomNumberEngine, size_t __k, | |
854 | typename _CharT, typename _Traits> | |
855 | std::basic_istream<_CharT, _Traits>& | |
856 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
857 | shuffle_order_engine<_RandomNumberEngine, __k>& __x) | |
858 | { | |
859 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
860 | typedef typename __istream_type::ios_base __ios_base; | |
861 | ||
862 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
863 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
864 | ||
865 | __is >> __x._M_b; | |
866 | for (size_t __i = 0; __i < __k; ++__i) | |
867 | __is >> __x._M_v[__i]; | |
868 | __is >> __x._M_y; | |
869 | ||
870 | __is.flags(__flags); | |
871 | return __is; | |
872 | } | |
873 | ||
874 | ||
8e79468d BK |
875 | template<typename _IntType, typename _CharT, typename _Traits> |
876 | std::basic_ostream<_CharT, _Traits>& | |
877 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
878 | const uniform_int_distribution<_IntType>& __x) | |
879 | { | |
880 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
881 | typedef typename __ostream_type::ios_base __ios_base; | |
882 | ||
883 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
884 | const _CharT __fill = __os.fill(); | |
885 | const _CharT __space = __os.widen(' '); | |
886 | __os.flags(__ios_base::scientific | __ios_base::left); | |
887 | __os.fill(__space); | |
888 | ||
889 | __os << __x.a() << __space << __x.b(); | |
890 | ||
891 | __os.flags(__flags); | |
892 | __os.fill(__fill); | |
893 | return __os; | |
894 | } | |
895 | ||
896 | template<typename _IntType, typename _CharT, typename _Traits> | |
897 | std::basic_istream<_CharT, _Traits>& | |
898 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
899 | uniform_int_distribution<_IntType>& __x) | |
900 | { | |
901 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
902 | typedef typename __istream_type::ios_base __ios_base; | |
903 | ||
904 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
905 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
906 | ||
907 | _IntType __a, __b; | |
908 | __is >> __a >> __b; | |
909 | __x.param(typename uniform_int_distribution<_IntType>:: | |
910 | param_type(__a, __b)); | |
911 | ||
912 | __is.flags(__flags); | |
913 | return __is; | |
914 | } | |
915 | ||
916 | ||
7b93bdde UD |
917 | template<typename _RealType> |
918 | template<typename _ForwardIterator, | |
919 | typename _UniformRandomNumberGenerator> | |
920 | void | |
921 | uniform_real_distribution<_RealType>:: | |
922 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
923 | _UniformRandomNumberGenerator& __urng, | |
924 | const param_type& __p) | |
925 | { | |
926 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
927 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
928 | __aurng(__urng); | |
929 | auto __range = __p.b() - __p.a(); | |
930 | while (__f != __t) | |
931 | *__f++ = __aurng() * __range + __p.a(); | |
932 | } | |
933 | ||
8e79468d BK |
934 | template<typename _RealType, typename _CharT, typename _Traits> |
935 | std::basic_ostream<_CharT, _Traits>& | |
936 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
937 | const uniform_real_distribution<_RealType>& __x) | |
938 | { | |
939 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
940 | typedef typename __ostream_type::ios_base __ios_base; | |
941 | ||
942 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
943 | const _CharT __fill = __os.fill(); | |
944 | const std::streamsize __precision = __os.precision(); | |
945 | const _CharT __space = __os.widen(' '); | |
946 | __os.flags(__ios_base::scientific | __ios_base::left); | |
947 | __os.fill(__space); | |
7703dc47 | 948 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d BK |
949 | |
950 | __os << __x.a() << __space << __x.b(); | |
951 | ||
952 | __os.flags(__flags); | |
953 | __os.fill(__fill); | |
954 | __os.precision(__precision); | |
955 | return __os; | |
956 | } | |
957 | ||
958 | template<typename _RealType, typename _CharT, typename _Traits> | |
959 | std::basic_istream<_CharT, _Traits>& | |
960 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
961 | uniform_real_distribution<_RealType>& __x) | |
962 | { | |
963 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
964 | typedef typename __istream_type::ios_base __ios_base; | |
965 | ||
966 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
967 | __is.flags(__ios_base::skipws); | |
968 | ||
969 | _RealType __a, __b; | |
970 | __is >> __a >> __b; | |
971 | __x.param(typename uniform_real_distribution<_RealType>:: | |
972 | param_type(__a, __b)); | |
973 | ||
974 | __is.flags(__flags); | |
975 | return __is; | |
976 | } | |
977 | ||
978 | ||
7b93bdde UD |
979 | template<typename _ForwardIterator, |
980 | typename _UniformRandomNumberGenerator> | |
981 | void | |
982 | std::bernoulli_distribution:: | |
983 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
984 | _UniformRandomNumberGenerator& __urng, | |
985 | const param_type& __p) | |
986 | { | |
987 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
988 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> | |
989 | __aurng(__urng); | |
990 | auto __limit = __p.p() * (__aurng.max() - __aurng.min()); | |
991 | ||
992 | while (__f != __t) | |
993 | *__f++ = (__aurng() - __aurng.min()) < __limit; | |
994 | } | |
995 | ||
8e79468d BK |
996 | template<typename _CharT, typename _Traits> |
997 | std::basic_ostream<_CharT, _Traits>& | |
998 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
999 | const bernoulli_distribution& __x) | |
1000 | { | |
1001 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
1002 | typedef typename __ostream_type::ios_base __ios_base; | |
1003 | ||
1004 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
1005 | const _CharT __fill = __os.fill(); | |
1006 | const std::streamsize __precision = __os.precision(); | |
1007 | __os.flags(__ios_base::scientific | __ios_base::left); | |
1008 | __os.fill(__os.widen(' ')); | |
7703dc47 | 1009 | __os.precision(std::numeric_limits<double>::max_digits10); |
8e79468d BK |
1010 | |
1011 | __os << __x.p(); | |
1012 | ||
1013 | __os.flags(__flags); | |
1014 | __os.fill(__fill); | |
1015 | __os.precision(__precision); | |
1016 | return __os; | |
1017 | } | |
1018 | ||
1019 | ||
1020 | template<typename _IntType> | |
1021 | template<typename _UniformRandomNumberGenerator> | |
1022 | typename geometric_distribution<_IntType>::result_type | |
1023 | geometric_distribution<_IntType>:: | |
1024 | operator()(_UniformRandomNumberGenerator& __urng, | |
1025 | const param_type& __param) | |
1026 | { | |
1027 | // About the epsilon thing see this thread: | |
1028 | // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html | |
1029 | const double __naf = | |
1030 | (1 - std::numeric_limits<double>::epsilon()) / 2; | |
1031 | // The largest _RealType convertible to _IntType. | |
1032 | const double __thr = | |
1033 | std::numeric_limits<_IntType>::max() + __naf; | |
9b88236b | 1034 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> |
8e79468d BK |
1035 | __aurng(__urng); |
1036 | ||
1037 | double __cand; | |
1038 | do | |
c2d9083d | 1039 | __cand = std::floor(std::log(1.0 - __aurng()) / __param._M_log_1_p); |
8e79468d BK |
1040 | while (__cand >= __thr); |
1041 | ||
1042 | return result_type(__cand + __naf); | |
1043 | } | |
1044 | ||
7b93bdde UD |
1045 | template<typename _IntType> |
1046 | template<typename _ForwardIterator, | |
1047 | typename _UniformRandomNumberGenerator> | |
1048 | void | |
1049 | geometric_distribution<_IntType>:: | |
1050 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
1051 | _UniformRandomNumberGenerator& __urng, | |
1052 | const param_type& __param) | |
1053 | { | |
1054 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
1055 | // About the epsilon thing see this thread: | |
1056 | // http://gcc.gnu.org/ml/gcc-patches/2006-10/msg00971.html | |
1057 | const double __naf = | |
1058 | (1 - std::numeric_limits<double>::epsilon()) / 2; | |
1059 | // The largest _RealType convertible to _IntType. | |
1060 | const double __thr = | |
1061 | std::numeric_limits<_IntType>::max() + __naf; | |
1062 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> | |
1063 | __aurng(__urng); | |
1064 | ||
1065 | while (__f != __t) | |
1066 | { | |
1067 | double __cand; | |
1068 | do | |
c2d9083d HY |
1069 | __cand = std::floor(std::log(1.0 - __aurng()) |
1070 | / __param._M_log_1_p); | |
7b93bdde UD |
1071 | while (__cand >= __thr); |
1072 | ||
1073 | *__f++ = __cand + __naf; | |
1074 | } | |
1075 | } | |
1076 | ||
8e79468d BK |
1077 | template<typename _IntType, |
1078 | typename _CharT, typename _Traits> | |
1079 | std::basic_ostream<_CharT, _Traits>& | |
1080 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
1081 | const geometric_distribution<_IntType>& __x) | |
1082 | { | |
1083 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
1084 | typedef typename __ostream_type::ios_base __ios_base; | |
1085 | ||
1086 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
1087 | const _CharT __fill = __os.fill(); | |
1088 | const std::streamsize __precision = __os.precision(); | |
1089 | __os.flags(__ios_base::scientific | __ios_base::left); | |
1090 | __os.fill(__os.widen(' ')); | |
7703dc47 | 1091 | __os.precision(std::numeric_limits<double>::max_digits10); |
8e79468d BK |
1092 | |
1093 | __os << __x.p(); | |
1094 | ||
1095 | __os.flags(__flags); | |
1096 | __os.fill(__fill); | |
1097 | __os.precision(__precision); | |
1098 | return __os; | |
1099 | } | |
1100 | ||
1101 | template<typename _IntType, | |
1102 | typename _CharT, typename _Traits> | |
1103 | std::basic_istream<_CharT, _Traits>& | |
1104 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
1105 | geometric_distribution<_IntType>& __x) | |
1106 | { | |
1107 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
1108 | typedef typename __istream_type::ios_base __ios_base; | |
1109 | ||
1110 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
1111 | __is.flags(__ios_base::skipws); | |
1112 | ||
1113 | double __p; | |
1114 | __is >> __p; | |
1115 | __x.param(typename geometric_distribution<_IntType>::param_type(__p)); | |
1116 | ||
1117 | __is.flags(__flags); | |
1118 | return __is; | |
1119 | } | |
1120 | ||
ff2e697a | 1121 | // This is Leger's algorithm, also in Devroye, Ch. X, Example 1.5. |
f9b09dec PC |
1122 | template<typename _IntType> |
1123 | template<typename _UniformRandomNumberGenerator> | |
1124 | typename negative_binomial_distribution<_IntType>::result_type | |
1125 | negative_binomial_distribution<_IntType>:: | |
1126 | operator()(_UniformRandomNumberGenerator& __urng) | |
1127 | { | |
1128 | const double __y = _M_gd(__urng); | |
1129 | ||
1130 | // XXX Is the constructor too slow? | |
ff2e697a | 1131 | std::poisson_distribution<result_type> __poisson(__y); |
f9b09dec PC |
1132 | return __poisson(__urng); |
1133 | } | |
1134 | ||
8e79468d BK |
1135 | template<typename _IntType> |
1136 | template<typename _UniformRandomNumberGenerator> | |
1137 | typename negative_binomial_distribution<_IntType>::result_type | |
1138 | negative_binomial_distribution<_IntType>:: | |
1139 | operator()(_UniformRandomNumberGenerator& __urng, | |
1140 | const param_type& __p) | |
1141 | { | |
e5fbc9fc | 1142 | typedef typename std::gamma_distribution<double>::param_type |
f9b09dec PC |
1143 | param_type; |
1144 | ||
ff2e697a PC |
1145 | const double __y = |
1146 | _M_gd(__urng, param_type(__p.k(), (1.0 - __p.p()) / __p.p())); | |
8e79468d | 1147 | |
ff2e697a | 1148 | std::poisson_distribution<result_type> __poisson(__y); |
b01630bb | 1149 | return __poisson(__urng); |
8e79468d BK |
1150 | } |
1151 | ||
7b93bdde UD |
1152 | template<typename _IntType> |
1153 | template<typename _ForwardIterator, | |
1154 | typename _UniformRandomNumberGenerator> | |
1155 | void | |
1156 | negative_binomial_distribution<_IntType>:: | |
1157 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
1158 | _UniformRandomNumberGenerator& __urng) | |
1159 | { | |
1160 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
1161 | while (__f != __t) | |
1162 | { | |
1163 | const double __y = _M_gd(__urng); | |
1164 | ||
1165 | // XXX Is the constructor too slow? | |
1166 | std::poisson_distribution<result_type> __poisson(__y); | |
1167 | *__f++ = __poisson(__urng); | |
1168 | } | |
1169 | } | |
1170 | ||
1171 | template<typename _IntType> | |
1172 | template<typename _ForwardIterator, | |
1173 | typename _UniformRandomNumberGenerator> | |
1174 | void | |
1175 | negative_binomial_distribution<_IntType>:: | |
1176 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
1177 | _UniformRandomNumberGenerator& __urng, | |
1178 | const param_type& __p) | |
1179 | { | |
1180 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
1181 | typename std::gamma_distribution<result_type>::param_type | |
1182 | __p2(__p.k(), (1.0 - __p.p()) / __p.p()); | |
1183 | ||
1184 | while (__f != __t) | |
1185 | { | |
1186 | const double __y = _M_gd(__urng, __p2); | |
1187 | ||
1188 | std::poisson_distribution<result_type> __poisson(__y); | |
1189 | *__f++ = __poisson(__urng); | |
1190 | } | |
1191 | } | |
1192 | ||
8e79468d BK |
1193 | template<typename _IntType, typename _CharT, typename _Traits> |
1194 | std::basic_ostream<_CharT, _Traits>& | |
1195 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
1196 | const negative_binomial_distribution<_IntType>& __x) | |
1197 | { | |
1198 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
1199 | typedef typename __ostream_type::ios_base __ios_base; | |
1200 | ||
1201 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
1202 | const _CharT __fill = __os.fill(); | |
1203 | const std::streamsize __precision = __os.precision(); | |
1204 | const _CharT __space = __os.widen(' '); | |
1205 | __os.flags(__ios_base::scientific | __ios_base::left); | |
1206 | __os.fill(__os.widen(' ')); | |
7703dc47 | 1207 | __os.precision(std::numeric_limits<double>::max_digits10); |
8e79468d | 1208 | |
f9b09dec PC |
1209 | __os << __x.k() << __space << __x.p() |
1210 | << __space << __x._M_gd; | |
8e79468d BK |
1211 | |
1212 | __os.flags(__flags); | |
1213 | __os.fill(__fill); | |
1214 | __os.precision(__precision); | |
1215 | return __os; | |
1216 | } | |
1217 | ||
1218 | template<typename _IntType, typename _CharT, typename _Traits> | |
1219 | std::basic_istream<_CharT, _Traits>& | |
1220 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
1221 | negative_binomial_distribution<_IntType>& __x) | |
1222 | { | |
1223 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
1224 | typedef typename __istream_type::ios_base __ios_base; | |
1225 | ||
1226 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
1227 | __is.flags(__ios_base::skipws); | |
1228 | ||
1229 | _IntType __k; | |
1230 | double __p; | |
f9b09dec | 1231 | __is >> __k >> __p >> __x._M_gd; |
8e79468d BK |
1232 | __x.param(typename negative_binomial_distribution<_IntType>:: |
1233 | param_type(__k, __p)); | |
1234 | ||
1235 | __is.flags(__flags); | |
1236 | return __is; | |
1237 | } | |
1238 | ||
1239 | ||
1240 | template<typename _IntType> | |
1241 | void | |
1242 | poisson_distribution<_IntType>::param_type:: | |
1243 | _M_initialize() | |
1244 | { | |
1245 | #if _GLIBCXX_USE_C99_MATH_TR1 | |
1246 | if (_M_mean >= 12) | |
1247 | { | |
1248 | const double __m = std::floor(_M_mean); | |
1249 | _M_lm_thr = std::log(_M_mean); | |
1250 | _M_lfm = std::lgamma(__m + 1); | |
1251 | _M_sm = std::sqrt(__m); | |
1252 | ||
1253 | const double __pi_4 = 0.7853981633974483096156608458198757L; | |
1254 | const double __dx = std::sqrt(2 * __m * std::log(32 * __m | |
1255 | / __pi_4)); | |
3af7efb7 | 1256 | _M_d = std::round(std::max<double>(6.0, std::min(__m, __dx))); |
8e79468d BK |
1257 | const double __cx = 2 * __m + _M_d; |
1258 | _M_scx = std::sqrt(__cx / 2); | |
1259 | _M_1cx = 1 / __cx; | |
1260 | ||
1261 | _M_c2b = std::sqrt(__pi_4 * __cx) * std::exp(_M_1cx); | |
1262 | _M_cb = 2 * __cx * std::exp(-_M_d * _M_1cx * (1 + _M_d / 2)) | |
1263 | / _M_d; | |
1264 | } | |
1265 | else | |
1266 | #endif | |
1267 | _M_lm_thr = std::exp(-_M_mean); | |
1268 | } | |
1269 | ||
1270 | /** | |
1271 | * A rejection algorithm when mean >= 12 and a simple method based | |
1272 | * upon the multiplication of uniform random variates otherwise. | |
1273 | * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1 | |
1274 | * is defined. | |
1275 | * | |
1276 | * Reference: | |
2a60a9f6 | 1277 | * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag, |
8e79468d BK |
1278 | * New York, 1986, Ch. X, Sects. 3.3 & 3.4 (+ Errata!). |
1279 | */ | |
1280 | template<typename _IntType> | |
1281 | template<typename _UniformRandomNumberGenerator> | |
1282 | typename poisson_distribution<_IntType>::result_type | |
1283 | poisson_distribution<_IntType>:: | |
1284 | operator()(_UniformRandomNumberGenerator& __urng, | |
1285 | const param_type& __param) | |
1286 | { | |
1287 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> | |
1288 | __aurng(__urng); | |
1289 | #if _GLIBCXX_USE_C99_MATH_TR1 | |
1290 | if (__param.mean() >= 12) | |
1291 | { | |
1292 | double __x; | |
1293 | ||
1294 | // See comments above... | |
1295 | const double __naf = | |
1296 | (1 - std::numeric_limits<double>::epsilon()) / 2; | |
1297 | const double __thr = | |
1298 | std::numeric_limits<_IntType>::max() + __naf; | |
1299 | ||
1300 | const double __m = std::floor(__param.mean()); | |
1301 | // sqrt(pi / 2) | |
1302 | const double __spi_2 = 1.2533141373155002512078826424055226L; | |
1303 | const double __c1 = __param._M_sm * __spi_2; | |
1304 | const double __c2 = __param._M_c2b + __c1; | |
1305 | const double __c3 = __c2 + 1; | |
1306 | const double __c4 = __c3 + 1; | |
73986c31 MP |
1307 | // 1 / 78 |
1308 | const double __178 = 0.0128205128205128205128205128205128L; | |
8e79468d BK |
1309 | // e^(1 / 78) |
1310 | const double __e178 = 1.0129030479320018583185514777512983L; | |
1311 | const double __c5 = __c4 + __e178; | |
1312 | const double __c = __param._M_cb + __c5; | |
1313 | const double __2cx = 2 * (2 * __m + __param._M_d); | |
1314 | ||
1315 | bool __reject = true; | |
1316 | do | |
1317 | { | |
1318 | const double __u = __c * __aurng(); | |
c2d9083d | 1319 | const double __e = -std::log(1.0 - __aurng()); |
8e79468d BK |
1320 | |
1321 | double __w = 0.0; | |
1322 | ||
1323 | if (__u <= __c1) | |
1324 | { | |
1325 | const double __n = _M_nd(__urng); | |
1326 | const double __y = -std::abs(__n) * __param._M_sm - 1; | |
1327 | __x = std::floor(__y); | |
1328 | __w = -__n * __n / 2; | |
1329 | if (__x < -__m) | |
1330 | continue; | |
1331 | } | |
1332 | else if (__u <= __c2) | |
1333 | { | |
1334 | const double __n = _M_nd(__urng); | |
1335 | const double __y = 1 + std::abs(__n) * __param._M_scx; | |
1336 | __x = std::ceil(__y); | |
1337 | __w = __y * (2 - __y) * __param._M_1cx; | |
1338 | if (__x > __param._M_d) | |
1339 | continue; | |
1340 | } | |
1341 | else if (__u <= __c3) | |
1342 | // NB: This case not in the book, nor in the Errata, | |
1343 | // but should be ok... | |
1344 | __x = -1; | |
1345 | else if (__u <= __c4) | |
1346 | __x = 0; | |
1347 | else if (__u <= __c5) | |
73986c31 MP |
1348 | { |
1349 | __x = 1; | |
1350 | // Only in the Errata, see libstdc++/83237. | |
1351 | __w = __178; | |
1352 | } | |
8e79468d BK |
1353 | else |
1354 | { | |
c2d9083d | 1355 | const double __v = -std::log(1.0 - __aurng()); |
8e79468d BK |
1356 | const double __y = __param._M_d |
1357 | + __v * __2cx / __param._M_d; | |
1358 | __x = std::ceil(__y); | |
1359 | __w = -__param._M_d * __param._M_1cx * (1 + __y / 2); | |
1360 | } | |
1361 | ||
1362 | __reject = (__w - __e - __x * __param._M_lm_thr | |
1363 | > __param._M_lfm - std::lgamma(__x + __m + 1)); | |
1364 | ||
1365 | __reject |= __x + __m >= __thr; | |
1366 | ||
1367 | } while (__reject); | |
1368 | ||
1369 | return result_type(__x + __m + __naf); | |
1370 | } | |
1371 | else | |
1372 | #endif | |
1373 | { | |
1374 | _IntType __x = 0; | |
1375 | double __prod = 1.0; | |
1376 | ||
1377 | do | |
1378 | { | |
1379 | __prod *= __aurng(); | |
1380 | __x += 1; | |
1381 | } | |
1382 | while (__prod > __param._M_lm_thr); | |
1383 | ||
1384 | return __x - 1; | |
1385 | } | |
1386 | } | |
1387 | ||
7b93bdde UD |
1388 | template<typename _IntType> |
1389 | template<typename _ForwardIterator, | |
1390 | typename _UniformRandomNumberGenerator> | |
1391 | void | |
1392 | poisson_distribution<_IntType>:: | |
1393 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
1394 | _UniformRandomNumberGenerator& __urng, | |
1395 | const param_type& __param) | |
1396 | { | |
1397 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
1398 | // We could duplicate everything from operator()... | |
1399 | while (__f != __t) | |
1400 | *__f++ = this->operator()(__urng, __param); | |
1401 | } | |
1402 | ||
8e79468d BK |
1403 | template<typename _IntType, |
1404 | typename _CharT, typename _Traits> | |
1405 | std::basic_ostream<_CharT, _Traits>& | |
1406 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
1407 | const poisson_distribution<_IntType>& __x) | |
1408 | { | |
1409 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
1410 | typedef typename __ostream_type::ios_base __ios_base; | |
1411 | ||
1412 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
1413 | const _CharT __fill = __os.fill(); | |
1414 | const std::streamsize __precision = __os.precision(); | |
1415 | const _CharT __space = __os.widen(' '); | |
1416 | __os.flags(__ios_base::scientific | __ios_base::left); | |
1417 | __os.fill(__space); | |
7703dc47 | 1418 | __os.precision(std::numeric_limits<double>::max_digits10); |
8e79468d BK |
1419 | |
1420 | __os << __x.mean() << __space << __x._M_nd; | |
1421 | ||
1422 | __os.flags(__flags); | |
1423 | __os.fill(__fill); | |
1424 | __os.precision(__precision); | |
1425 | return __os; | |
1426 | } | |
1427 | ||
1428 | template<typename _IntType, | |
1429 | typename _CharT, typename _Traits> | |
1430 | std::basic_istream<_CharT, _Traits>& | |
1431 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
1432 | poisson_distribution<_IntType>& __x) | |
1433 | { | |
1434 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
1435 | typedef typename __istream_type::ios_base __ios_base; | |
1436 | ||
1437 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
1438 | __is.flags(__ios_base::skipws); | |
1439 | ||
1440 | double __mean; | |
1441 | __is >> __mean >> __x._M_nd; | |
1442 | __x.param(typename poisson_distribution<_IntType>::param_type(__mean)); | |
1443 | ||
1444 | __is.flags(__flags); | |
1445 | return __is; | |
1446 | } | |
1447 | ||
1448 | ||
1449 | template<typename _IntType> | |
1450 | void | |
1451 | binomial_distribution<_IntType>::param_type:: | |
1452 | _M_initialize() | |
1453 | { | |
1454 | const double __p12 = _M_p <= 0.5 ? _M_p : 1.0 - _M_p; | |
1455 | ||
1456 | _M_easy = true; | |
1457 | ||
1458 | #if _GLIBCXX_USE_C99_MATH_TR1 | |
1459 | if (_M_t * __p12 >= 8) | |
1460 | { | |
1461 | _M_easy = false; | |
1462 | const double __np = std::floor(_M_t * __p12); | |
1463 | const double __pa = __np / _M_t; | |
1464 | const double __1p = 1 - __pa; | |
1465 | ||
1466 | const double __pi_4 = 0.7853981633974483096156608458198757L; | |
1467 | const double __d1x = | |
1468 | std::sqrt(__np * __1p * std::log(32 * __np | |
1469 | / (81 * __pi_4 * __1p))); | |
3af7efb7 | 1470 | _M_d1 = std::round(std::max<double>(1.0, __d1x)); |
8e79468d BK |
1471 | const double __d2x = |
1472 | std::sqrt(__np * __1p * std::log(32 * _M_t * __1p | |
1473 | / (__pi_4 * __pa))); | |
3af7efb7 | 1474 | _M_d2 = std::round(std::max<double>(1.0, __d2x)); |
8e79468d BK |
1475 | |
1476 | // sqrt(pi / 2) | |
1477 | const double __spi_2 = 1.2533141373155002512078826424055226L; | |
1478 | _M_s1 = std::sqrt(__np * __1p) * (1 + _M_d1 / (4 * __np)); | |
1479 | _M_s2 = std::sqrt(__np * __1p) * (1 + _M_d2 / (4 * _M_t * __1p)); | |
1480 | _M_c = 2 * _M_d1 / __np; | |
1481 | _M_a1 = std::exp(_M_c) * _M_s1 * __spi_2; | |
1482 | const double __a12 = _M_a1 + _M_s2 * __spi_2; | |
1483 | const double __s1s = _M_s1 * _M_s1; | |
1484 | _M_a123 = __a12 + (std::exp(_M_d1 / (_M_t * __1p)) | |
1485 | * 2 * __s1s / _M_d1 | |
1486 | * std::exp(-_M_d1 * _M_d1 / (2 * __s1s))); | |
1487 | const double __s2s = _M_s2 * _M_s2; | |
1488 | _M_s = (_M_a123 + 2 * __s2s / _M_d2 | |
1489 | * std::exp(-_M_d2 * _M_d2 / (2 * __s2s))); | |
1490 | _M_lf = (std::lgamma(__np + 1) | |
1491 | + std::lgamma(_M_t - __np + 1)); | |
1492 | _M_lp1p = std::log(__pa / __1p); | |
1493 | ||
1494 | _M_q = -std::log(1 - (__p12 - __pa) / __1p); | |
1495 | } | |
1496 | else | |
1497 | #endif | |
1498 | _M_q = -std::log(1 - __p12); | |
1499 | } | |
1500 | ||
1501 | template<typename _IntType> | |
1502 | template<typename _UniformRandomNumberGenerator> | |
1503 | typename binomial_distribution<_IntType>::result_type | |
1504 | binomial_distribution<_IntType>:: | |
07bba3b1 PC |
1505 | _M_waiting(_UniformRandomNumberGenerator& __urng, |
1506 | _IntType __t, double __q) | |
8e79468d BK |
1507 | { |
1508 | _IntType __x = 0; | |
1509 | double __sum = 0.0; | |
1510 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> | |
1511 | __aurng(__urng); | |
1512 | ||
1513 | do | |
1514 | { | |
85018f40 | 1515 | if (__t == __x) |
9ea146e6 MLI |
1516 | return __x; |
1517 | const double __e = -std::log(1.0 - __aurng()); | |
8e79468d BK |
1518 | __sum += __e / (__t - __x); |
1519 | __x += 1; | |
1520 | } | |
07bba3b1 | 1521 | while (__sum <= __q); |
8e79468d BK |
1522 | |
1523 | return __x - 1; | |
1524 | } | |
1525 | ||
1526 | /** | |
1527 | * A rejection algorithm when t * p >= 8 and a simple waiting time | |
1528 | * method - the second in the referenced book - otherwise. | |
1529 | * NB: The former is available only if _GLIBCXX_USE_C99_MATH_TR1 | |
1530 | * is defined. | |
1531 | * | |
1532 | * Reference: | |
2a60a9f6 | 1533 | * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag, |
8e79468d BK |
1534 | * New York, 1986, Ch. X, Sect. 4 (+ Errata!). |
1535 | */ | |
1536 | template<typename _IntType> | |
1537 | template<typename _UniformRandomNumberGenerator> | |
1538 | typename binomial_distribution<_IntType>::result_type | |
1539 | binomial_distribution<_IntType>:: | |
1540 | operator()(_UniformRandomNumberGenerator& __urng, | |
1541 | const param_type& __param) | |
1542 | { | |
1543 | result_type __ret; | |
1544 | const _IntType __t = __param.t(); | |
d8d4db33 | 1545 | const double __p = __param.p(); |
8e79468d BK |
1546 | const double __p12 = __p <= 0.5 ? __p : 1.0 - __p; |
1547 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> | |
1548 | __aurng(__urng); | |
1549 | ||
1550 | #if _GLIBCXX_USE_C99_MATH_TR1 | |
1551 | if (!__param._M_easy) | |
1552 | { | |
1553 | double __x; | |
1554 | ||
1555 | // See comments above... | |
1556 | const double __naf = | |
1557 | (1 - std::numeric_limits<double>::epsilon()) / 2; | |
1558 | const double __thr = | |
1559 | std::numeric_limits<_IntType>::max() + __naf; | |
1560 | ||
1561 | const double __np = std::floor(__t * __p12); | |
1562 | ||
1563 | // sqrt(pi / 2) | |
1564 | const double __spi_2 = 1.2533141373155002512078826424055226L; | |
1565 | const double __a1 = __param._M_a1; | |
1566 | const double __a12 = __a1 + __param._M_s2 * __spi_2; | |
1567 | const double __a123 = __param._M_a123; | |
1568 | const double __s1s = __param._M_s1 * __param._M_s1; | |
1569 | const double __s2s = __param._M_s2 * __param._M_s2; | |
1570 | ||
1571 | bool __reject; | |
1572 | do | |
1573 | { | |
1574 | const double __u = __param._M_s * __aurng(); | |
1575 | ||
1576 | double __v; | |
1577 | ||
1578 | if (__u <= __a1) | |
1579 | { | |
1580 | const double __n = _M_nd(__urng); | |
1581 | const double __y = __param._M_s1 * std::abs(__n); | |
1582 | __reject = __y >= __param._M_d1; | |
1583 | if (!__reject) | |
1584 | { | |
c2d9083d | 1585 | const double __e = -std::log(1.0 - __aurng()); |
8e79468d BK |
1586 | __x = std::floor(__y); |
1587 | __v = -__e - __n * __n / 2 + __param._M_c; | |
1588 | } | |
1589 | } | |
1590 | else if (__u <= __a12) | |
1591 | { | |
1592 | const double __n = _M_nd(__urng); | |
1593 | const double __y = __param._M_s2 * std::abs(__n); | |
1594 | __reject = __y >= __param._M_d2; | |
1595 | if (!__reject) | |
1596 | { | |
c2d9083d | 1597 | const double __e = -std::log(1.0 - __aurng()); |
8e79468d BK |
1598 | __x = std::floor(-__y); |
1599 | __v = -__e - __n * __n / 2; | |
1600 | } | |
1601 | } | |
1602 | else if (__u <= __a123) | |
1603 | { | |
c2d9083d HY |
1604 | const double __e1 = -std::log(1.0 - __aurng()); |
1605 | const double __e2 = -std::log(1.0 - __aurng()); | |
8e79468d BK |
1606 | |
1607 | const double __y = __param._M_d1 | |
1608 | + 2 * __s1s * __e1 / __param._M_d1; | |
1609 | __x = std::floor(__y); | |
1610 | __v = (-__e2 + __param._M_d1 * (1 / (__t - __np) | |
1611 | -__y / (2 * __s1s))); | |
1612 | __reject = false; | |
1613 | } | |
1614 | else | |
1615 | { | |
c2d9083d HY |
1616 | const double __e1 = -std::log(1.0 - __aurng()); |
1617 | const double __e2 = -std::log(1.0 - __aurng()); | |
8e79468d BK |
1618 | |
1619 | const double __y = __param._M_d2 | |
1620 | + 2 * __s2s * __e1 / __param._M_d2; | |
1621 | __x = std::floor(-__y); | |
1622 | __v = -__e2 - __param._M_d2 * __y / (2 * __s2s); | |
1623 | __reject = false; | |
1624 | } | |
1625 | ||
1626 | __reject = __reject || __x < -__np || __x > __t - __np; | |
1627 | if (!__reject) | |
1628 | { | |
1629 | const double __lfx = | |
1630 | std::lgamma(__np + __x + 1) | |
1631 | + std::lgamma(__t - (__np + __x) + 1); | |
1632 | __reject = __v > __param._M_lf - __lfx | |
1633 | + __x * __param._M_lp1p; | |
1634 | } | |
1635 | ||
1636 | __reject |= __x + __np >= __thr; | |
1637 | } | |
1638 | while (__reject); | |
1639 | ||
1640 | __x += __np + __naf; | |
1641 | ||
07bba3b1 PC |
1642 | const _IntType __z = _M_waiting(__urng, __t - _IntType(__x), |
1643 | __param._M_q); | |
8e79468d BK |
1644 | __ret = _IntType(__x) + __z; |
1645 | } | |
1646 | else | |
1647 | #endif | |
07bba3b1 | 1648 | __ret = _M_waiting(__urng, __t, __param._M_q); |
8e79468d BK |
1649 | |
1650 | if (__p12 != __p) | |
1651 | __ret = __t - __ret; | |
1652 | return __ret; | |
1653 | } | |
1654 | ||
7b93bdde UD |
1655 | template<typename _IntType> |
1656 | template<typename _ForwardIterator, | |
1657 | typename _UniformRandomNumberGenerator> | |
1658 | void | |
1659 | binomial_distribution<_IntType>:: | |
1660 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
1661 | _UniformRandomNumberGenerator& __urng, | |
1662 | const param_type& __param) | |
1663 | { | |
1664 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
1665 | // We could duplicate everything from operator()... | |
1666 | while (__f != __t) | |
1667 | *__f++ = this->operator()(__urng, __param); | |
1668 | } | |
1669 | ||
8e79468d BK |
1670 | template<typename _IntType, |
1671 | typename _CharT, typename _Traits> | |
1672 | std::basic_ostream<_CharT, _Traits>& | |
1673 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
1674 | const binomial_distribution<_IntType>& __x) | |
1675 | { | |
1676 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
1677 | typedef typename __ostream_type::ios_base __ios_base; | |
1678 | ||
1679 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
1680 | const _CharT __fill = __os.fill(); | |
1681 | const std::streamsize __precision = __os.precision(); | |
1682 | const _CharT __space = __os.widen(' '); | |
1683 | __os.flags(__ios_base::scientific | __ios_base::left); | |
1684 | __os.fill(__space); | |
7703dc47 | 1685 | __os.precision(std::numeric_limits<double>::max_digits10); |
8e79468d BK |
1686 | |
1687 | __os << __x.t() << __space << __x.p() | |
1688 | << __space << __x._M_nd; | |
1689 | ||
1690 | __os.flags(__flags); | |
1691 | __os.fill(__fill); | |
1692 | __os.precision(__precision); | |
1693 | return __os; | |
1694 | } | |
1695 | ||
1696 | template<typename _IntType, | |
1697 | typename _CharT, typename _Traits> | |
1698 | std::basic_istream<_CharT, _Traits>& | |
1699 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
1700 | binomial_distribution<_IntType>& __x) | |
1701 | { | |
1702 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
1703 | typedef typename __istream_type::ios_base __ios_base; | |
1704 | ||
1705 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
1706 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
1707 | ||
1708 | _IntType __t; | |
1709 | double __p; | |
1710 | __is >> __t >> __p >> __x._M_nd; | |
1711 | __x.param(typename binomial_distribution<_IntType>:: | |
1712 | param_type(__t, __p)); | |
1713 | ||
1714 | __is.flags(__flags); | |
1715 | return __is; | |
1716 | } | |
1717 | ||
1718 | ||
7b93bdde UD |
1719 | template<typename _RealType> |
1720 | template<typename _ForwardIterator, | |
1721 | typename _UniformRandomNumberGenerator> | |
1722 | void | |
1723 | std::exponential_distribution<_RealType>:: | |
1724 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
1725 | _UniformRandomNumberGenerator& __urng, | |
1726 | const param_type& __p) | |
1727 | { | |
1728 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
1729 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
1730 | __aurng(__urng); | |
1731 | while (__f != __t) | |
c2d9083d | 1732 | *__f++ = -std::log(result_type(1) - __aurng()) / __p.lambda(); |
7b93bdde UD |
1733 | } |
1734 | ||
8e79468d BK |
1735 | template<typename _RealType, typename _CharT, typename _Traits> |
1736 | std::basic_ostream<_CharT, _Traits>& | |
1737 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
1738 | const exponential_distribution<_RealType>& __x) | |
1739 | { | |
1740 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
1741 | typedef typename __ostream_type::ios_base __ios_base; | |
1742 | ||
1743 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
1744 | const _CharT __fill = __os.fill(); | |
1745 | const std::streamsize __precision = __os.precision(); | |
1746 | __os.flags(__ios_base::scientific | __ios_base::left); | |
1747 | __os.fill(__os.widen(' ')); | |
7703dc47 | 1748 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d BK |
1749 | |
1750 | __os << __x.lambda(); | |
1751 | ||
1752 | __os.flags(__flags); | |
1753 | __os.fill(__fill); | |
1754 | __os.precision(__precision); | |
1755 | return __os; | |
1756 | } | |
1757 | ||
1758 | template<typename _RealType, typename _CharT, typename _Traits> | |
1759 | std::basic_istream<_CharT, _Traits>& | |
1760 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
1761 | exponential_distribution<_RealType>& __x) | |
1762 | { | |
1763 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
1764 | typedef typename __istream_type::ios_base __ios_base; | |
1765 | ||
1766 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
1767 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
1768 | ||
1769 | _RealType __lambda; | |
1770 | __is >> __lambda; | |
1771 | __x.param(typename exponential_distribution<_RealType>:: | |
1772 | param_type(__lambda)); | |
1773 | ||
1774 | __is.flags(__flags); | |
1775 | return __is; | |
1776 | } | |
1777 | ||
1778 | ||
8e79468d BK |
1779 | /** |
1780 | * Polar method due to Marsaglia. | |
1781 | * | |
2a60a9f6 | 1782 | * Devroye, L. Non-Uniform Random Variates Generation. Springer-Verlag, |
8e79468d BK |
1783 | * New York, 1986, Ch. V, Sect. 4.4. |
1784 | */ | |
1785 | template<typename _RealType> | |
1786 | template<typename _UniformRandomNumberGenerator> | |
1787 | typename normal_distribution<_RealType>::result_type | |
1788 | normal_distribution<_RealType>:: | |
1789 | operator()(_UniformRandomNumberGenerator& __urng, | |
1790 | const param_type& __param) | |
1791 | { | |
1792 | result_type __ret; | |
1793 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
1794 | __aurng(__urng); | |
1795 | ||
1796 | if (_M_saved_available) | |
1797 | { | |
1798 | _M_saved_available = false; | |
1799 | __ret = _M_saved; | |
1800 | } | |
1801 | else | |
1802 | { | |
1803 | result_type __x, __y, __r2; | |
1804 | do | |
1805 | { | |
1806 | __x = result_type(2.0) * __aurng() - 1.0; | |
1807 | __y = result_type(2.0) * __aurng() - 1.0; | |
1808 | __r2 = __x * __x + __y * __y; | |
1809 | } | |
1810 | while (__r2 > 1.0 || __r2 == 0.0); | |
1811 | ||
1812 | const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2); | |
1813 | _M_saved = __x * __mult; | |
1814 | _M_saved_available = true; | |
1815 | __ret = __y * __mult; | |
1816 | } | |
1817 | ||
1818 | __ret = __ret * __param.stddev() + __param.mean(); | |
1819 | return __ret; | |
1820 | } | |
1821 | ||
7b93bdde UD |
1822 | template<typename _RealType> |
1823 | template<typename _ForwardIterator, | |
1824 | typename _UniformRandomNumberGenerator> | |
1825 | void | |
1826 | normal_distribution<_RealType>:: | |
1827 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
1828 | _UniformRandomNumberGenerator& __urng, | |
1829 | const param_type& __param) | |
1830 | { | |
1831 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
1832 | ||
1833 | if (__f == __t) | |
1834 | return; | |
1835 | ||
1836 | if (_M_saved_available) | |
1837 | { | |
1838 | _M_saved_available = false; | |
1839 | *__f++ = _M_saved * __param.stddev() + __param.mean(); | |
1840 | ||
1841 | if (__f == __t) | |
1842 | return; | |
1843 | } | |
1844 | ||
1845 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
1846 | __aurng(__urng); | |
1847 | ||
1848 | while (__f + 1 < __t) | |
1849 | { | |
1850 | result_type __x, __y, __r2; | |
1851 | do | |
1852 | { | |
1853 | __x = result_type(2.0) * __aurng() - 1.0; | |
1854 | __y = result_type(2.0) * __aurng() - 1.0; | |
1855 | __r2 = __x * __x + __y * __y; | |
1856 | } | |
1857 | while (__r2 > 1.0 || __r2 == 0.0); | |
1858 | ||
1859 | const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2); | |
1860 | *__f++ = __y * __mult * __param.stddev() + __param.mean(); | |
1861 | *__f++ = __x * __mult * __param.stddev() + __param.mean(); | |
1862 | } | |
1863 | ||
1864 | if (__f != __t) | |
1865 | { | |
1866 | result_type __x, __y, __r2; | |
1867 | do | |
1868 | { | |
1869 | __x = result_type(2.0) * __aurng() - 1.0; | |
1870 | __y = result_type(2.0) * __aurng() - 1.0; | |
1871 | __r2 = __x * __x + __y * __y; | |
1872 | } | |
1873 | while (__r2 > 1.0 || __r2 == 0.0); | |
1874 | ||
1875 | const result_type __mult = std::sqrt(-2 * std::log(__r2) / __r2); | |
1876 | _M_saved = __x * __mult; | |
1877 | _M_saved_available = true; | |
1878 | *__f = __y * __mult * __param.stddev() + __param.mean(); | |
1879 | } | |
1880 | } | |
1881 | ||
fc05e1ba PC |
1882 | template<typename _RealType> |
1883 | bool | |
1884 | operator==(const std::normal_distribution<_RealType>& __d1, | |
1885 | const std::normal_distribution<_RealType>& __d2) | |
1886 | { | |
1887 | if (__d1._M_param == __d2._M_param | |
1888 | && __d1._M_saved_available == __d2._M_saved_available) | |
1889 | { | |
1890 | if (__d1._M_saved_available | |
1891 | && __d1._M_saved == __d2._M_saved) | |
1892 | return true; | |
1893 | else if(!__d1._M_saved_available) | |
1894 | return true; | |
1895 | else | |
1896 | return false; | |
1897 | } | |
1898 | else | |
1899 | return false; | |
1900 | } | |
1901 | ||
8e79468d BK |
1902 | template<typename _RealType, typename _CharT, typename _Traits> |
1903 | std::basic_ostream<_CharT, _Traits>& | |
1904 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
1905 | const normal_distribution<_RealType>& __x) | |
1906 | { | |
1907 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
1908 | typedef typename __ostream_type::ios_base __ios_base; | |
1909 | ||
1910 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
1911 | const _CharT __fill = __os.fill(); | |
1912 | const std::streamsize __precision = __os.precision(); | |
1913 | const _CharT __space = __os.widen(' '); | |
1914 | __os.flags(__ios_base::scientific | __ios_base::left); | |
1915 | __os.fill(__space); | |
7703dc47 | 1916 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d BK |
1917 | |
1918 | __os << __x.mean() << __space << __x.stddev() | |
1919 | << __space << __x._M_saved_available; | |
1920 | if (__x._M_saved_available) | |
1921 | __os << __space << __x._M_saved; | |
1922 | ||
1923 | __os.flags(__flags); | |
1924 | __os.fill(__fill); | |
1925 | __os.precision(__precision); | |
1926 | return __os; | |
1927 | } | |
1928 | ||
1929 | template<typename _RealType, typename _CharT, typename _Traits> | |
1930 | std::basic_istream<_CharT, _Traits>& | |
1931 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
1932 | normal_distribution<_RealType>& __x) | |
1933 | { | |
1934 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
1935 | typedef typename __istream_type::ios_base __ios_base; | |
1936 | ||
1937 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
1938 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
1939 | ||
1940 | double __mean, __stddev; | |
1941 | __is >> __mean >> __stddev | |
1942 | >> __x._M_saved_available; | |
1943 | if (__x._M_saved_available) | |
1944 | __is >> __x._M_saved; | |
1945 | __x.param(typename normal_distribution<_RealType>:: | |
1946 | param_type(__mean, __stddev)); | |
1947 | ||
1948 | __is.flags(__flags); | |
1949 | return __is; | |
1950 | } | |
1951 | ||
1952 | ||
7b93bdde UD |
1953 | template<typename _RealType> |
1954 | template<typename _ForwardIterator, | |
1955 | typename _UniformRandomNumberGenerator> | |
1956 | void | |
1957 | lognormal_distribution<_RealType>:: | |
1958 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
1959 | _UniformRandomNumberGenerator& __urng, | |
1960 | const param_type& __p) | |
1961 | { | |
1962 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
1963 | while (__f != __t) | |
1964 | *__f++ = std::exp(__p.s() * _M_nd(__urng) + __p.m()); | |
1965 | } | |
1966 | ||
8e79468d BK |
1967 | template<typename _RealType, typename _CharT, typename _Traits> |
1968 | std::basic_ostream<_CharT, _Traits>& | |
1969 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
1970 | const lognormal_distribution<_RealType>& __x) | |
1971 | { | |
1972 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
1973 | typedef typename __ostream_type::ios_base __ios_base; | |
1974 | ||
1975 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
1976 | const _CharT __fill = __os.fill(); | |
1977 | const std::streamsize __precision = __os.precision(); | |
1978 | const _CharT __space = __os.widen(' '); | |
1979 | __os.flags(__ios_base::scientific | __ios_base::left); | |
1980 | __os.fill(__space); | |
7703dc47 | 1981 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d | 1982 | |
b01630bb PC |
1983 | __os << __x.m() << __space << __x.s() |
1984 | << __space << __x._M_nd; | |
8e79468d BK |
1985 | |
1986 | __os.flags(__flags); | |
1987 | __os.fill(__fill); | |
1988 | __os.precision(__precision); | |
1989 | return __os; | |
1990 | } | |
1991 | ||
1992 | template<typename _RealType, typename _CharT, typename _Traits> | |
1993 | std::basic_istream<_CharT, _Traits>& | |
1994 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
1995 | lognormal_distribution<_RealType>& __x) | |
1996 | { | |
1997 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
1998 | typedef typename __istream_type::ios_base __ios_base; | |
1999 | ||
2000 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
2001 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
2002 | ||
2003 | _RealType __m, __s; | |
b01630bb | 2004 | __is >> __m >> __s >> __x._M_nd; |
8e79468d BK |
2005 | __x.param(typename lognormal_distribution<_RealType>:: |
2006 | param_type(__m, __s)); | |
2007 | ||
2008 | __is.flags(__flags); | |
2009 | return __is; | |
2010 | } | |
2011 | ||
5bcb3b4d PC |
2012 | template<typename _RealType> |
2013 | template<typename _ForwardIterator, | |
2014 | typename _UniformRandomNumberGenerator> | |
2015 | void | |
2016 | std::chi_squared_distribution<_RealType>:: | |
2017 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2018 | _UniformRandomNumberGenerator& __urng) | |
2019 | { | |
2020 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2021 | while (__f != __t) | |
2022 | *__f++ = 2 * _M_gd(__urng); | |
2023 | } | |
8e79468d | 2024 | |
7b93bdde UD |
2025 | template<typename _RealType> |
2026 | template<typename _ForwardIterator, | |
2027 | typename _UniformRandomNumberGenerator> | |
2028 | void | |
2029 | std::chi_squared_distribution<_RealType>:: | |
2030 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2031 | _UniformRandomNumberGenerator& __urng, | |
5bcb3b4d PC |
2032 | const typename |
2033 | std::gamma_distribution<result_type>::param_type& __p) | |
7b93bdde UD |
2034 | { |
2035 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2036 | while (__f != __t) | |
2037 | *__f++ = 2 * _M_gd(__urng, __p); | |
2038 | } | |
2039 | ||
8e79468d BK |
2040 | template<typename _RealType, typename _CharT, typename _Traits> |
2041 | std::basic_ostream<_CharT, _Traits>& | |
2042 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
2043 | const chi_squared_distribution<_RealType>& __x) | |
2044 | { | |
2045 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
2046 | typedef typename __ostream_type::ios_base __ios_base; | |
2047 | ||
2048 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
2049 | const _CharT __fill = __os.fill(); | |
2050 | const std::streamsize __precision = __os.precision(); | |
2051 | const _CharT __space = __os.widen(' '); | |
2052 | __os.flags(__ios_base::scientific | __ios_base::left); | |
2053 | __os.fill(__space); | |
7703dc47 | 2054 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d | 2055 | |
f9b09dec | 2056 | __os << __x.n() << __space << __x._M_gd; |
8e79468d BK |
2057 | |
2058 | __os.flags(__flags); | |
2059 | __os.fill(__fill); | |
2060 | __os.precision(__precision); | |
2061 | return __os; | |
2062 | } | |
2063 | ||
2064 | template<typename _RealType, typename _CharT, typename _Traits> | |
2065 | std::basic_istream<_CharT, _Traits>& | |
2066 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
2067 | chi_squared_distribution<_RealType>& __x) | |
2068 | { | |
2069 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
2070 | typedef typename __istream_type::ios_base __ios_base; | |
2071 | ||
2072 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
2073 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
2074 | ||
2075 | _RealType __n; | |
f9b09dec | 2076 | __is >> __n >> __x._M_gd; |
8e79468d BK |
2077 | __x.param(typename chi_squared_distribution<_RealType>:: |
2078 | param_type(__n)); | |
2079 | ||
2080 | __is.flags(__flags); | |
2081 | return __is; | |
2082 | } | |
2083 | ||
2084 | ||
2085 | template<typename _RealType> | |
2086 | template<typename _UniformRandomNumberGenerator> | |
2087 | typename cauchy_distribution<_RealType>::result_type | |
2088 | cauchy_distribution<_RealType>:: | |
2089 | operator()(_UniformRandomNumberGenerator& __urng, | |
2090 | const param_type& __p) | |
2091 | { | |
2092 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
2093 | __aurng(__urng); | |
2094 | _RealType __u; | |
2095 | do | |
e1a02963 | 2096 | __u = __aurng(); |
8e79468d BK |
2097 | while (__u == 0.5); |
2098 | ||
e1a02963 PC |
2099 | const _RealType __pi = 3.1415926535897932384626433832795029L; |
2100 | return __p.a() + __p.b() * std::tan(__pi * __u); | |
8e79468d BK |
2101 | } |
2102 | ||
7b93bdde UD |
2103 | template<typename _RealType> |
2104 | template<typename _ForwardIterator, | |
2105 | typename _UniformRandomNumberGenerator> | |
2106 | void | |
2107 | cauchy_distribution<_RealType>:: | |
2108 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2109 | _UniformRandomNumberGenerator& __urng, | |
2110 | const param_type& __p) | |
2111 | { | |
2112 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2113 | const _RealType __pi = 3.1415926535897932384626433832795029L; | |
2114 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
2115 | __aurng(__urng); | |
2116 | while (__f != __t) | |
2117 | { | |
2118 | _RealType __u; | |
2119 | do | |
2120 | __u = __aurng(); | |
2121 | while (__u == 0.5); | |
2122 | ||
2123 | *__f++ = __p.a() + __p.b() * std::tan(__pi * __u); | |
2124 | } | |
2125 | } | |
2126 | ||
8e79468d BK |
2127 | template<typename _RealType, typename _CharT, typename _Traits> |
2128 | std::basic_ostream<_CharT, _Traits>& | |
2129 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
2130 | const cauchy_distribution<_RealType>& __x) | |
2131 | { | |
2132 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
2133 | typedef typename __ostream_type::ios_base __ios_base; | |
2134 | ||
2135 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
2136 | const _CharT __fill = __os.fill(); | |
2137 | const std::streamsize __precision = __os.precision(); | |
2138 | const _CharT __space = __os.widen(' '); | |
2139 | __os.flags(__ios_base::scientific | __ios_base::left); | |
2140 | __os.fill(__space); | |
7703dc47 | 2141 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d BK |
2142 | |
2143 | __os << __x.a() << __space << __x.b(); | |
2144 | ||
2145 | __os.flags(__flags); | |
2146 | __os.fill(__fill); | |
2147 | __os.precision(__precision); | |
2148 | return __os; | |
2149 | } | |
2150 | ||
2151 | template<typename _RealType, typename _CharT, typename _Traits> | |
2152 | std::basic_istream<_CharT, _Traits>& | |
2153 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
2154 | cauchy_distribution<_RealType>& __x) | |
2155 | { | |
2156 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
2157 | typedef typename __istream_type::ios_base __ios_base; | |
2158 | ||
2159 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
2160 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
2161 | ||
2162 | _RealType __a, __b; | |
2163 | __is >> __a >> __b; | |
2164 | __x.param(typename cauchy_distribution<_RealType>:: | |
2165 | param_type(__a, __b)); | |
2166 | ||
2167 | __is.flags(__flags); | |
2168 | return __is; | |
2169 | } | |
2170 | ||
2171 | ||
7b93bdde UD |
2172 | template<typename _RealType> |
2173 | template<typename _ForwardIterator, | |
2174 | typename _UniformRandomNumberGenerator> | |
2175 | void | |
2176 | std::fisher_f_distribution<_RealType>:: | |
2177 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2178 | _UniformRandomNumberGenerator& __urng) | |
2179 | { | |
2180 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2181 | while (__f != __t) | |
2182 | *__f++ = ((_M_gd_x(__urng) * n()) / (_M_gd_y(__urng) * m())); | |
2183 | } | |
2184 | ||
2185 | template<typename _RealType> | |
2186 | template<typename _ForwardIterator, | |
2187 | typename _UniformRandomNumberGenerator> | |
2188 | void | |
2189 | std::fisher_f_distribution<_RealType>:: | |
2190 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2191 | _UniformRandomNumberGenerator& __urng, | |
2192 | const param_type& __p) | |
2193 | { | |
2194 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2195 | typedef typename std::gamma_distribution<result_type>::param_type | |
2196 | param_type; | |
2197 | param_type __p1(__p.m() / 2); | |
2198 | param_type __p2(__p.n() / 2); | |
2199 | while (__f != __t) | |
2200 | *__f++ = ((_M_gd_x(__urng, __p1) * n()) | |
2201 | / (_M_gd_y(__urng, __p2) * m())); | |
2202 | } | |
2203 | ||
8e79468d BK |
2204 | template<typename _RealType, typename _CharT, typename _Traits> |
2205 | std::basic_ostream<_CharT, _Traits>& | |
2206 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
2207 | const fisher_f_distribution<_RealType>& __x) | |
2208 | { | |
2209 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
2210 | typedef typename __ostream_type::ios_base __ios_base; | |
2211 | ||
2212 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
2213 | const _CharT __fill = __os.fill(); | |
2214 | const std::streamsize __precision = __os.precision(); | |
2215 | const _CharT __space = __os.widen(' '); | |
2216 | __os.flags(__ios_base::scientific | __ios_base::left); | |
2217 | __os.fill(__space); | |
7703dc47 | 2218 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d | 2219 | |
f9b09dec PC |
2220 | __os << __x.m() << __space << __x.n() |
2221 | << __space << __x._M_gd_x << __space << __x._M_gd_y; | |
8e79468d BK |
2222 | |
2223 | __os.flags(__flags); | |
2224 | __os.fill(__fill); | |
2225 | __os.precision(__precision); | |
2226 | return __os; | |
2227 | } | |
2228 | ||
2229 | template<typename _RealType, typename _CharT, typename _Traits> | |
2230 | std::basic_istream<_CharT, _Traits>& | |
2231 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
2232 | fisher_f_distribution<_RealType>& __x) | |
2233 | { | |
2234 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
2235 | typedef typename __istream_type::ios_base __ios_base; | |
2236 | ||
2237 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
2238 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
2239 | ||
2240 | _RealType __m, __n; | |
f9b09dec | 2241 | __is >> __m >> __n >> __x._M_gd_x >> __x._M_gd_y; |
8e79468d BK |
2242 | __x.param(typename fisher_f_distribution<_RealType>:: |
2243 | param_type(__m, __n)); | |
2244 | ||
2245 | __is.flags(__flags); | |
2246 | return __is; | |
2247 | } | |
2248 | ||
2249 | ||
7b93bdde UD |
2250 | template<typename _RealType> |
2251 | template<typename _ForwardIterator, | |
2252 | typename _UniformRandomNumberGenerator> | |
2253 | void | |
2254 | std::student_t_distribution<_RealType>:: | |
2255 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2256 | _UniformRandomNumberGenerator& __urng) | |
2257 | { | |
2258 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2259 | while (__f != __t) | |
2260 | *__f++ = _M_nd(__urng) * std::sqrt(n() / _M_gd(__urng)); | |
2261 | } | |
2262 | ||
2263 | template<typename _RealType> | |
2264 | template<typename _ForwardIterator, | |
2265 | typename _UniformRandomNumberGenerator> | |
2266 | void | |
2267 | std::student_t_distribution<_RealType>:: | |
2268 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2269 | _UniformRandomNumberGenerator& __urng, | |
2270 | const param_type& __p) | |
2271 | { | |
2272 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2273 | typename std::gamma_distribution<result_type>::param_type | |
2274 | __p2(__p.n() / 2, 2); | |
2275 | while (__f != __t) | |
2276 | *__f++ = _M_nd(__urng) * std::sqrt(__p.n() / _M_gd(__urng, __p2)); | |
2277 | } | |
2278 | ||
8e79468d BK |
2279 | template<typename _RealType, typename _CharT, typename _Traits> |
2280 | std::basic_ostream<_CharT, _Traits>& | |
2281 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
2282 | const student_t_distribution<_RealType>& __x) | |
2283 | { | |
2284 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
2285 | typedef typename __ostream_type::ios_base __ios_base; | |
2286 | ||
2287 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
2288 | const _CharT __fill = __os.fill(); | |
2289 | const std::streamsize __precision = __os.precision(); | |
2290 | const _CharT __space = __os.widen(' '); | |
2291 | __os.flags(__ios_base::scientific | __ios_base::left); | |
2292 | __os.fill(__space); | |
7703dc47 | 2293 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d | 2294 | |
f9b09dec | 2295 | __os << __x.n() << __space << __x._M_nd << __space << __x._M_gd; |
8e79468d BK |
2296 | |
2297 | __os.flags(__flags); | |
2298 | __os.fill(__fill); | |
2299 | __os.precision(__precision); | |
2300 | return __os; | |
2301 | } | |
2302 | ||
2303 | template<typename _RealType, typename _CharT, typename _Traits> | |
2304 | std::basic_istream<_CharT, _Traits>& | |
2305 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
2306 | student_t_distribution<_RealType>& __x) | |
2307 | { | |
2308 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
2309 | typedef typename __istream_type::ios_base __ios_base; | |
2310 | ||
2311 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
2312 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
2313 | ||
2314 | _RealType __n; | |
f9b09dec | 2315 | __is >> __n >> __x._M_nd >> __x._M_gd; |
8e79468d BK |
2316 | __x.param(typename student_t_distribution<_RealType>::param_type(__n)); |
2317 | ||
2318 | __is.flags(__flags); | |
2319 | return __is; | |
2320 | } | |
2321 | ||
2322 | ||
2323 | template<typename _RealType> | |
2324 | void | |
2325 | gamma_distribution<_RealType>::param_type:: | |
2326 | _M_initialize() | |
2327 | { | |
b01630bb PC |
2328 | _M_malpha = _M_alpha < 1.0 ? _M_alpha + _RealType(1.0) : _M_alpha; |
2329 | ||
2330 | const _RealType __a1 = _M_malpha - _RealType(1.0) / _RealType(3.0); | |
2331 | _M_a2 = _RealType(1.0) / std::sqrt(_RealType(9.0) * __a1); | |
8e79468d BK |
2332 | } |
2333 | ||
2334 | /** | |
b01630bb PC |
2335 | * Marsaglia, G. and Tsang, W. W. |
2336 | * "A Simple Method for Generating Gamma Variables" | |
2337 | * ACM Transactions on Mathematical Software, 26, 3, 363-372, 2000. | |
8e79468d BK |
2338 | */ |
2339 | template<typename _RealType> | |
2340 | template<typename _UniformRandomNumberGenerator> | |
2341 | typename gamma_distribution<_RealType>::result_type | |
2342 | gamma_distribution<_RealType>:: | |
2343 | operator()(_UniformRandomNumberGenerator& __urng, | |
2344 | const param_type& __param) | |
2345 | { | |
8e79468d BK |
2346 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> |
2347 | __aurng(__urng); | |
2348 | ||
b01630bb PC |
2349 | result_type __u, __v, __n; |
2350 | const result_type __a1 = (__param._M_malpha | |
2351 | - _RealType(1.0) / _RealType(3.0)); | |
8e79468d | 2352 | |
b01630bb PC |
2353 | do |
2354 | { | |
8e79468d BK |
2355 | do |
2356 | { | |
b01630bb PC |
2357 | __n = _M_nd(__urng); |
2358 | __v = result_type(1.0) + __param._M_a2 * __n; | |
8e79468d | 2359 | } |
b01630bb PC |
2360 | while (__v <= 0.0); |
2361 | ||
2362 | __v = __v * __v * __v; | |
2363 | __u = __aurng(); | |
8e79468d | 2364 | } |
957221f5 | 2365 | while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n |
b01630bb PC |
2366 | && (std::log(__u) > (0.5 * __n * __n + __a1 |
2367 | * (1.0 - __v + std::log(__v))))); | |
2368 | ||
2369 | if (__param.alpha() == __param._M_malpha) | |
2370 | return __a1 * __v * __param.beta(); | |
8e79468d BK |
2371 | else |
2372 | { | |
8e79468d | 2373 | do |
b01630bb PC |
2374 | __u = __aurng(); |
2375 | while (__u == 0.0); | |
2376 | ||
2377 | return (std::pow(__u, result_type(1.0) / __param.alpha()) | |
2378 | * __a1 * __v * __param.beta()); | |
8e79468d | 2379 | } |
8e79468d BK |
2380 | } |
2381 | ||
7b93bdde UD |
2382 | template<typename _RealType> |
2383 | template<typename _ForwardIterator, | |
2384 | typename _UniformRandomNumberGenerator> | |
2385 | void | |
2386 | gamma_distribution<_RealType>:: | |
2387 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2388 | _UniformRandomNumberGenerator& __urng, | |
2389 | const param_type& __param) | |
2390 | { | |
2391 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2392 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
2393 | __aurng(__urng); | |
2394 | ||
2395 | result_type __u, __v, __n; | |
2396 | const result_type __a1 = (__param._M_malpha | |
2397 | - _RealType(1.0) / _RealType(3.0)); | |
2398 | ||
2399 | if (__param.alpha() == __param._M_malpha) | |
2400 | while (__f != __t) | |
2401 | { | |
2402 | do | |
2403 | { | |
2404 | do | |
2405 | { | |
2406 | __n = _M_nd(__urng); | |
2407 | __v = result_type(1.0) + __param._M_a2 * __n; | |
2408 | } | |
2409 | while (__v <= 0.0); | |
2410 | ||
2411 | __v = __v * __v * __v; | |
2412 | __u = __aurng(); | |
2413 | } | |
6fa8c51f | 2414 | while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n |
7b93bdde UD |
2415 | && (std::log(__u) > (0.5 * __n * __n + __a1 |
2416 | * (1.0 - __v + std::log(__v))))); | |
2417 | ||
2418 | *__f++ = __a1 * __v * __param.beta(); | |
2419 | } | |
2420 | else | |
2421 | while (__f != __t) | |
2422 | { | |
2423 | do | |
2424 | { | |
2425 | do | |
2426 | { | |
2427 | __n = _M_nd(__urng); | |
2428 | __v = result_type(1.0) + __param._M_a2 * __n; | |
2429 | } | |
2430 | while (__v <= 0.0); | |
2431 | ||
2432 | __v = __v * __v * __v; | |
2433 | __u = __aurng(); | |
2434 | } | |
6fa8c51f | 2435 | while (__u > result_type(1.0) - 0.0331 * __n * __n * __n * __n |
7b93bdde UD |
2436 | && (std::log(__u) > (0.5 * __n * __n + __a1 |
2437 | * (1.0 - __v + std::log(__v))))); | |
2438 | ||
2439 | do | |
2440 | __u = __aurng(); | |
2441 | while (__u == 0.0); | |
2442 | ||
2443 | *__f++ = (std::pow(__u, result_type(1.0) / __param.alpha()) | |
2444 | * __a1 * __v * __param.beta()); | |
2445 | } | |
2446 | } | |
2447 | ||
8e79468d BK |
2448 | template<typename _RealType, typename _CharT, typename _Traits> |
2449 | std::basic_ostream<_CharT, _Traits>& | |
2450 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
2451 | const gamma_distribution<_RealType>& __x) | |
2452 | { | |
2453 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
2454 | typedef typename __ostream_type::ios_base __ios_base; | |
2455 | ||
2456 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
2457 | const _CharT __fill = __os.fill(); | |
2458 | const std::streamsize __precision = __os.precision(); | |
2459 | const _CharT __space = __os.widen(' '); | |
2460 | __os.flags(__ios_base::scientific | __ios_base::left); | |
2461 | __os.fill(__space); | |
7703dc47 | 2462 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d | 2463 | |
b01630bb PC |
2464 | __os << __x.alpha() << __space << __x.beta() |
2465 | << __space << __x._M_nd; | |
8e79468d BK |
2466 | |
2467 | __os.flags(__flags); | |
2468 | __os.fill(__fill); | |
2469 | __os.precision(__precision); | |
2470 | return __os; | |
2471 | } | |
2472 | ||
2473 | template<typename _RealType, typename _CharT, typename _Traits> | |
2474 | std::basic_istream<_CharT, _Traits>& | |
2475 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
2476 | gamma_distribution<_RealType>& __x) | |
2477 | { | |
2478 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
2479 | typedef typename __istream_type::ios_base __ios_base; | |
2480 | ||
2481 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
2482 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
2483 | ||
ed2807f4 | 2484 | _RealType __alpha_val, __beta_val; |
b01630bb | 2485 | __is >> __alpha_val >> __beta_val >> __x._M_nd; |
8e79468d | 2486 | __x.param(typename gamma_distribution<_RealType>:: |
ed2807f4 | 2487 | param_type(__alpha_val, __beta_val)); |
8e79468d BK |
2488 | |
2489 | __is.flags(__flags); | |
2490 | return __is; | |
2491 | } | |
2492 | ||
2493 | ||
b01630bb PC |
2494 | template<typename _RealType> |
2495 | template<typename _UniformRandomNumberGenerator> | |
2496 | typename weibull_distribution<_RealType>::result_type | |
2497 | weibull_distribution<_RealType>:: | |
2498 | operator()(_UniformRandomNumberGenerator& __urng, | |
2499 | const param_type& __p) | |
2500 | { | |
2501 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
2502 | __aurng(__urng); | |
c2d9083d | 2503 | return __p.b() * std::pow(-std::log(result_type(1) - __aurng()), |
b01630bb PC |
2504 | result_type(1) / __p.a()); |
2505 | } | |
2506 | ||
7b93bdde UD |
2507 | template<typename _RealType> |
2508 | template<typename _ForwardIterator, | |
2509 | typename _UniformRandomNumberGenerator> | |
2510 | void | |
2511 | weibull_distribution<_RealType>:: | |
2512 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2513 | _UniformRandomNumberGenerator& __urng, | |
2514 | const param_type& __p) | |
2515 | { | |
2516 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2517 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
2518 | __aurng(__urng); | |
c2d9083d | 2519 | auto __inv_a = result_type(1) / __p.a(); |
7b93bdde UD |
2520 | |
2521 | while (__f != __t) | |
c2d9083d HY |
2522 | *__f++ = __p.b() * std::pow(-std::log(result_type(1) - __aurng()), |
2523 | __inv_a); | |
7b93bdde UD |
2524 | } |
2525 | ||
8e79468d BK |
2526 | template<typename _RealType, typename _CharT, typename _Traits> |
2527 | std::basic_ostream<_CharT, _Traits>& | |
2528 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
2529 | const weibull_distribution<_RealType>& __x) | |
2530 | { | |
2531 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
2532 | typedef typename __ostream_type::ios_base __ios_base; | |
2533 | ||
2534 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
2535 | const _CharT __fill = __os.fill(); | |
2536 | const std::streamsize __precision = __os.precision(); | |
2537 | const _CharT __space = __os.widen(' '); | |
2538 | __os.flags(__ios_base::scientific | __ios_base::left); | |
2539 | __os.fill(__space); | |
7703dc47 | 2540 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d BK |
2541 | |
2542 | __os << __x.a() << __space << __x.b(); | |
2543 | ||
2544 | __os.flags(__flags); | |
2545 | __os.fill(__fill); | |
2546 | __os.precision(__precision); | |
2547 | return __os; | |
2548 | } | |
2549 | ||
2550 | template<typename _RealType, typename _CharT, typename _Traits> | |
2551 | std::basic_istream<_CharT, _Traits>& | |
2552 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
2553 | weibull_distribution<_RealType>& __x) | |
2554 | { | |
2555 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
2556 | typedef typename __istream_type::ios_base __ios_base; | |
2557 | ||
2558 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
2559 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
2560 | ||
2561 | _RealType __a, __b; | |
2562 | __is >> __a >> __b; | |
2563 | __x.param(typename weibull_distribution<_RealType>:: | |
2564 | param_type(__a, __b)); | |
2565 | ||
2566 | __is.flags(__flags); | |
2567 | return __is; | |
2568 | } | |
2569 | ||
2570 | ||
2571 | template<typename _RealType> | |
2572 | template<typename _UniformRandomNumberGenerator> | |
2573 | typename extreme_value_distribution<_RealType>::result_type | |
2574 | extreme_value_distribution<_RealType>:: | |
2575 | operator()(_UniformRandomNumberGenerator& __urng, | |
2576 | const param_type& __p) | |
2577 | { | |
2578 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
2579 | __aurng(__urng); | |
c2d9083d HY |
2580 | return __p.a() - __p.b() * std::log(-std::log(result_type(1) |
2581 | - __aurng())); | |
8e79468d BK |
2582 | } |
2583 | ||
7b93bdde UD |
2584 | template<typename _RealType> |
2585 | template<typename _ForwardIterator, | |
2586 | typename _UniformRandomNumberGenerator> | |
2587 | void | |
2588 | extreme_value_distribution<_RealType>:: | |
2589 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2590 | _UniformRandomNumberGenerator& __urng, | |
2591 | const param_type& __p) | |
2592 | { | |
2593 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2594 | __detail::_Adaptor<_UniformRandomNumberGenerator, result_type> | |
2595 | __aurng(__urng); | |
2596 | ||
2597 | while (__f != __t) | |
c2d9083d HY |
2598 | *__f++ = __p.a() - __p.b() * std::log(-std::log(result_type(1) |
2599 | - __aurng())); | |
7b93bdde UD |
2600 | } |
2601 | ||
8e79468d BK |
2602 | template<typename _RealType, typename _CharT, typename _Traits> |
2603 | std::basic_ostream<_CharT, _Traits>& | |
2604 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
2605 | const extreme_value_distribution<_RealType>& __x) | |
2606 | { | |
2607 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
2608 | typedef typename __ostream_type::ios_base __ios_base; | |
2609 | ||
2610 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
2611 | const _CharT __fill = __os.fill(); | |
2612 | const std::streamsize __precision = __os.precision(); | |
2613 | const _CharT __space = __os.widen(' '); | |
2614 | __os.flags(__ios_base::scientific | __ios_base::left); | |
2615 | __os.fill(__space); | |
7703dc47 | 2616 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d BK |
2617 | |
2618 | __os << __x.a() << __space << __x.b(); | |
2619 | ||
2620 | __os.flags(__flags); | |
2621 | __os.fill(__fill); | |
2622 | __os.precision(__precision); | |
2623 | return __os; | |
2624 | } | |
2625 | ||
2626 | template<typename _RealType, typename _CharT, typename _Traits> | |
2627 | std::basic_istream<_CharT, _Traits>& | |
2628 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
2629 | extreme_value_distribution<_RealType>& __x) | |
2630 | { | |
2631 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
2632 | typedef typename __istream_type::ios_base __ios_base; | |
2633 | ||
2634 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
2635 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
2636 | ||
2637 | _RealType __a, __b; | |
2638 | __is >> __a >> __b; | |
2639 | __x.param(typename extreme_value_distribution<_RealType>:: | |
2640 | param_type(__a, __b)); | |
2641 | ||
2642 | __is.flags(__flags); | |
2643 | return __is; | |
2644 | } | |
2645 | ||
2646 | ||
2647 | template<typename _IntType> | |
2648 | void | |
2649 | discrete_distribution<_IntType>::param_type:: | |
2650 | _M_initialize() | |
2651 | { | |
2652 | if (_M_prob.size() < 2) | |
2653 | { | |
2654 | _M_prob.clear(); | |
8e79468d BK |
2655 | return; |
2656 | } | |
2657 | ||
f8dd9e0d PC |
2658 | const double __sum = std::accumulate(_M_prob.begin(), |
2659 | _M_prob.end(), 0.0); | |
2660 | // Now normalize the probabilites. | |
60f3a59f PC |
2661 | __detail::__normalize(_M_prob.begin(), _M_prob.end(), _M_prob.begin(), |
2662 | __sum); | |
f8dd9e0d PC |
2663 | // Accumulate partial sums. |
2664 | _M_cp.reserve(_M_prob.size()); | |
8e79468d BK |
2665 | std::partial_sum(_M_prob.begin(), _M_prob.end(), |
2666 | std::back_inserter(_M_cp)); | |
f8dd9e0d | 2667 | // Make sure the last cumulative probability is one. |
8e79468d BK |
2668 | _M_cp[_M_cp.size() - 1] = 1.0; |
2669 | } | |
2670 | ||
2671 | template<typename _IntType> | |
2672 | template<typename _Func> | |
2673 | discrete_distribution<_IntType>::param_type:: | |
f8dd9e0d | 2674 | param_type(size_t __nw, double __xmin, double __xmax, _Func __fw) |
8e79468d BK |
2675 | : _M_prob(), _M_cp() |
2676 | { | |
f8dd9e0d PC |
2677 | const size_t __n = __nw == 0 ? 1 : __nw; |
2678 | const double __delta = (__xmax - __xmin) / __n; | |
2679 | ||
2680 | _M_prob.reserve(__n); | |
2681 | for (size_t __k = 0; __k < __nw; ++__k) | |
2682 | _M_prob.push_back(__fw(__xmin + __k * __delta + 0.5 * __delta)); | |
8e79468d BK |
2683 | |
2684 | _M_initialize(); | |
2685 | } | |
2686 | ||
2687 | template<typename _IntType> | |
2688 | template<typename _UniformRandomNumberGenerator> | |
2689 | typename discrete_distribution<_IntType>::result_type | |
2690 | discrete_distribution<_IntType>:: | |
2691 | operator()(_UniformRandomNumberGenerator& __urng, | |
2692 | const param_type& __param) | |
2693 | { | |
879b9073 PC |
2694 | if (__param._M_cp.empty()) |
2695 | return result_type(0); | |
2696 | ||
9b88236b | 2697 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> |
8e79468d BK |
2698 | __aurng(__urng); |
2699 | ||
2700 | const double __p = __aurng(); | |
2701 | auto __pos = std::lower_bound(__param._M_cp.begin(), | |
2702 | __param._M_cp.end(), __p); | |
8e79468d | 2703 | |
f8dd9e0d | 2704 | return __pos - __param._M_cp.begin(); |
8e79468d BK |
2705 | } |
2706 | ||
7b93bdde UD |
2707 | template<typename _IntType> |
2708 | template<typename _ForwardIterator, | |
2709 | typename _UniformRandomNumberGenerator> | |
2710 | void | |
2711 | discrete_distribution<_IntType>:: | |
2712 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2713 | _UniformRandomNumberGenerator& __urng, | |
2714 | const param_type& __param) | |
2715 | { | |
2716 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2717 | ||
2718 | if (__param._M_cp.empty()) | |
2719 | { | |
2720 | while (__f != __t) | |
2721 | *__f++ = result_type(0); | |
2722 | return; | |
2723 | } | |
2724 | ||
2725 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> | |
2726 | __aurng(__urng); | |
2727 | ||
2728 | while (__f != __t) | |
2729 | { | |
2730 | const double __p = __aurng(); | |
2731 | auto __pos = std::lower_bound(__param._M_cp.begin(), | |
2732 | __param._M_cp.end(), __p); | |
2733 | ||
2734 | *__f++ = __pos - __param._M_cp.begin(); | |
2735 | } | |
2736 | } | |
2737 | ||
8e79468d BK |
2738 | template<typename _IntType, typename _CharT, typename _Traits> |
2739 | std::basic_ostream<_CharT, _Traits>& | |
2740 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
2741 | const discrete_distribution<_IntType>& __x) | |
2742 | { | |
2743 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
2744 | typedef typename __ostream_type::ios_base __ios_base; | |
2745 | ||
2746 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
2747 | const _CharT __fill = __os.fill(); | |
2748 | const std::streamsize __precision = __os.precision(); | |
2749 | const _CharT __space = __os.widen(' '); | |
2750 | __os.flags(__ios_base::scientific | __ios_base::left); | |
2751 | __os.fill(__space); | |
7703dc47 | 2752 | __os.precision(std::numeric_limits<double>::max_digits10); |
8e79468d BK |
2753 | |
2754 | std::vector<double> __prob = __x.probabilities(); | |
2755 | __os << __prob.size(); | |
2756 | for (auto __dit = __prob.begin(); __dit != __prob.end(); ++__dit) | |
2757 | __os << __space << *__dit; | |
2758 | ||
2759 | __os.flags(__flags); | |
2760 | __os.fill(__fill); | |
2761 | __os.precision(__precision); | |
2762 | return __os; | |
2763 | } | |
2764 | ||
2765 | template<typename _IntType, typename _CharT, typename _Traits> | |
2766 | std::basic_istream<_CharT, _Traits>& | |
2767 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
2768 | discrete_distribution<_IntType>& __x) | |
2769 | { | |
2770 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
2771 | typedef typename __istream_type::ios_base __ios_base; | |
2772 | ||
2773 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
2774 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
2775 | ||
2776 | size_t __n; | |
2777 | __is >> __n; | |
2778 | ||
2779 | std::vector<double> __prob_vec; | |
f8dd9e0d | 2780 | __prob_vec.reserve(__n); |
8e79468d BK |
2781 | for (; __n != 0; --__n) |
2782 | { | |
2783 | double __prob; | |
2784 | __is >> __prob; | |
2785 | __prob_vec.push_back(__prob); | |
2786 | } | |
2787 | ||
2788 | __x.param(typename discrete_distribution<_IntType>:: | |
2789 | param_type(__prob_vec.begin(), __prob_vec.end())); | |
2790 | ||
2791 | __is.flags(__flags); | |
2792 | return __is; | |
2793 | } | |
2794 | ||
2795 | ||
2796 | template<typename _RealType> | |
2797 | void | |
2798 | piecewise_constant_distribution<_RealType>::param_type:: | |
2799 | _M_initialize() | |
2800 | { | |
879b9073 PC |
2801 | if (_M_int.size() < 2 |
2802 | || (_M_int.size() == 2 | |
2803 | && _M_int[0] == _RealType(0) | |
2804 | && _M_int[1] == _RealType(1))) | |
8e79468d BK |
2805 | { |
2806 | _M_int.clear(); | |
8e79468d | 2807 | _M_den.clear(); |
8e79468d BK |
2808 | return; |
2809 | } | |
2810 | ||
f8dd9e0d PC |
2811 | const double __sum = std::accumulate(_M_den.begin(), |
2812 | _M_den.end(), 0.0); | |
8e79468d | 2813 | |
60f3a59f PC |
2814 | __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(), |
2815 | __sum); | |
f8dd9e0d PC |
2816 | |
2817 | _M_cp.reserve(_M_den.size()); | |
2818 | std::partial_sum(_M_den.begin(), _M_den.end(), | |
2819 | std::back_inserter(_M_cp)); | |
2820 | ||
2821 | // Make sure the last cumulative probability is one. | |
8e79468d | 2822 | _M_cp[_M_cp.size() - 1] = 1.0; |
f8dd9e0d PC |
2823 | |
2824 | for (size_t __k = 0; __k < _M_den.size(); ++__k) | |
2825 | _M_den[__k] /= _M_int[__k + 1] - _M_int[__k]; | |
8e79468d BK |
2826 | } |
2827 | ||
8e79468d BK |
2828 | template<typename _RealType> |
2829 | template<typename _InputIteratorB, typename _InputIteratorW> | |
2830 | piecewise_constant_distribution<_RealType>::param_type:: | |
2831 | param_type(_InputIteratorB __bbegin, | |
2832 | _InputIteratorB __bend, | |
2833 | _InputIteratorW __wbegin) | |
2834 | : _M_int(), _M_den(), _M_cp() | |
2835 | { | |
f8dd9e0d | 2836 | if (__bbegin != __bend) |
8e79468d | 2837 | { |
f8dd9e0d | 2838 | for (;;) |
8e79468d | 2839 | { |
f8dd9e0d PC |
2840 | _M_int.push_back(*__bbegin); |
2841 | ++__bbegin; | |
2842 | if (__bbegin == __bend) | |
2843 | break; | |
2844 | ||
8e79468d BK |
2845 | _M_den.push_back(*__wbegin); |
2846 | ++__wbegin; | |
2847 | } | |
2848 | } | |
8e79468d BK |
2849 | |
2850 | _M_initialize(); | |
2851 | } | |
2852 | ||
2853 | template<typename _RealType> | |
2854 | template<typename _Func> | |
2855 | piecewise_constant_distribution<_RealType>::param_type:: | |
f8dd9e0d | 2856 | param_type(initializer_list<_RealType> __bl, _Func __fw) |
8e79468d BK |
2857 | : _M_int(), _M_den(), _M_cp() |
2858 | { | |
f8dd9e0d PC |
2859 | _M_int.reserve(__bl.size()); |
2860 | for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter) | |
8e79468d BK |
2861 | _M_int.push_back(*__biter); |
2862 | ||
f8dd9e0d PC |
2863 | _M_den.reserve(_M_int.size() - 1); |
2864 | for (size_t __k = 0; __k < _M_int.size() - 1; ++__k) | |
2865 | _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k]))); | |
8e79468d BK |
2866 | |
2867 | _M_initialize(); | |
2868 | } | |
2869 | ||
2870 | template<typename _RealType> | |
2871 | template<typename _Func> | |
2872 | piecewise_constant_distribution<_RealType>::param_type:: | |
b01630bb | 2873 | param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw) |
8e79468d BK |
2874 | : _M_int(), _M_den(), _M_cp() |
2875 | { | |
f8dd9e0d PC |
2876 | const size_t __n = __nw == 0 ? 1 : __nw; |
2877 | const _RealType __delta = (__xmax - __xmin) / __n; | |
2878 | ||
2879 | _M_int.reserve(__n + 1); | |
2880 | for (size_t __k = 0; __k <= __nw; ++__k) | |
2881 | _M_int.push_back(__xmin + __k * __delta); | |
2882 | ||
2883 | _M_den.reserve(__n); | |
2884 | for (size_t __k = 0; __k < __nw; ++__k) | |
2885 | _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta)); | |
8e79468d BK |
2886 | |
2887 | _M_initialize(); | |
2888 | } | |
2889 | ||
2890 | template<typename _RealType> | |
2891 | template<typename _UniformRandomNumberGenerator> | |
2892 | typename piecewise_constant_distribution<_RealType>::result_type | |
2893 | piecewise_constant_distribution<_RealType>:: | |
2894 | operator()(_UniformRandomNumberGenerator& __urng, | |
2895 | const param_type& __param) | |
2896 | { | |
9b88236b | 2897 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> |
8e79468d BK |
2898 | __aurng(__urng); |
2899 | ||
2900 | const double __p = __aurng(); | |
879b9073 PC |
2901 | if (__param._M_cp.empty()) |
2902 | return __p; | |
2903 | ||
8e79468d BK |
2904 | auto __pos = std::lower_bound(__param._M_cp.begin(), |
2905 | __param._M_cp.end(), __p); | |
2906 | const size_t __i = __pos - __param._M_cp.begin(); | |
2907 | ||
f8dd9e0d PC |
2908 | const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0; |
2909 | ||
2910 | return __param._M_int[__i] + (__p - __pref) / __param._M_den[__i]; | |
8e79468d BK |
2911 | } |
2912 | ||
7b93bdde UD |
2913 | template<typename _RealType> |
2914 | template<typename _ForwardIterator, | |
2915 | typename _UniformRandomNumberGenerator> | |
2916 | void | |
2917 | piecewise_constant_distribution<_RealType>:: | |
2918 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
2919 | _UniformRandomNumberGenerator& __urng, | |
2920 | const param_type& __param) | |
2921 | { | |
2922 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
2923 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> | |
2924 | __aurng(__urng); | |
2925 | ||
2926 | if (__param._M_cp.empty()) | |
2927 | { | |
2928 | while (__f != __t) | |
2929 | *__f++ = __aurng(); | |
2930 | return; | |
2931 | } | |
2932 | ||
2933 | while (__f != __t) | |
2934 | { | |
2935 | const double __p = __aurng(); | |
2936 | ||
2937 | auto __pos = std::lower_bound(__param._M_cp.begin(), | |
2938 | __param._M_cp.end(), __p); | |
2939 | const size_t __i = __pos - __param._M_cp.begin(); | |
2940 | ||
2941 | const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0; | |
2942 | ||
2943 | *__f++ = (__param._M_int[__i] | |
2944 | + (__p - __pref) / __param._M_den[__i]); | |
2945 | } | |
2946 | } | |
2947 | ||
8e79468d BK |
2948 | template<typename _RealType, typename _CharT, typename _Traits> |
2949 | std::basic_ostream<_CharT, _Traits>& | |
2950 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
2951 | const piecewise_constant_distribution<_RealType>& __x) | |
2952 | { | |
2953 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
2954 | typedef typename __ostream_type::ios_base __ios_base; | |
2955 | ||
2956 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
2957 | const _CharT __fill = __os.fill(); | |
2958 | const std::streamsize __precision = __os.precision(); | |
2959 | const _CharT __space = __os.widen(' '); | |
2960 | __os.flags(__ios_base::scientific | __ios_base::left); | |
2961 | __os.fill(__space); | |
7703dc47 | 2962 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d BK |
2963 | |
2964 | std::vector<_RealType> __int = __x.intervals(); | |
2965 | __os << __int.size() - 1; | |
2966 | ||
2967 | for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit) | |
2968 | __os << __space << *__xit; | |
2969 | ||
2970 | std::vector<double> __den = __x.densities(); | |
2971 | for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit) | |
2972 | __os << __space << *__dit; | |
2973 | ||
2974 | __os.flags(__flags); | |
2975 | __os.fill(__fill); | |
2976 | __os.precision(__precision); | |
2977 | return __os; | |
2978 | } | |
2979 | ||
2980 | template<typename _RealType, typename _CharT, typename _Traits> | |
2981 | std::basic_istream<_CharT, _Traits>& | |
2982 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
2983 | piecewise_constant_distribution<_RealType>& __x) | |
2984 | { | |
2985 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
2986 | typedef typename __istream_type::ios_base __ios_base; | |
2987 | ||
2988 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
2989 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
2990 | ||
2991 | size_t __n; | |
2992 | __is >> __n; | |
2993 | ||
2994 | std::vector<_RealType> __int_vec; | |
f8dd9e0d | 2995 | __int_vec.reserve(__n + 1); |
8e79468d BK |
2996 | for (size_t __i = 0; __i <= __n; ++__i) |
2997 | { | |
2998 | _RealType __int; | |
2999 | __is >> __int; | |
3000 | __int_vec.push_back(__int); | |
3001 | } | |
3002 | ||
3003 | std::vector<double> __den_vec; | |
f8dd9e0d | 3004 | __den_vec.reserve(__n); |
8e79468d BK |
3005 | for (size_t __i = 0; __i < __n; ++__i) |
3006 | { | |
3007 | double __den; | |
3008 | __is >> __den; | |
3009 | __den_vec.push_back(__den); | |
3010 | } | |
3011 | ||
3012 | __x.param(typename piecewise_constant_distribution<_RealType>:: | |
3013 | param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin())); | |
3014 | ||
3015 | __is.flags(__flags); | |
3016 | return __is; | |
3017 | } | |
3018 | ||
3019 | ||
3020 | template<typename _RealType> | |
3021 | void | |
3022 | piecewise_linear_distribution<_RealType>::param_type:: | |
3023 | _M_initialize() | |
3024 | { | |
879b9073 PC |
3025 | if (_M_int.size() < 2 |
3026 | || (_M_int.size() == 2 | |
3027 | && _M_int[0] == _RealType(0) | |
3028 | && _M_int[1] == _RealType(1) | |
3029 | && _M_den[0] == _M_den[1])) | |
8e79468d BK |
3030 | { |
3031 | _M_int.clear(); | |
8e79468d | 3032 | _M_den.clear(); |
8e79468d BK |
3033 | return; |
3034 | } | |
3035 | ||
3036 | double __sum = 0.0; | |
f8dd9e0d PC |
3037 | _M_cp.reserve(_M_int.size() - 1); |
3038 | _M_m.reserve(_M_int.size() - 1); | |
3039 | for (size_t __k = 0; __k < _M_int.size() - 1; ++__k) | |
8e79468d | 3040 | { |
f8dd9e0d PC |
3041 | const _RealType __delta = _M_int[__k + 1] - _M_int[__k]; |
3042 | __sum += 0.5 * (_M_den[__k + 1] + _M_den[__k]) * __delta; | |
8e79468d | 3043 | _M_cp.push_back(__sum); |
f8dd9e0d | 3044 | _M_m.push_back((_M_den[__k + 1] - _M_den[__k]) / __delta); |
8e79468d BK |
3045 | } |
3046 | ||
3047 | // Now normalize the densities... | |
60f3a59f PC |
3048 | __detail::__normalize(_M_den.begin(), _M_den.end(), _M_den.begin(), |
3049 | __sum); | |
8e79468d | 3050 | // ... and partial sums... |
60f3a59f | 3051 | __detail::__normalize(_M_cp.begin(), _M_cp.end(), _M_cp.begin(), __sum); |
8e79468d | 3052 | // ... and slopes. |
60f3a59f PC |
3053 | __detail::__normalize(_M_m.begin(), _M_m.end(), _M_m.begin(), __sum); |
3054 | ||
8e79468d BK |
3055 | // Make sure the last cumulative probablility is one. |
3056 | _M_cp[_M_cp.size() - 1] = 1.0; | |
f8dd9e0d | 3057 | } |
8e79468d BK |
3058 | |
3059 | template<typename _RealType> | |
3060 | template<typename _InputIteratorB, typename _InputIteratorW> | |
3061 | piecewise_linear_distribution<_RealType>::param_type:: | |
3062 | param_type(_InputIteratorB __bbegin, | |
3063 | _InputIteratorB __bend, | |
3064 | _InputIteratorW __wbegin) | |
3065 | : _M_int(), _M_den(), _M_cp(), _M_m() | |
3066 | { | |
3067 | for (; __bbegin != __bend; ++__bbegin, ++__wbegin) | |
3068 | { | |
3069 | _M_int.push_back(*__bbegin); | |
3070 | _M_den.push_back(*__wbegin); | |
3071 | } | |
3072 | ||
3073 | _M_initialize(); | |
3074 | } | |
3075 | ||
3076 | template<typename _RealType> | |
3077 | template<typename _Func> | |
3078 | piecewise_linear_distribution<_RealType>::param_type:: | |
f8dd9e0d | 3079 | param_type(initializer_list<_RealType> __bl, _Func __fw) |
8e79468d BK |
3080 | : _M_int(), _M_den(), _M_cp(), _M_m() |
3081 | { | |
f8dd9e0d PC |
3082 | _M_int.reserve(__bl.size()); |
3083 | _M_den.reserve(__bl.size()); | |
3084 | for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter) | |
8e79468d BK |
3085 | { |
3086 | _M_int.push_back(*__biter); | |
3087 | _M_den.push_back(__fw(*__biter)); | |
3088 | } | |
3089 | ||
3090 | _M_initialize(); | |
3091 | } | |
3092 | ||
3093 | template<typename _RealType> | |
3094 | template<typename _Func> | |
3095 | piecewise_linear_distribution<_RealType>::param_type:: | |
f8dd9e0d | 3096 | param_type(size_t __nw, _RealType __xmin, _RealType __xmax, _Func __fw) |
8e79468d BK |
3097 | : _M_int(), _M_den(), _M_cp(), _M_m() |
3098 | { | |
f8dd9e0d PC |
3099 | const size_t __n = __nw == 0 ? 1 : __nw; |
3100 | const _RealType __delta = (__xmax - __xmin) / __n; | |
3101 | ||
3102 | _M_int.reserve(__n + 1); | |
3103 | _M_den.reserve(__n + 1); | |
3104 | for (size_t __k = 0; __k <= __nw; ++__k) | |
8e79468d | 3105 | { |
f8dd9e0d PC |
3106 | _M_int.push_back(__xmin + __k * __delta); |
3107 | _M_den.push_back(__fw(_M_int[__k] + __delta)); | |
8e79468d BK |
3108 | } |
3109 | ||
3110 | _M_initialize(); | |
3111 | } | |
3112 | ||
3113 | template<typename _RealType> | |
3114 | template<typename _UniformRandomNumberGenerator> | |
3115 | typename piecewise_linear_distribution<_RealType>::result_type | |
3116 | piecewise_linear_distribution<_RealType>:: | |
3117 | operator()(_UniformRandomNumberGenerator& __urng, | |
3118 | const param_type& __param) | |
3119 | { | |
9b88236b | 3120 | __detail::_Adaptor<_UniformRandomNumberGenerator, double> |
8e79468d BK |
3121 | __aurng(__urng); |
3122 | ||
3123 | const double __p = __aurng(); | |
879b9073 | 3124 | if (__param._M_cp.empty()) |
d3a73504 PC |
3125 | return __p; |
3126 | ||
8e79468d BK |
3127 | auto __pos = std::lower_bound(__param._M_cp.begin(), |
3128 | __param._M_cp.end(), __p); | |
3129 | const size_t __i = __pos - __param._M_cp.begin(); | |
f8dd9e0d PC |
3130 | |
3131 | const double __pref = __i > 0 ? __param._M_cp[__i - 1] : 0.0; | |
3132 | ||
8e79468d BK |
3133 | const double __a = 0.5 * __param._M_m[__i]; |
3134 | const double __b = __param._M_den[__i]; | |
f8dd9e0d PC |
3135 | const double __cm = __p - __pref; |
3136 | ||
3137 | _RealType __x = __param._M_int[__i]; | |
3138 | if (__a == 0) | |
3139 | __x += __cm / __b; | |
3140 | else | |
3141 | { | |
3142 | const double __d = __b * __b + 4.0 * __a * __cm; | |
3143 | __x += 0.5 * (std::sqrt(__d) - __b) / __a; | |
3144 | } | |
8e79468d | 3145 | |
f8dd9e0d | 3146 | return __x; |
8e79468d BK |
3147 | } |
3148 | ||
7b93bdde UD |
3149 | template<typename _RealType> |
3150 | template<typename _ForwardIterator, | |
3151 | typename _UniformRandomNumberGenerator> | |
3152 | void | |
3153 | piecewise_linear_distribution<_RealType>:: | |
3154 | __generate_impl(_ForwardIterator __f, _ForwardIterator __t, | |
3155 | _UniformRandomNumberGenerator& __urng, | |
3156 | const param_type& __param) | |
3157 | { | |
3158 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) | |
3159 | // We could duplicate everything from operator()... | |
3160 | while (__f != __t) | |
3161 | *__f++ = this->operator()(__urng, __param); | |
3162 | } | |
3163 | ||
8e79468d BK |
3164 | template<typename _RealType, typename _CharT, typename _Traits> |
3165 | std::basic_ostream<_CharT, _Traits>& | |
3166 | operator<<(std::basic_ostream<_CharT, _Traits>& __os, | |
3167 | const piecewise_linear_distribution<_RealType>& __x) | |
3168 | { | |
3169 | typedef std::basic_ostream<_CharT, _Traits> __ostream_type; | |
3170 | typedef typename __ostream_type::ios_base __ios_base; | |
3171 | ||
3172 | const typename __ios_base::fmtflags __flags = __os.flags(); | |
3173 | const _CharT __fill = __os.fill(); | |
3174 | const std::streamsize __precision = __os.precision(); | |
3175 | const _CharT __space = __os.widen(' '); | |
3176 | __os.flags(__ios_base::scientific | __ios_base::left); | |
3177 | __os.fill(__space); | |
7703dc47 | 3178 | __os.precision(std::numeric_limits<_RealType>::max_digits10); |
8e79468d BK |
3179 | |
3180 | std::vector<_RealType> __int = __x.intervals(); | |
3181 | __os << __int.size() - 1; | |
3182 | ||
3183 | for (auto __xit = __int.begin(); __xit != __int.end(); ++__xit) | |
3184 | __os << __space << *__xit; | |
3185 | ||
3186 | std::vector<double> __den = __x.densities(); | |
3187 | for (auto __dit = __den.begin(); __dit != __den.end(); ++__dit) | |
3188 | __os << __space << *__dit; | |
3189 | ||
3190 | __os.flags(__flags); | |
3191 | __os.fill(__fill); | |
3192 | __os.precision(__precision); | |
3193 | return __os; | |
3194 | } | |
3195 | ||
3196 | template<typename _RealType, typename _CharT, typename _Traits> | |
3197 | std::basic_istream<_CharT, _Traits>& | |
3198 | operator>>(std::basic_istream<_CharT, _Traits>& __is, | |
3199 | piecewise_linear_distribution<_RealType>& __x) | |
3200 | { | |
3201 | typedef std::basic_istream<_CharT, _Traits> __istream_type; | |
3202 | typedef typename __istream_type::ios_base __ios_base; | |
3203 | ||
3204 | const typename __ios_base::fmtflags __flags = __is.flags(); | |
3205 | __is.flags(__ios_base::dec | __ios_base::skipws); | |
3206 | ||
3207 | size_t __n; | |
3208 | __is >> __n; | |
3209 | ||
3210 | std::vector<_RealType> __int_vec; | |
f8dd9e0d | 3211 | __int_vec.reserve(__n + 1); |
8e79468d BK |
3212 | for (size_t __i = 0; __i <= __n; ++__i) |
3213 | { | |
3214 | _RealType __int; | |
3215 | __is >> __int; | |
3216 | __int_vec.push_back(__int); | |
3217 | } | |
3218 | ||
3219 | std::vector<double> __den_vec; | |
f8dd9e0d | 3220 | __den_vec.reserve(__n + 1); |
8e79468d BK |
3221 | for (size_t __i = 0; __i <= __n; ++__i) |
3222 | { | |
3223 | double __den; | |
3224 | __is >> __den; | |
3225 | __den_vec.push_back(__den); | |
3226 | } | |
3227 | ||
3228 | __x.param(typename piecewise_linear_distribution<_RealType>:: | |
3229 | param_type(__int_vec.begin(), __int_vec.end(), __den_vec.begin())); | |
3230 | ||
3231 | __is.flags(__flags); | |
3232 | return __is; | |
3233 | } | |
3234 | ||
3235 | ||
3236 | template<typename _IntType> | |
3237 | seed_seq::seed_seq(std::initializer_list<_IntType> __il) | |
3238 | { | |
3239 | for (auto __iter = __il.begin(); __iter != __il.end(); ++__iter) | |
b94f4bef | 3240 | _M_v.push_back(__detail::__mod<result_type, |
8e79468d BK |
3241 | __detail::_Shift<result_type, 32>::__value>(*__iter)); |
3242 | } | |
3243 | ||
3244 | template<typename _InputIterator> | |
3245 | seed_seq::seed_seq(_InputIterator __begin, _InputIterator __end) | |
3246 | { | |
3247 | for (_InputIterator __iter = __begin; __iter != __end; ++__iter) | |
b94f4bef | 3248 | _M_v.push_back(__detail::__mod<result_type, |
8e79468d BK |
3249 | __detail::_Shift<result_type, 32>::__value>(*__iter)); |
3250 | } | |
3251 | ||
3252 | template<typename _RandomAccessIterator> | |
3253 | void | |
3254 | seed_seq::generate(_RandomAccessIterator __begin, | |
3255 | _RandomAccessIterator __end) | |
3256 | { | |
3257 | typedef typename iterator_traits<_RandomAccessIterator>::value_type | |
6855fe45 | 3258 | _Type; |
8e79468d BK |
3259 | |
3260 | if (__begin == __end) | |
3261 | return; | |
3262 | ||
b94f4bef | 3263 | std::fill(__begin, __end, _Type(0x8b8b8b8bu)); |
8e79468d BK |
3264 | |
3265 | const size_t __n = __end - __begin; | |
3266 | const size_t __s = _M_v.size(); | |
3267 | const size_t __t = (__n >= 623) ? 11 | |
3268 | : (__n >= 68) ? 7 | |
3269 | : (__n >= 39) ? 5 | |
3270 | : (__n >= 7) ? 3 | |
3271 | : (__n - 1) / 2; | |
3272 | const size_t __p = (__n - __t) / 2; | |
3273 | const size_t __q = __p + __t; | |
58651854 | 3274 | const size_t __m = std::max(size_t(__s + 1), __n); |
8e79468d BK |
3275 | |
3276 | for (size_t __k = 0; __k < __m; ++__k) | |
3277 | { | |
6855fe45 PC |
3278 | _Type __arg = (__begin[__k % __n] |
3279 | ^ __begin[(__k + __p) % __n] | |
3280 | ^ __begin[(__k - 1) % __n]); | |
9d1f3ce6 | 3281 | _Type __r1 = __arg ^ (__arg >> 27); |
d9c257a7 PC |
3282 | __r1 = __detail::__mod<_Type, |
3283 | __detail::_Shift<_Type, 32>::__value>(1664525u * __r1); | |
6855fe45 | 3284 | _Type __r2 = __r1; |
8e79468d BK |
3285 | if (__k == 0) |
3286 | __r2 += __s; | |
3287 | else if (__k <= __s) | |
3288 | __r2 += __k % __n + _M_v[__k - 1]; | |
3289 | else | |
3290 | __r2 += __k % __n; | |
b94f4bef PC |
3291 | __r2 = __detail::__mod<_Type, |
3292 | __detail::_Shift<_Type, 32>::__value>(__r2); | |
8e79468d BK |
3293 | __begin[(__k + __p) % __n] += __r1; |
3294 | __begin[(__k + __q) % __n] += __r2; | |
3295 | __begin[__k % __n] = __r2; | |
3296 | } | |
3297 | ||
3298 | for (size_t __k = __m; __k < __m + __n; ++__k) | |
3299 | { | |
6855fe45 PC |
3300 | _Type __arg = (__begin[__k % __n] |
3301 | + __begin[(__k + __p) % __n] | |
3302 | + __begin[(__k - 1) % __n]); | |
9d1f3ce6 | 3303 | _Type __r3 = __arg ^ (__arg >> 27); |
d9c257a7 PC |
3304 | __r3 = __detail::__mod<_Type, |
3305 | __detail::_Shift<_Type, 32>::__value>(1566083941u * __r3); | |
6855fe45 | 3306 | _Type __r4 = __r3 - __k % __n; |
b94f4bef PC |
3307 | __r4 = __detail::__mod<_Type, |
3308 | __detail::_Shift<_Type, 32>::__value>(__r4); | |
82582df6 JS |
3309 | __begin[(__k + __p) % __n] ^= __r3; |
3310 | __begin[(__k + __q) % __n] ^= __r4; | |
8e79468d BK |
3311 | __begin[__k % __n] = __r4; |
3312 | } | |
3313 | } | |
3314 | ||
3315 | template<typename _RealType, size_t __bits, | |
3316 | typename _UniformRandomNumberGenerator> | |
3317 | _RealType | |
3318 | generate_canonical(_UniformRandomNumberGenerator& __urng) | |
3319 | { | |
1c4ff014 | 3320 | static_assert(std::is_floating_point<_RealType>::value, |
0cded43d | 3321 | "template argument must be a floating point type"); |
1c4ff014 | 3322 | |
8e79468d BK |
3323 | const size_t __b |
3324 | = std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits), | |
3325 | __bits); | |
3326 | const long double __r = static_cast<long double>(__urng.max()) | |
3327 | - static_cast<long double>(__urng.min()) + 1.0L; | |
b01630bb | 3328 | const size_t __log2r = std::log(__r) / std::log(2.0L); |
33df19a7 ESR |
3329 | const size_t __m = std::max<size_t>(1UL, |
3330 | (__b + __log2r - 1UL) / __log2r); | |
3331 | _RealType __ret; | |
92d85953 JW |
3332 | _RealType __sum = _RealType(0); |
3333 | _RealType __tmp = _RealType(1); | |
3334 | for (size_t __k = __m; __k != 0; --__k) | |
8e79468d | 3335 | { |
92d85953 JW |
3336 | __sum += _RealType(__urng() - __urng.min()) * __tmp; |
3337 | __tmp *= __r; | |
3338 | } | |
3339 | __ret = __sum / __tmp; | |
3340 | if (__builtin_expect(__ret >= _RealType(1), 0)) | |
3341 | { | |
3342 | #if _GLIBCXX_USE_C99_MATH_TR1 | |
3343 | __ret = std::nextafter(_RealType(1), _RealType(0)); | |
3344 | #else | |
3345 | __ret = _RealType(1) | |
3346 | - std::numeric_limits<_RealType>::epsilon() / _RealType(2); | |
3347 | #endif | |
8e79468d | 3348 | } |
33df19a7 | 3349 | return __ret; |
8e79468d | 3350 | } |
12ffa228 BK |
3351 | |
3352 | _GLIBCXX_END_NAMESPACE_VERSION | |
3353 | } // namespace | |
06f29237 PC |
3354 | |
3355 | #endif |