| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifndef NEWRAN_LIB |
| 8 |
#define NEWRAN_LIB 0 |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "include.h" |
| 13 |
#include "myexcept.h" |
| 14 |
#include "extreal.h" |
| 15 |
#include "simpstr.h" |
| 16 |
|
| 17 |
#ifdef use_namespace |
| 18 |
namespace NEWRAN { using namespace RBD_COMMON; } |
| 19 |
namespace RBD_LIBRARIES { using namespace NEWRAN; } |
| 20 |
namespace NEWRAN { |
| 21 |
using RBD_COMMON::Real; |
| 22 |
using RBD_COMMON::long_Real; |
| 23 |
#endif |
| 24 |
|
| 25 |
|
| 26 |
typedef Real (*PDF)(Real); |
| 27 |
|
| 28 |
|
| 29 |
Real gamma_series(long_Real z); |
| 30 |
inline Real gamma_g_value() { return 10.0; } |
| 31 |
Real ln_gamma(Real z, int& sign); |
| 32 |
Real ln_gamma(Real z); |
| 33 |
Real ln_gamma_lanczos(Real z); |
| 34 |
Real rbd_gamma(Real z); |
| 35 |
|
| 36 |
|
| 37 |
class RepeatedRandom; |
| 38 |
class SelectedRandom; |
| 39 |
|
| 40 |
|
| 41 |
class Random |
| 42 |
{ |
| 43 |
|
| 44 |
private: |
| 45 |
void operator=(const Random&) {} |
| 46 |
|
| 47 |
virtual void CSFD(bool update) {} |
| 48 |
virtual void CSTD() {} |
| 49 |
|
| 50 |
protected: |
| 51 |
static Random* RNG; |
| 52 |
static Random* Dummy; |
| 53 |
static SimpleString Dir; |
| 54 |
|
| 55 |
public: |
| 56 |
static void Set(Random& r); |
| 57 |
static void CopySeedFromDisk(bool update=false); |
| 58 |
static void CopySeedToDisk(); |
| 59 |
static void SetDirectory(const char* dir); |
| 60 |
|
| 61 |
virtual Real Next() { return RNG->Next(); } |
| 62 |
virtual unsigned long ulNext(); |
| 63 |
virtual const char* Name(); |
| 64 |
virtual Real Density(Real) const; |
| 65 |
Random() {} |
| 66 |
virtual ~Random() {} |
| 67 |
SelectedRandom& operator()(double); |
| 68 |
RepeatedRandom& operator()(int); |
| 69 |
virtual ExtReal Mean() const { return 0.5; } |
| 70 |
|
| 71 |
virtual ExtReal Variance() const { return 1.0/12.0; } |
| 72 |
|
| 73 |
virtual void tDelete() {} |
| 74 |
virtual int nelems() const { return 1; } |
| 75 |
|
| 76 |
virtual void load(int*,Real*,Random**); |
| 77 |
friend class RandomPermutation; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
class LGM_base : public Random |
| 83 |
{ |
| 84 |
void operator=(const LGM_base&) {} |
| 85 |
protected: |
| 86 |
long seed; |
| 87 |
void NextValue(); |
| 88 |
bool Update; |
| 89 |
|
| 90 |
public: |
| 91 |
LGM_base(double s); |
| 92 |
Real Next(); |
| 93 |
unsigned long ulNext(); |
| 94 |
const char* Name(); |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
class LGM_simple : public LGM_base |
| 99 |
{ |
| 100 |
void operator=(const LGM_simple&) {} |
| 101 |
void CSFD(bool update); |
| 102 |
void CSTD(); |
| 103 |
|
| 104 |
public: |
| 105 |
LGM_simple(double s=0.46875); |
| 106 |
const char* Name(); |
| 107 |
~LGM_simple(); |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
class LGM_mixed : public LGM_base |
| 112 |
{ |
| 113 |
long Buffer[128]; |
| 114 |
void NextValue(); |
| 115 |
long seed_mixed; |
| 116 |
void operator=(const LGM_mixed&) {} |
| 117 |
void CSFD(bool update); |
| 118 |
void CSTD(); |
| 119 |
|
| 120 |
public: |
| 121 |
LGM_mixed(double s=0.46875); |
| 122 |
Real Next(); |
| 123 |
unsigned long ulNext(); |
| 124 |
const char* Name(); |
| 125 |
~LGM_mixed(); |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
class WH : public Random |
| 130 |
{ |
| 131 |
bool Update; |
| 132 |
void operator=(const WH&) {} |
| 133 |
unsigned long seed1, seed2, seed3; |
| 134 |
void CSFD(bool update); |
| 135 |
void CSTD(); |
| 136 |
|
| 137 |
public: |
| 138 |
WH(double s=0.46875); |
| 139 |
Real Next(); |
| 140 |
unsigned long ulNext(); |
| 141 |
const char* Name(); |
| 142 |
~WH(); |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
class FM : public Random |
| 147 |
{ |
| 148 |
bool Update; |
| 149 |
void operator=(const FM&) {} |
| 150 |
unsigned long seed; |
| 151 |
void NextValue(); |
| 152 |
void CSFD(bool update); |
| 153 |
void CSTD(); |
| 154 |
|
| 155 |
public: |
| 156 |
FM(double s=0.46875); |
| 157 |
Real Next(); |
| 158 |
unsigned long ulNext(); |
| 159 |
const char* Name(); |
| 160 |
~FM(); |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
class MotherOfAll : public Random |
| 165 |
{ |
| 166 |
bool Update; |
| 167 |
short mother1[10]; |
| 168 |
short mother2[10]; |
| 169 |
short mStart; |
| 170 |
unsigned long seed; |
| 171 |
void Mother(); |
| 172 |
void operator=(const MotherOfAll&) {} |
| 173 |
void CSFD(bool update); |
| 174 |
void CSTD(); |
| 175 |
|
| 176 |
public: |
| 177 |
MotherOfAll(double s=0.9375); |
| 178 |
Real Next(); |
| 179 |
unsigned long ulNext(); |
| 180 |
const char* Name(); |
| 181 |
~MotherOfAll(); |
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
class MultWithCarry : public Random |
| 186 |
{ |
| 187 |
bool Update; |
| 188 |
unsigned long x; |
| 189 |
unsigned long crry; |
| 190 |
|
| 191 |
void NextValue(); |
| 192 |
|
| 193 |
void operator=(MultWithCarry&) {} |
| 194 |
void CSFD(bool update); |
| 195 |
void CSTD(); |
| 196 |
|
| 197 |
public: |
| 198 |
MultWithCarry(double s=0.46875); |
| 199 |
Real Next(); |
| 200 |
unsigned long ulNext(); |
| 201 |
const char* Name(); |
| 202 |
~MultWithCarry(); |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
class MT : public Random |
| 207 |
{ |
| 208 |
bool Update; |
| 209 |
void init_genrand(unsigned long s); |
| 210 |
unsigned long genrand_int32(); |
| 211 |
void operator=(const MT&) {} |
| 212 |
void CSFD(bool update); |
| 213 |
void CSTD(); |
| 214 |
|
| 215 |
public: |
| 216 |
MT(double s=0.46875); |
| 217 |
Real Next(); |
| 218 |
unsigned long ulNext() { return genrand_int32(); } |
| 219 |
const char* Name(); |
| 220 |
~MT(); |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
class DummyRNG : public Random |
| 226 |
{ |
| 227 |
void operator=(const DummyRNG&) {} |
| 228 |
void CSFD(bool update); |
| 229 |
void CSTD(); |
| 230 |
|
| 231 |
public: |
| 232 |
Real Next(); |
| 233 |
const char* Name(); |
| 234 |
}; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
class Uniform : public Random |
| 239 |
{ |
| 240 |
void operator=(const Uniform&) {} |
| 241 |
|
| 242 |
public: |
| 243 |
const char* Name(); |
| 244 |
Uniform() {} |
| 245 |
Real Next() { return RNG->Next(); } |
| 246 |
ExtReal Mean() const { return 0.5; } |
| 247 |
ExtReal Variance() const { return 1.0/12.0; } |
| 248 |
Real Density(Real x) const { return (x < 0.0 || x > 1.0) ? 0 : 1.0; } |
| 249 |
}; |
| 250 |
|
| 251 |
|
| 252 |
class Constant : public Random |
| 253 |
{ |
| 254 |
void operator=(const Constant&) {} |
| 255 |
Real value; |
| 256 |
|
| 257 |
public: |
| 258 |
const char* Name(); |
| 259 |
Constant(Real v) { value=v; } |
| 260 |
Real Next() { return value; } |
| 261 |
ExtReal Mean() const { return value; } |
| 262 |
ExtReal Variance() const { return 0.0; } |
| 263 |
}; |
| 264 |
|
| 265 |
|
| 266 |
class PosGen : public Random |
| 267 |
{ |
| 268 |
void operator=(const PosGen&) {} |
| 269 |
|
| 270 |
protected: |
| 271 |
Real xi, *sx, *sfx; |
| 272 |
bool NotReady; |
| 273 |
void Build(bool); |
| 274 |
|
| 275 |
public: |
| 276 |
const char* Name(); |
| 277 |
PosGen(); |
| 278 |
~PosGen(); |
| 279 |
Real Next(); |
| 280 |
ExtReal Mean() const { return (ExtReal)Missing; } |
| 281 |
ExtReal Variance() const { return (ExtReal)Missing; } |
| 282 |
}; |
| 283 |
|
| 284 |
|
| 285 |
class SymGen : public PosGen |
| 286 |
{ |
| 287 |
void operator=(const SymGen&) {} |
| 288 |
|
| 289 |
public: |
| 290 |
const char* Name(); |
| 291 |
Real Next(); |
| 292 |
}; |
| 293 |
|
| 294 |
|
| 295 |
class Normal : public SymGen |
| 296 |
{ |
| 297 |
void operator=(const Normal&) {} |
| 298 |
static Real Nxi, *Nsx, *Nsfx; |
| 299 |
static long count; |
| 300 |
|
| 301 |
public: |
| 302 |
const char* Name(); |
| 303 |
Normal(); |
| 304 |
~Normal(); |
| 305 |
Real Density(Real) const; |
| 306 |
ExtReal Mean() const { return 0.0; } |
| 307 |
ExtReal Variance() const { return 1.0; } |
| 308 |
}; |
| 309 |
|
| 310 |
|
| 311 |
class ChiSq : public Random |
| 312 |
{ |
| 313 |
void operator=(const ChiSq&) {} |
| 314 |
Random* c1; |
| 315 |
Random* c2; |
| 316 |
int version; |
| 317 |
Real mean, var; |
| 318 |
|
| 319 |
public: |
| 320 |
const char* Name(); |
| 321 |
ChiSq(int, Real=0.0); |
| 322 |
~ChiSq(); |
| 323 |
ExtReal Mean() const { return mean; } |
| 324 |
ExtReal Variance() const { return var; } |
| 325 |
Real Next(); |
| 326 |
}; |
| 327 |
|
| 328 |
|
| 329 |
class Cauchy : public SymGen |
| 330 |
{ |
| 331 |
void operator=(const Cauchy&) {} |
| 332 |
|
| 333 |
public: |
| 334 |
const char* Name(); |
| 335 |
Real Density(Real) const; |
| 336 |
ExtReal Mean() const { return Indefinite; } |
| 337 |
ExtReal Variance() const { return PlusInfinity; } |
| 338 |
}; |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
class Exponential : public Uniform |
| 343 |
{ |
| 344 |
Uniform U; |
| 345 |
double mean; |
| 346 |
double variance; |
| 347 |
|
| 348 |
public: |
| 349 |
|
| 350 |
Exponential(double md=1):mean(md){} |
| 351 |
Real Next(); |
| 352 |
const char* Name(); |
| 353 |
Real Density(Real) const; |
| 354 |
ExtReal Mean() const { return mean; } |
| 355 |
ExtReal Variance() const { return mean*mean; } |
| 356 |
}; |
| 357 |
|
| 358 |
class Weibull : public Uniform |
| 359 |
{ |
| 360 |
Uniform U; |
| 361 |
double scale; |
| 362 |
double shape; |
| 363 |
public: |
| 364 |
Weibull(double scale_, double shape_) |
| 365 |
{ |
| 366 |
scale = scale_; |
| 367 |
shape = shape_; |
| 368 |
} |
| 369 |
Real Next(); |
| 370 |
const char* Name(); |
| 371 |
}; |
| 372 |
|
| 373 |
|
| 374 |
class Extreme_Largest : public Uniform |
| 375 |
{ |
| 376 |
Uniform U; |
| 377 |
double mu; |
| 378 |
double sigma; |
| 379 |
public: |
| 380 |
Extreme_Largest(double mu_, double sigma_) |
| 381 |
{ |
| 382 |
mu = mu_; |
| 383 |
sigma = sigma_; |
| 384 |
} |
| 385 |
Real Next(); |
| 386 |
const char* Name(); |
| 387 |
}; |
| 388 |
|
| 389 |
|
| 390 |
class Student : public Uniform |
| 391 |
{ |
| 392 |
private: |
| 393 |
Real mu; |
| 394 |
Real sigma; |
| 395 |
Real nu; |
| 396 |
|
| 397 |
public: |
| 398 |
Student(Real mu_, Real sigma_, Real nu_) |
| 399 |
{ |
| 400 |
mu = mu_; |
| 401 |
sigma = sigma_; |
| 402 |
nu=nu_; |
| 403 |
} |
| 404 |
Real Next(); |
| 405 |
const char* Name(); |
| 406 |
}; |
| 407 |
|
| 408 |
|
| 409 |
class AsymGen : public Random |
| 410 |
{ |
| 411 |
void operator=(const AsymGen&) {} |
| 412 |
Real xi, *sx, *sfx; int ic; |
| 413 |
bool NotReady; |
| 414 |
void Build(); |
| 415 |
|
| 416 |
protected: |
| 417 |
Real mode; |
| 418 |
|
| 419 |
public: |
| 420 |
const char* Name(); |
| 421 |
AsymGen(Real); |
| 422 |
~AsymGen(); |
| 423 |
Real Next(); |
| 424 |
ExtReal Mean() const { return (ExtReal)Missing; } |
| 425 |
ExtReal Variance() const { return (ExtReal)Missing; } |
| 426 |
}; |
| 427 |
|
| 428 |
|
| 429 |
class Gamma : public Random |
| 430 |
{ |
| 431 |
void operator=(const Gamma&) {} |
| 432 |
Random* method; |
| 433 |
|
| 434 |
public: |
| 435 |
const char* Name(); |
| 436 |
Gamma(Real); |
| 437 |
~Gamma() { delete method; } |
| 438 |
Real Next() { return method->Next(); } |
| 439 |
ExtReal Mean() const { return method->Mean(); } |
| 440 |
ExtReal Variance() const { return method->Variance(); } |
| 441 |
}; |
| 442 |
|
| 443 |
|
| 444 |
class PosGenX : public PosGen |
| 445 |
{ |
| 446 |
void operator=(const PosGenX&) {} |
| 447 |
PDF f; |
| 448 |
|
| 449 |
public: |
| 450 |
const char* Name(); |
| 451 |
PosGenX(PDF fx); |
| 452 |
Real Density(Real x) const { return (*f)(x); } |
| 453 |
}; |
| 454 |
|
| 455 |
|
| 456 |
class SymGenX : public SymGen |
| 457 |
{ |
| 458 |
void operator=(const SymGenX&) {} |
| 459 |
PDF f; |
| 460 |
|
| 461 |
public: |
| 462 |
const char* Name(); |
| 463 |
SymGenX(PDF fx); |
| 464 |
Real Density(Real x) const { return (*f)(x); } |
| 465 |
}; |
| 466 |
|
| 467 |
|
| 468 |
class AsymGenX : public AsymGen |
| 469 |
{ |
| 470 |
void operator=(const AsymGenX&) {} |
| 471 |
PDF f; |
| 472 |
|
| 473 |
public: |
| 474 |
const char* Name(); |
| 475 |
AsymGenX(PDF fx, Real mx); |
| 476 |
Real Density(Real x) const { return (*f)(x); } |
| 477 |
}; |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
class Pareto : public Random |
| 483 |
{ |
| 484 |
void operator=(const Pareto&) {} |
| 485 |
Real Shape, RS; |
| 486 |
|
| 487 |
public: |
| 488 |
const char* Name(); |
| 489 |
Pareto(Real shape); |
| 490 |
~Pareto() {} |
| 491 |
Real Next(); |
| 492 |
ExtReal Mean() const; |
| 493 |
ExtReal Variance() const; |
| 494 |
}; |
| 495 |
|
| 496 |
|
| 497 |
class DiscreteGen : public Random |
| 498 |
{ |
| 499 |
void operator=(const DiscreteGen&) {} |
| 500 |
Real *p; int *ialt; int n; Real *val; |
| 501 |
void Gen(int, Real*); |
| 502 |
Real mean, var; |
| 503 |
|
| 504 |
public: |
| 505 |
const char* Name(); |
| 506 |
DiscreteGen(int,Real*); |
| 507 |
DiscreteGen(int,Real*,Real*); |
| 508 |
~DiscreteGen(); |
| 509 |
Real Next(); |
| 510 |
ExtReal Mean() const { return mean; } |
| 511 |
ExtReal Variance() const { return var; } |
| 512 |
}; |
| 513 |
|
| 514 |
|
| 515 |
class Poisson : public Random |
| 516 |
{ |
| 517 |
void operator=(const Poisson&) {} |
| 518 |
Random* method; |
| 519 |
|
| 520 |
public: |
| 521 |
const char* Name(); |
| 522 |
Poisson(Real); |
| 523 |
~Poisson() { delete method; } |
| 524 |
Real Next() { return method->Next(); } |
| 525 |
ExtReal Mean() const { return method->Mean(); } |
| 526 |
ExtReal Variance() const { return method->Variance(); } |
| 527 |
}; |
| 528 |
|
| 529 |
|
| 530 |
class Binomial : public Random |
| 531 |
{ |
| 532 |
void operator=(const Binomial&) {} |
| 533 |
Random* method; |
| 534 |
|
| 535 |
public: |
| 536 |
const char* Name(); |
| 537 |
Binomial(int p, Real n); |
| 538 |
~Binomial() { delete method; } |
| 539 |
Real Next() { return method->Next(); } |
| 540 |
ExtReal Mean() const { return method->Mean(); } |
| 541 |
ExtReal Variance() const { return method->Variance(); } |
| 542 |
}; |
| 543 |
|
| 544 |
|
| 545 |
class NegativeBinomial : public AsymGen |
| 546 |
{ |
| 547 |
Real N, P, Q, p, ln_q, c; |
| 548 |
|
| 549 |
public: |
| 550 |
const char* Name(); |
| 551 |
NegativeBinomial(Real NX, Real PX); |
| 552 |
Real Density(Real) const; |
| 553 |
Real Next(); |
| 554 |
ExtReal Mean() const { return N * P; } |
| 555 |
ExtReal Variance() const { return N * P * Q; } |
| 556 |
}; |
| 557 |
|
| 558 |
|
| 559 |
class Stable : public Random |
| 560 |
{ |
| 561 |
public: |
| 562 |
enum Notation { Chambers, Standard, Kalpha, Default }; |
| 563 |
private: |
| 564 |
Real alpha, bprime; |
| 565 |
Real beta; |
| 566 |
Notation notation; |
| 567 |
Real d2(Real z); |
| 568 |
Real tan2(Real x); |
| 569 |
Real k(Real a); |
| 570 |
const Real piby2; |
| 571 |
const Real piby4; |
| 572 |
|
| 573 |
public: |
| 574 |
const char* Name(); |
| 575 |
Stable(Real a, Real bx, Notation n = Default); |
| 576 |
Real Next(); |
| 577 |
ExtReal Mean() const; |
| 578 |
ExtReal Variance() const; |
| 579 |
Real beta_prime() const { return bprime; } |
| 580 |
}; |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
class NegatedRandom : public Random |
| 588 |
{ |
| 589 |
protected: |
| 590 |
Random* rv; |
| 591 |
NegatedRandom(Random& rvx) : rv(&rvx) {} |
| 592 |
void tDelete() { rv->tDelete(); delete this; } |
| 593 |
|
| 594 |
public: |
| 595 |
Real Next(); |
| 596 |
ExtReal Mean() const; |
| 597 |
ExtReal Variance() const; |
| 598 |
friend NegatedRandom& operator-(Random&); |
| 599 |
}; |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
class ScaledRandom : public Random |
| 604 |
{ |
| 605 |
protected: |
| 606 |
Random* rv; Real s; |
| 607 |
ScaledRandom(Random& rvx, Real sx) : rv(&rvx), s(sx) {} |
| 608 |
void tDelete() { rv->tDelete(); delete this; } |
| 609 |
|
| 610 |
public: |
| 611 |
Real Next(); |
| 612 |
ExtReal Mean() const; |
| 613 |
ExtReal Variance() const; |
| 614 |
friend ScaledRandom& operator*(Real, Random&); |
| 615 |
friend ScaledRandom& operator*(Random&, Real); |
| 616 |
friend ScaledRandom& operator/(Random&, Real); |
| 617 |
}; |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
class ReciprocalRandom : public Random |
| 622 |
{ |
| 623 |
protected: |
| 624 |
Random* rv; Real s; |
| 625 |
ReciprocalRandom(Random& rvx, Real sx) : rv(&rvx), s(sx) {} |
| 626 |
void tDelete() { rv->tDelete(); delete this; } |
| 627 |
|
| 628 |
public: |
| 629 |
Real Next(); |
| 630 |
ExtReal Mean() const { return Missing; } |
| 631 |
ExtReal Variance() const { return Missing; } |
| 632 |
friend ReciprocalRandom& operator/(Real, Random&); |
| 633 |
}; |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
class ShiftedRandom : public ScaledRandom |
| 638 |
{ |
| 639 |
ShiftedRandom(Random& rvx, Real sx) : ScaledRandom(rvx, sx) {} |
| 640 |
|
| 641 |
public: |
| 642 |
Real Next(); |
| 643 |
ExtReal Mean() const; |
| 644 |
ExtReal Variance() const; |
| 645 |
friend ShiftedRandom& operator+(Real, Random&); |
| 646 |
friend ShiftedRandom& operator+(Random&, Real); |
| 647 |
friend ShiftedRandom& operator-(Random&, Real); |
| 648 |
}; |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
class ReverseShiftedRandom : public ScaledRandom |
| 653 |
{ |
| 654 |
ReverseShiftedRandom(Random& rvx, Real sx) : ScaledRandom(rvx, sx) {} |
| 655 |
|
| 656 |
public: |
| 657 |
Real Next(); |
| 658 |
ExtReal Mean() const; |
| 659 |
ExtReal Variance() const; |
| 660 |
friend ReverseShiftedRandom& operator-(Real, Random&); |
| 661 |
}; |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
class RepeatedRandom : public Random |
| 666 |
{ |
| 667 |
Random* rv; int n; |
| 668 |
void tDelete() { rv->tDelete(); delete this; } |
| 669 |
RepeatedRandom(Random& rvx, int nx) : rv(&rvx), n(nx) {} |
| 670 |
|
| 671 |
public: |
| 672 |
Real Next(); |
| 673 |
ExtReal Mean() const; |
| 674 |
ExtReal Variance() const; |
| 675 |
friend class Random; |
| 676 |
}; |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
class MultipliedRandom : public Random |
| 681 |
{ |
| 682 |
protected: |
| 683 |
Random* rv1; Random* rv2; |
| 684 |
void tDelete() { rv1->tDelete(); rv2->tDelete(); delete this; } |
| 685 |
MultipliedRandom(Random& rv1x, Random& rv2x) : rv1(&rv1x), rv2(&rv2x) {} |
| 686 |
|
| 687 |
public: |
| 688 |
Real Next(); |
| 689 |
ExtReal Mean() const; |
| 690 |
ExtReal Variance() const; |
| 691 |
friend MultipliedRandom& operator*(Random&, Random&); |
| 692 |
}; |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
class AddedRandom : public MultipliedRandom |
| 697 |
{ |
| 698 |
AddedRandom(Random& rv1x, Random& rv2x) : MultipliedRandom(rv1x, rv2x) {} |
| 699 |
|
| 700 |
public: |
| 701 |
Real Next(); |
| 702 |
ExtReal Mean() const; |
| 703 |
ExtReal Variance() const; |
| 704 |
friend AddedRandom& operator+(Random&, Random&); |
| 705 |
}; |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
class SubtractedRandom : public MultipliedRandom |
| 710 |
{ |
| 711 |
SubtractedRandom(Random& rv1x, Random& rv2x) |
| 712 |
: MultipliedRandom(rv1x, rv2x) {} |
| 713 |
|
| 714 |
public: |
| 715 |
Real Next(); |
| 716 |
ExtReal Mean() const; |
| 717 |
ExtReal Variance() const; |
| 718 |
friend SubtractedRandom& operator-(Random&, Random&); |
| 719 |
}; |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
class DividedRandom : public MultipliedRandom |
| 724 |
{ |
| 725 |
DividedRandom(Random& rv1x, Random& rv2x) |
| 726 |
: MultipliedRandom(rv1x, rv2x) {} |
| 727 |
|
| 728 |
public: |
| 729 |
Real Next(); |
| 730 |
ExtReal Mean() const { return Missing; } |
| 731 |
ExtReal Variance() const { return Missing; } |
| 732 |
friend DividedRandom& operator/(Random&, Random&); |
| 733 |
}; |
| 734 |
|
| 735 |
|
| 736 |
class SumRandom : public Random |
| 737 |
{ |
| 738 |
void operator=(const SumRandom&) {} |
| 739 |
Random* rv; |
| 740 |
|
| 741 |
public: |
| 742 |
const char* Name(); |
| 743 |
SumRandom(NegatedRandom& rvx) : rv(&rvx) {} |
| 744 |
SumRandom(AddedRandom& rvx) : rv(&rvx) {} |
| 745 |
SumRandom(MultipliedRandom& rvx) : rv(&rvx) {} |
| 746 |
SumRandom(SubtractedRandom& rvx) : rv(&rvx) {} |
| 747 |
SumRandom(DividedRandom& rvx) : rv(&rvx) {} |
| 748 |
SumRandom(ShiftedRandom& rvx) : rv(&rvx) {} |
| 749 |
SumRandom(ReverseShiftedRandom& rvx) : rv(&rvx) {} |
| 750 |
SumRandom(ScaledRandom& rvx) : rv(&rvx) {} |
| 751 |
SumRandom(ReciprocalRandom& rvx) : rv(&rvx) {} |
| 752 |
SumRandom(RepeatedRandom& rvx) : rv(&rvx) {} |
| 753 |
Real Next() { return rv->Next(); } |
| 754 |
ExtReal Mean() const { return rv->Mean(); } |
| 755 |
ExtReal Variance() const { return rv->Variance(); } |
| 756 |
~SumRandom() { rv->tDelete(); } |
| 757 |
}; |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
class SelectedRandom : public Random |
| 764 |
{ |
| 765 |
friend class Random; |
| 766 |
Random* rv; Real p; |
| 767 |
SelectedRandom(Random& rvx, Real px) : rv(&rvx), p(px) {} |
| 768 |
void tDelete() { rv->tDelete(); delete this; } |
| 769 |
|
| 770 |
public: |
| 771 |
void load(int*, Real*, Random**); |
| 772 |
Real Next(); |
| 773 |
}; |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
class AddedSelectedRandom : public Random |
| 778 |
{ |
| 779 |
friend class Random; |
| 780 |
|
| 781 |
protected: |
| 782 |
Random* rv1; Random* rv2; |
| 783 |
AddedSelectedRandom(Random& rv1x, Random& rv2x) |
| 784 |
: rv1(&rv1x), rv2(&rv2x) {} |
| 785 |
void tDelete() { rv1->tDelete(); rv2->tDelete(); delete this; } |
| 786 |
|
| 787 |
public: |
| 788 |
int nelems() const; |
| 789 |
void load(int*, Real*, Random**); |
| 790 |
Real Next(); |
| 791 |
friend AddedSelectedRandom& operator+(SelectedRandom&, |
| 792 |
SelectedRandom&); |
| 793 |
friend AddedSelectedRandom& operator+(AddedSelectedRandom&, |
| 794 |
SelectedRandom&); |
| 795 |
friend AddedSelectedRandom& operator+(SelectedRandom&, |
| 796 |
AddedSelectedRandom&); |
| 797 |
friend AddedSelectedRandom& operator+(AddedSelectedRandom&, |
| 798 |
AddedSelectedRandom&); |
| 799 |
}; |
| 800 |
|
| 801 |
|
| 802 |
class MixedRandom : public Random |
| 803 |
{ |
| 804 |
void operator=(const MixedRandom&) {} |
| 805 |
int n; |
| 806 |
DiscreteGen* dg; |
| 807 |
Random** rv; |
| 808 |
ExtReal mean, var; |
| 809 |
void Build(Real*); |
| 810 |
|
| 811 |
public: |
| 812 |
const char* Name(); |
| 813 |
MixedRandom(int, Real*, Random**); |
| 814 |
MixedRandom(AddedSelectedRandom&); |
| 815 |
~MixedRandom(); |
| 816 |
Real Next(); |
| 817 |
ExtReal Mean() const { return mean; } |
| 818 |
ExtReal Variance() const { return var; } |
| 819 |
}; |
| 820 |
|
| 821 |
|
| 822 |
class RandomPermutation |
| 823 |
{ |
| 824 |
Random U; |
| 825 |
|
| 826 |
public: |
| 827 |
void Next(int N, int M, int p[], int start = 0); |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
void Next(int N, int p[], int start = 0) { Next(N, N, p, start); } |
| 832 |
}; |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
class RandomCombination : public RandomPermutation |
| 837 |
{ |
| 838 |
void SortAscending(int n, int gm[]); |
| 839 |
|
| 840 |
public: |
| 841 |
void Next(int N, int M, int p[], int start = 0) |
| 842 |
{ RandomPermutation::Next(N, M, p, start); SortAscending(M, p); } |
| 843 |
void Next(int N, int p[], int start = 0) { Next(N, N, p, start); } |
| 844 |
}; |
| 845 |
|
| 846 |
|
| 847 |
class VariPoisson |
| 848 |
{ |
| 849 |
Uniform U; |
| 850 |
Normal N; |
| 851 |
Poisson P100; |
| 852 |
Poisson P200; |
| 853 |
Real Next_very_small(Real mu); |
| 854 |
Real Next_small(Real mu); |
| 855 |
Real Next_large(Real mu); |
| 856 |
public: |
| 857 |
VariPoisson(); |
| 858 |
int iNext(Real mu); |
| 859 |
Real Next(Real mu); |
| 860 |
}; |
| 861 |
|
| 862 |
|
| 863 |
class VariBinomial |
| 864 |
{ |
| 865 |
Uniform U; |
| 866 |
Normal N; |
| 867 |
Real Next_very_small(int n, Real p); |
| 868 |
Real Next_small(int n, Real p); |
| 869 |
Real Next_large(int n, Real p); |
| 870 |
public: |
| 871 |
VariBinomial(); |
| 872 |
int iNext(int n, Real p); |
| 873 |
Real Next(int n, Real p); |
| 874 |
}; |
| 875 |
|
| 876 |
|
| 877 |
class VariLogNormal |
| 878 |
{ |
| 879 |
Normal N; |
| 880 |
public: |
| 881 |
VariLogNormal() {} |
| 882 |
Real Next(Real mean, Real sd); |
| 883 |
}; |
| 884 |
|
| 885 |
|
| 886 |
NegatedRandom& operator-(Random&); |
| 887 |
ScaledRandom& operator*(Real, Random&); |
| 888 |
ScaledRandom& operator*(Random&, Real); |
| 889 |
ScaledRandom& operator/(Random&, Real); |
| 890 |
ReciprocalRandom& operator/(Real, Random&); |
| 891 |
ShiftedRandom& operator+(Real, Random&); |
| 892 |
ShiftedRandom& operator+(Random&, Real); |
| 893 |
ShiftedRandom& operator-(Random&, Real); |
| 894 |
ReverseShiftedRandom& operator-(Real, Random&); |
| 895 |
MultipliedRandom& operator*(Random&, Random&); |
| 896 |
AddedRandom& operator+(Random&, Random&); |
| 897 |
SubtractedRandom& operator-(Random&, Random&); |
| 898 |
DividedRandom& operator/(Random&, Random&); |
| 899 |
AddedSelectedRandom& operator+(SelectedRandom&, SelectedRandom&); |
| 900 |
AddedSelectedRandom& operator+(AddedSelectedRandom&, SelectedRandom&); |
| 901 |
AddedSelectedRandom& operator+(SelectedRandom&, AddedSelectedRandom&); |
| 902 |
AddedSelectedRandom& operator+(AddedSelectedRandom&, AddedSelectedRandom&); |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
#ifdef use_namespace |
| 908 |
} |
| 909 |
#endif |
| 910 |
|
| 911 |
#endif |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|