00001
00002
00003
00004
00005 #ifndef NEWRAN_LIB
00006 #define NEWRAN_LIB 0
00007
00008
00009
00010
00011 #include "include.h"
00012 #include "myexcept.h"
00013 #include "extreal.h"
00014
00015 #ifdef use_namespace
00016 namespace NEWRAN { using namespace RBD_COMMON; }
00017 namespace RBD_LIBRARIES { using namespace NEWRAN; }
00018 namespace NEWRAN {
00019 #endif
00020
00021 typedef Real (*PDF)(Real);
00022
00023 Real ln_gamma(Real);
00024
00025
00026
00027 class RepeatedRandom;
00028 class SelectedRandom;
00029
00030 class Random
00031 {
00032 static double seed;
00033
00034 Real Buffer[128];
00035 Real Raw();
00036 void operator=(const Random&) {}
00037
00038 public:
00039 static void Set(double s);
00040 double Get();
00041 virtual Real Next();
00042 virtual char* Name();
00043 virtual Real Density(Real) const;
00044 Random();
00045 virtual ~Random() {}
00046 SelectedRandom& operator()(double);
00047 RepeatedRandom& operator()(int);
00048 virtual ExtReal Mean() const { return 0.5; }
00049
00050 virtual ExtReal Variance() const { return 1.0/12.0; }
00051
00052 virtual void tDelete() {}
00053 virtual int nelems() const { return 1; }
00054
00055 virtual void load(int*,Real*,Random**);
00056 friend class RandomPermutation;
00057
00058 virtual Random *Getrv( void ) { return 0; }
00059 };
00060
00061
00062
00063
00064 class Uniform : public Random
00065 {
00066 void operator=(const Uniform&) {}
00067
00068 public:
00069 char* Name();
00070 Uniform() {}
00071 Real Next() { return Random::Next(); }
00072 ExtReal Mean() const { return 0.5; }
00073 ExtReal Variance() const { return 1.0/12.0; }
00074 Real Density(Real x) const { return (x < 0.0 || x > 1.0) ? 0 : 1.0; }
00075 };
00076
00077
00078
00079
00080 class Constant : public Random
00081 {
00082 void operator=(const Constant&) {}
00083 Real value;
00084
00085 public:
00086 char* Name();
00087 Constant(Real v) { value=v; }
00088 Real Next() { return value; }
00089 ExtReal Mean() const { return value; }
00090 ExtReal Variance() const { return 0.0; }
00091 };
00092
00093
00094
00095 class PosGen : public Random
00096 {
00097 void operator=(const PosGen&) {}
00098
00099 protected:
00100 Real xi, *sx, *sfx;
00101 bool NotReady;
00102 void Build(bool);
00103
00104 public:
00105 char* Name();
00106 PosGen();
00107 virtual ~PosGen();
00108 Real Next();
00109 ExtReal Mean() const { return (ExtReal)Missing; }
00110 ExtReal Variance() const { return (ExtReal)Missing; }
00111 };
00112
00113
00114
00115 class SymGen : public PosGen
00116 {
00117 void operator=(const SymGen&) {}
00118
00119 public:
00120 char* Name();
00121 Real Next();
00122 };
00123
00124
00125
00126 class Normal : public SymGen
00127 {
00128 void operator=(const Normal&) {}
00129 static Real Nxi, *Nsx, *Nsfx;
00130 static long count;
00131
00132 public:
00133 char* Name();
00134 Normal();
00135 virtual ~Normal();
00136 Real Density(Real) const;
00137 ExtReal Mean() const { return 0.0; }
00138 ExtReal Variance() const { return 1.0; }
00139 };
00140
00141
00142
00143 class ChiSq : public Random
00144 {
00145 void operator=(const ChiSq&) {}
00146 Random* c1;
00147 Random* c2;
00148 int version;
00149 Real mean, var;
00150
00151 public:
00152 char* Name();
00153 ChiSq(int, Real=0.0);
00154 virtual ~ChiSq();
00155 ExtReal Mean() const { return mean; }
00156 ExtReal Variance() const { return var; }
00157 Real Next();
00158 };
00159
00160
00161
00162 class Cauchy : public SymGen
00163 {
00164 void operator=(const Cauchy&) {}
00165
00166 public:
00167 char* Name();
00168 Real Density(Real) const;
00169 ExtReal Mean() const { return Indefinite; }
00170 ExtReal Variance() const { return PlusInfinity; }
00171 };
00172
00173
00174
00175 class Exponential : public PosGen
00176 {
00177 void operator=(const Exponential&) {}
00178
00179 public:
00180 char* Name();
00181 Real Density(Real) const;
00182 ExtReal Mean() const { return 1.0; }
00183 ExtReal Variance() const { return 1.0; }
00184 };
00185
00186
00187
00188 class AsymGen : public Random
00189 {
00190 void operator=(const AsymGen&) {}
00191 Real xi, *sx, *sfx; int ic;
00192 bool NotReady;
00193 void Build();
00194
00195 protected:
00196 Real mode;
00197
00198 public:
00199 char* Name();
00200 AsymGen(Real);
00201 virtual ~AsymGen();
00202 Real Next();
00203 ExtReal Mean() const { return (ExtReal)Missing; }
00204 ExtReal Variance() const { return (ExtReal)Missing; }
00205 };
00206
00207
00208
00209 class Gamma : public Random
00210 {
00211 void operator=(const Gamma&) {}
00212 Random* method;
00213
00214 public:
00215 char* Name();
00216 Gamma(Real);
00217 virtual ~Gamma()
00218 {
00219 #ifdef MONITOR
00220 tron << "destructing Gamma\n";
00221 #endif
00222 delete method;
00223 }
00224 Real Next() { return method->Next(); }
00225 ExtReal Mean() const { return method->Mean(); }
00226 ExtReal Variance() const { return method->Variance(); }
00227 };
00228
00229
00230
00231 class PosGenX : public PosGen
00232 {
00233 void operator=(const PosGenX&) {}
00234 PDF f;
00235
00236 public:
00237 char* Name();
00238 PosGenX(PDF fx);
00239 Real Density(Real x) const { return (*f)(x); }
00240 };
00241
00242 class SymGenX : public SymGen
00243 {
00244 void operator=(const SymGenX&) {}
00245 PDF f;
00246
00247 public:
00248 char* Name();
00249 SymGenX(PDF fx);
00250 Real Density(Real x) const { return (*f)(x); }
00251 };
00252
00253 class AsymGenX : public AsymGen
00254 {
00255 void operator=(const AsymGenX&) {}
00256 PDF f;
00257
00258 public:
00259 char* Name();
00260 AsymGenX(PDF fx, Real mx);
00261 Real Density(Real x) const { return (*f)(x); }
00262 };
00263
00264
00265
00266 class Pareto : public Random
00267
00268
00269 {
00270 void operator=(const Pareto&) {}
00271 Real Shape, RS;
00272
00273 public:
00274 char* Name();
00275 Pareto(Real shape);
00276 virtual ~Pareto() {}
00277 Real Next();
00278 ExtReal Mean() const;
00279 ExtReal Variance() const;
00280 };
00281
00282
00283
00284
00285 class DiscreteGen : public Random
00286 {
00287 void operator=(const DiscreteGen&) {}
00288 Real *p; int *ialt; int n; Real *val;
00289 void Gen(int, Real*);
00290 Real mean, var;
00291
00292 public:
00293 char* Name();
00294 DiscreteGen(int,Real*);
00295 DiscreteGen(int,Real*,Real*);
00296 virtual ~DiscreteGen();
00297 Real Next();
00298 ExtReal Mean() const { return mean; }
00299 ExtReal Variance() const { return var; }
00300 };
00301
00302
00303
00304 class Poisson : public Random
00305 {
00306 void operator=(const Poisson&) {}
00307 Random* method;
00308
00309 public:
00310 char* Name();
00311 Poisson(Real);
00312 virtual ~Poisson()
00313 {
00314 #ifdef MONITOR
00315 tron << "destructing Poisson\n";
00316 #endif
00317 delete method;
00318 }
00319 Real Next() { return method->Next(); }
00320 ExtReal Mean() const { return method->Mean(); }
00321 ExtReal Variance() const { return method->Variance(); }
00322 };
00323
00324
00325
00326 class Binomial : public Random
00327 {
00328 void operator=(const Binomial&) {}
00329 Random* method;
00330
00331 public:
00332 char* Name();
00333 Binomial(int p, Real n);
00334 virtual ~Binomial()
00335 {
00336 #ifdef MONITOR
00337 tron << "destructing Binomial\n";
00338 #endif
00339 delete method;
00340 }
00341 Real Next() { return method->Next(); }
00342 ExtReal Mean() const { return method->Mean(); }
00343 ExtReal Variance() const { return method->Variance(); }
00344 };
00345
00346
00347
00348 class NegativeBinomial : public AsymGen
00349 {
00350 Real N, P, Q, p, ln_q, c;
00351
00352 public:
00353 char* Name();
00354 NegativeBinomial(Real NX, Real PX);
00355 Real Density(Real) const;
00356 Real Next();
00357 ExtReal Mean() const { return N * P; }
00358 ExtReal Variance() const { return N * P * Q; }
00359 };
00360
00361
00362
00363
00364
00365 class NegatedRandom : public Random
00366 {
00367 protected:
00368 Random* rv;
00369 void tDelete()
00370 {
00371 #ifdef MONITOR
00372 tron << "destructing NegatedRandom\n";
00373 #endif
00374 rv->tDelete(); delete this;
00375 }
00376
00377 NegatedRandom(Random& rvx) : rv(&rvx) {}
00378
00379 public:
00380 Real Next();
00381 ExtReal Mean() const;
00382 ExtReal Variance() const;
00383 virtual Random *Getrv( void ) { return rv; }
00384 friend NegatedRandom& operator-(Random&);
00385 };
00386
00387 class ScaledRandom : public Random
00388 {
00389 protected:
00390 Random* rv; Real s;
00391 ScaledRandom(Random& rvx, Real sx) : rv(&rvx), s(sx) {}
00392 void tDelete()
00393 {
00394 #ifdef MONITOR
00395 tron << "destructing ScaledRandom\n";
00396 #endif
00397 rv->tDelete(); delete this;
00398 }
00399
00400 public:
00401 Real Next();
00402 ExtReal Mean() const;
00403 ExtReal Variance() const;
00404 friend ScaledRandom& operator*(Real, Random&);
00405 friend ScaledRandom& operator*(Random&, Real);
00406 friend ScaledRandom& operator/(Random&, Real);
00407 };
00408
00409 class ReciprocalRandom : public Random
00410 {
00411 protected:
00412 Random* rv; Real s;
00413 ReciprocalRandom(Random& rvx, Real sx) : rv(&rvx), s(sx) {}
00414 void tDelete()
00415 {
00416 #ifdef MONITOR
00417 tron << "destructing ReciprocalRandom\n";
00418 #endif
00419 rv->tDelete(); delete this;
00420 }
00421
00422 public:
00423 Real Next();
00424 ExtReal Mean() const { return Missing; }
00425 ExtReal Variance() const { return Missing; }
00426 friend ReciprocalRandom& operator/(Real, Random&);
00427 };
00428
00429 class ShiftedRandom : public ScaledRandom
00430 {
00431 ShiftedRandom(Random& rvx, Real sx) : ScaledRandom(rvx, sx) {}
00432
00433 public:
00434 Real Next();
00435 ExtReal Mean() const;
00436 ExtReal Variance() const;
00437 friend ShiftedRandom& operator+(Real, Random&);
00438 friend ShiftedRandom& operator+(Random&, Real);
00439 friend ShiftedRandom& operator-(Random&, Real);
00440 };
00441
00442 class ReverseShiftedRandom : public ScaledRandom
00443 {
00444 ReverseShiftedRandom(Random& rvx, Real sx) : ScaledRandom(rvx, sx) {}
00445
00446 public:
00447 Real Next();
00448 ExtReal Mean() const;
00449 ExtReal Variance() const;
00450 friend ReverseShiftedRandom& operator-(Real, Random&);
00451 };
00452
00453 class RepeatedRandom : public Random
00454 {
00455 Random* rv; int n;
00456 void tDelete()
00457 {
00458 #ifdef MONITOR
00459 tron << "destructing RepeatedRandom\n";
00460 #endif
00461 rv->tDelete(); delete this;
00462 }
00463 RepeatedRandom(Random& rvx, int nx) : rv(&rvx), n(nx) {}
00464
00465 public:
00466 Real Next();
00467 ExtReal Mean() const;
00468 ExtReal Variance() const;
00469 friend class Random;
00470 };
00471
00472 class MultipliedRandom : public Random
00473 {
00474 protected:
00475 Random* rv1; Random* rv2;
00476 void tDelete()
00477 {
00478 #ifdef MONITOR
00479 tron << "destructing MultipliedRandom\n";
00480 #endif
00481 rv1->tDelete(); rv2->tDelete(); delete this;
00482 }
00483 MultipliedRandom(Random& rv1x, Random& rv2x) : rv1(&rv1x), rv2(&rv2x) {}
00484
00485 public:
00486 Real Next();
00487 ExtReal Mean() const;
00488 ExtReal Variance() const;
00489 friend MultipliedRandom& operator*(Random&, Random&);
00490 };
00491
00492 class AddedRandom : public MultipliedRandom
00493 {
00494 AddedRandom(Random& rv1x, Random& rv2x) : MultipliedRandom(rv1x, rv2x) {}
00495
00496 public:
00497 Real Next();
00498 ExtReal Mean() const;
00499 ExtReal Variance() const;
00500 friend AddedRandom& operator+(Random&, Random&);
00501 };
00502
00503 class OredRandom : public MultipliedRandom
00504 {
00505 OredRandom(Random& rv1x, Random& rv2x) : MultipliedRandom(rv1x, rv2x) , i( 0 ) {}
00506
00507 int i;
00508 public:
00509 Real Next();
00510 ExtReal Mean() const;
00511 ExtReal Variance() const;
00512 friend OredRandom& operator|(Random&, Random&);
00513 };
00514
00515 class SubtractedRandom : public MultipliedRandom
00516 {
00517 SubtractedRandom(Random& rv1x, Random& rv2x)
00518 : MultipliedRandom(rv1x, rv2x) {}
00519
00520 public:
00521 Real Next();
00522 ExtReal Mean() const;
00523 ExtReal Variance() const;
00524 friend SubtractedRandom& operator-(Random&, Random&);
00525 };
00526
00527 class DividedRandom : public MultipliedRandom
00528 {
00529 DividedRandom(Random& rv1x, Random& rv2x)
00530 : MultipliedRandom(rv1x, rv2x) {}
00531
00532 public:
00533 Real Next();
00534 ExtReal Mean() const { return Missing; }
00535 ExtReal Variance() const { return Missing; }
00536 friend DividedRandom& operator/(Random&, Random&);
00537 };
00538
00539 class SumRandom : public Random
00540 {
00541 void operator=(const SumRandom&) {}
00542 Random* rv;
00543
00544 public:
00545 char* Name();
00546 SumRandom( Random &rvx ) : rv(&rvx) {}
00547 SumRandom(NegatedRandom& rvx) : rv(&rvx) {}
00548 SumRandom(AddedRandom& rvx) : rv(&rvx) {}
00549 SumRandom(MultipliedRandom& rvx) : rv(&rvx) {}
00550 SumRandom(SubtractedRandom& rvx) : rv(&rvx) {}
00551 SumRandom(DividedRandom& rvx) : rv(&rvx) {}
00552 SumRandom(ShiftedRandom& rvx) : rv(&rvx) {}
00553 SumRandom(ReverseShiftedRandom& rvx) : rv(&rvx) {}
00554 SumRandom(ScaledRandom& rvx) : rv(&rvx) {}
00555 SumRandom(ReciprocalRandom& rvx) : rv(&rvx) {}
00556 SumRandom(RepeatedRandom& rvx) : rv(&rvx) {}
00557 Real Next()
00558 {
00559 return rv->Next();
00560 }
00561 ExtReal Mean() const { return rv->Mean(); }
00562 ExtReal Variance() const { return rv->Variance(); }
00563 virtual ~SumRandom()
00564 {
00565 #ifdef MONITOR
00566 tron << "destructing SumRandom\n";
00567 #endif
00568 rv->tDelete();
00569 rv = 0;
00570 }
00571 };
00572
00573
00574
00575
00576 class SelectedRandom : public Random
00577 {
00578 friend class Random;
00579 Random* rv; Real p;
00580 SelectedRandom(Random& rvx, Real px) : rv(&rvx), p(px) {}
00581 void tDelete()
00582 {
00583 #ifdef MONITOR
00584 tron << "destructing SelectedRandom\n";
00585 #endif
00586 rv->tDelete(); delete this;
00587 }
00588
00589 public:
00590 void load(int*, Real*, Random**);
00591 Real Next();
00592 };
00593
00594 class AddedSelectedRandom : public Random
00595 {
00596 friend class Random;
00597
00598 protected:
00599 Random* rv1; Random* rv2;
00600 AddedSelectedRandom(Random& rv1x, Random& rv2x)
00601 : rv1(&rv1x), rv2(&rv2x) {}
00602 void tDelete() {
00603 #ifdef MONITOR
00604 tron << "destructing AddedSelectedRandom\n";
00605 #endif
00606 rv1->tDelete(); rv2->tDelete();
00607 rv1 = 0;
00608 rv2 = 0;
00609 delete this;
00610 }
00611
00612 public:
00613 int nelems() const;
00614 void load(int*, Real*, Random**);
00615 Real Next();
00616 friend AddedSelectedRandom& operator+(SelectedRandom&,
00617 SelectedRandom&);
00618 friend AddedSelectedRandom& operator+(AddedSelectedRandom&,
00619 SelectedRandom&);
00620 friend AddedSelectedRandom& operator+(SelectedRandom&,
00621 AddedSelectedRandom&);
00622 friend AddedSelectedRandom& operator+(AddedSelectedRandom&,
00623 AddedSelectedRandom&);
00624 };
00625
00626 class MixedRandom : public Random
00627 {
00628 void operator=(const MixedRandom&) {}
00629 int n;
00630 DiscreteGen* dg;
00631 Random** rv;
00632 ExtReal mean, var;
00633 void Build(Real*);
00634
00635 public:
00636 char* Name();
00637 MixedRandom(int, Real*, Random**);
00638 MixedRandom(AddedSelectedRandom&);
00639 virtual ~MixedRandom();
00640 Real Next();
00641 ExtReal Mean() const { return mean; }
00642 ExtReal Variance() const { return var; }
00643 };
00644
00645
00646
00647 class RandomPermutation
00648 {
00649 Random U;
00650
00651 public:
00652 void Next(int N, int M, int p[], int start = 0);
00653
00654
00655
00656 void Next(int N, int p[], int start = 0) { Next(N, N, p, start); }
00657 };
00658
00659 class RandomCombination : public RandomPermutation
00660
00661
00662 {
00663 void SortAscending(int n, int gm[]);
00664
00665 public:
00666 void Next(int N, int M, int p[], int start = 0)
00667 { RandomPermutation::Next(N, M, p, start); SortAscending(M, p); }
00668 void Next(int N, int p[], int start = 0) { Next(N, N, p, start); }
00669 };
00670
00671 #include <vector>
00672
00673 class OredVectRandom : public std::vector<Random *> , public Random {
00674 private:
00675 int ati;
00676 public:
00677 OredVectRandom( void ) : ati( -1 ) {}
00678 ~OredVectRandom( void )
00679 {}
00680
00681 Real Next()
00682 {
00683 if( ++ati >= (int)size() )
00684 ati = 0;
00685 return at( ati )->Next();
00686 }
00687
00688 ExtReal Mean() const { return 0.0; }
00689 ExtReal Variance() const { return 0.0; }
00690 };
00691
00692
00693
00694 #ifdef use_namespace
00695 }
00696 #endif
00697
00698 #endif