| [658] | 1 | #include "machdefs.h" | 
|---|
|  | 2 | #include "pexceptions.h" | 
|---|
|  | 3 | #include "perandom.h" | 
|---|
|  | 4 | #include "pemath.h" | 
|---|
|  | 5 | #include <iostream.h> | 
|---|
|  | 6 |  | 
|---|
|  | 7 | //////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 8 | //++ | 
|---|
|  | 9 | // Class        FunRan | 
|---|
|  | 10 | // Lib  Outils++ | 
|---|
|  | 11 | // include      perandom.h | 
|---|
|  | 12 | // | 
|---|
|  | 13 | //      Tirage aleatoire sur un histogramme 1D. | 
|---|
|  | 14 | //-- | 
|---|
|  | 15 |  | 
|---|
|  | 16 | //++ | 
|---|
|  | 17 | FunRan::FunRan(FunRan::Func f, float xMin, float xMax, int nBin) | 
|---|
|  | 18 | // | 
|---|
|  | 19 | //      Createur. | 
|---|
|  | 20 | //-- | 
|---|
|  | 21 | : Histo(xMin, xMax, nBin) | 
|---|
|  | 22 | { | 
|---|
|  | 23 | (*this)(0) = f(BinLowEdge(0)); | 
|---|
|  | 24 | for(int i=1; i<nBin; i++) | 
|---|
|  | 25 | (*this)(i) = (*this)(i-1) + f(BinLowEdge(i)); | 
|---|
|  | 26 |  | 
|---|
|  | 27 | for(int j=0; j<nBin; j++) | 
|---|
|  | 28 | (*this)(j) /= (*this)(nBin-1); | 
|---|
|  | 29 | END_CONSTRUCTOR | 
|---|
|  | 30 | } | 
|---|
|  | 31 |  | 
|---|
|  | 32 | //++ | 
|---|
|  | 33 | FunRan::FunRan(double *tab, int nBin) | 
|---|
|  | 34 | // | 
|---|
|  | 35 | //      Createur. | 
|---|
|  | 36 | //-- | 
|---|
|  | 37 | : Histo(0, (float)(nBin), nBin) | 
|---|
|  | 38 | { | 
|---|
|  | 39 | (*this)(0) = tab[0]; | 
|---|
|  | 40 | for(int i=1; i<nBin; i++) | 
|---|
|  | 41 | (*this)(i) = (*this)(i-1) + tab[i]; | 
|---|
|  | 42 |  | 
|---|
|  | 43 | if ((*this)(nBin-1) == 0) { | 
|---|
|  | 44 | cerr << "FunRan::FunRan : sum(prob) = 0" << endl; | 
|---|
|  | 45 | exit(-1); | 
|---|
|  | 46 | } | 
|---|
|  | 47 |  | 
|---|
|  | 48 | for(int j=0; j<nBin; j++) | 
|---|
|  | 49 | (*this)(j) /= (*this)(nBin-1); | 
|---|
|  | 50 | END_CONSTRUCTOR | 
|---|
|  | 51 | } | 
|---|
|  | 52 |  | 
|---|
|  | 53 | FunRan::FunRan(double *tab, int nBin, float xMin, float xMax) | 
|---|
|  | 54 | : Histo(xMin, xMax, nBin) | 
|---|
|  | 55 | { | 
|---|
|  | 56 | (*this)(0) = tab[0]; | 
|---|
|  | 57 | for(int i=1; i<nBin; i++) | 
|---|
|  | 58 | (*this)(i) = (*this)(i-1) + tab[i]; | 
|---|
|  | 59 |  | 
|---|
|  | 60 | if ((*this)(nBin-1) == 0) { | 
|---|
|  | 61 | cerr << "FunRan::FunRan : sum(prob) = 0" << endl; | 
|---|
|  | 62 | exit(-1); | 
|---|
|  | 63 | } | 
|---|
|  | 64 |  | 
|---|
|  | 65 | for(int j=0; j<nBin; j++) | 
|---|
|  | 66 | (*this)(j) /= (*this)(nBin-1); | 
|---|
|  | 67 | END_CONSTRUCTOR | 
|---|
|  | 68 | } | 
|---|
|  | 69 |  | 
|---|
|  | 70 | //++ | 
|---|
|  | 71 | int FunRan::BinRandom() | 
|---|
|  | 72 | // | 
|---|
|  | 73 | //      Tirage avec retour du numero de bin. | 
|---|
|  | 74 | //-- | 
|---|
|  | 75 | { | 
|---|
|  | 76 | double z=drand01(); | 
|---|
|  | 77 | if (z <= 0) return 0; | 
|---|
|  | 78 | if (z >= 1) return bins-1; | 
|---|
|  | 79 |  | 
|---|
|  | 80 | // recherche du premier bin plus grand que z | 
|---|
|  | 81 | int iBin = 0; | 
|---|
|  | 82 | for (; iBin<bins; iBin++) | 
|---|
|  | 83 | if (z < (*this)(iBin)) break; | 
|---|
|  | 84 |  | 
|---|
|  | 85 | return iBin; | 
|---|
|  | 86 | } | 
|---|
|  | 87 |  | 
|---|
|  | 88 | //++ | 
|---|
|  | 89 | double FunRan::Random() | 
|---|
|  | 90 | // | 
|---|
|  | 91 | //      Tirage avec retour abscisse du bin interpole. | 
|---|
|  | 92 | //-- | 
|---|
|  | 93 | { | 
|---|
|  | 94 | double z=drand01(); | 
|---|
|  | 95 | if (z <= 0) return min; | 
|---|
|  | 96 | if (z >= 1) return max; | 
|---|
|  | 97 | // cas z <= tab[0] | 
|---|
|  | 98 | if (z <= (*this)(0)) { | 
|---|
|  | 99 | double t = min + binWidth/(*this)(0) * z; | 
|---|
|  | 100 | return t; | 
|---|
|  | 101 | } | 
|---|
|  | 102 |  | 
|---|
|  | 103 | // recherche du premier bin plus grand que z | 
|---|
|  | 104 | int iBin = 0; | 
|---|
|  | 105 | for (; iBin<bins; iBin++) | 
|---|
|  | 106 | if (z < (*this)(iBin)) break; | 
|---|
|  | 107 |  | 
|---|
|  | 108 | // interpolation pour trouver la valeur du tirage aleatoire | 
|---|
|  | 109 | double t1 = (*this)(iBin-1); | 
|---|
|  | 110 | double x1 = BinLowEdge(iBin-1); | 
|---|
|  | 111 | double t2 = (*this)(iBin); | 
|---|
|  | 112 | double x2 = x1 + binWidth; | 
|---|
|  | 113 | double t = x1 + (x2-x1) / (t2-t1) * (z-t1); | 
|---|
|  | 114 | if (t < min) t = min; | 
|---|
|  | 115 | if (t > max) t = max; | 
|---|
|  | 116 | return(t); | 
|---|
|  | 117 | } | 
|---|
|  | 118 |  | 
|---|
|  | 119 |  | 
|---|
|  | 120 |  | 
|---|
|  | 121 | //////////////////////////////////////////////////////////////////////////// | 
|---|
|  | 122 | //++ | 
|---|
|  | 123 | // Class        FunRan2D | 
|---|
|  | 124 | // Lib  Outils++ | 
|---|
|  | 125 | // include      perandom.h | 
|---|
|  | 126 | // | 
|---|
|  | 127 | //      Tirage aleatoire sur un histogramme 2D. | 
|---|
|  | 128 | //-- | 
|---|
|  | 129 |  | 
|---|
|  | 130 | //++ | 
|---|
|  | 131 | FunRan2D::FunRan2D(double *tab, int nBinX, int nBinY) | 
|---|
|  | 132 | // | 
|---|
|  | 133 | //      Createur. | 
|---|
|  | 134 | //-- | 
|---|
|  | 135 | { | 
|---|
|  | 136 | // Tirage en X, somme sur les Y. | 
|---|
|  | 137 | double* tabX = new double[nBinX]; | 
|---|
|  | 138 | for (int i=0; i<nBinX; i++) { | 
|---|
|  | 139 | tabX[i] = 0; | 
|---|
|  | 140 | for (int j=0; j<nBinY; j++) { | 
|---|
|  | 141 | tabX[i] += tab[i*nBinY +j]; | 
|---|
|  | 142 | } | 
|---|
|  | 143 | } | 
|---|
|  | 144 | ranX = new FunRan(tabX, nBinX); | 
|---|
|  | 145 | delete[] tabX; | 
|---|
|  | 146 |  | 
|---|
|  | 147 | ranY = new(FunRan*[nBinX]); | 
|---|
|  | 148 |  | 
|---|
|  | 149 | for (int k=0; k<nBinX; k++) | 
|---|
|  | 150 | ranY[k] = new FunRan(tab + nBinY*k, nBinY); | 
|---|
|  | 151 |  | 
|---|
|  | 152 | nx = nBinX; | 
|---|
|  | 153 | END_CONSTRUCTOR | 
|---|
|  | 154 | } | 
|---|
|  | 155 |  | 
|---|
|  | 156 | //++ | 
|---|
|  | 157 | FunRan2D::FunRan2D(double **tab, int nBinX, int nBinY) | 
|---|
|  | 158 | // | 
|---|
|  | 159 | //      Createur. | 
|---|
|  | 160 | //-- | 
|---|
|  | 161 | { | 
|---|
|  | 162 | // Tirage en X, somme sur les Y. | 
|---|
|  | 163 | double* tabX = new double[nBinX]; | 
|---|
|  | 164 | for (int i=0; i<nBinX; i++) { | 
|---|
|  | 165 | tabX[i] = 0; | 
|---|
|  | 166 | for (int j=0; j<nBinY; j++) { | 
|---|
|  | 167 | tabX[i] += tab[i][j]; | 
|---|
|  | 168 | } | 
|---|
|  | 169 | } | 
|---|
|  | 170 | ranX = new FunRan(tabX, nBinX); | 
|---|
|  | 171 |  | 
|---|
|  | 172 | ranY = new(FunRan*[nBinX]); | 
|---|
|  | 173 |  | 
|---|
|  | 174 | for (int k=0; k<nBinX; k++) | 
|---|
|  | 175 | if (tabX[k] != 0) | 
|---|
|  | 176 | ranY[k] = new FunRan(tab[k], nBinY); | 
|---|
|  | 177 | else ranY[k] = NULL; | 
|---|
|  | 178 |  | 
|---|
|  | 179 | delete[] tabX; | 
|---|
|  | 180 | nx = nBinX; | 
|---|
|  | 181 | END_CONSTRUCTOR | 
|---|
|  | 182 | } | 
|---|
|  | 183 |  | 
|---|
|  | 184 | FunRan2D::~FunRan2D() | 
|---|
|  | 185 | { | 
|---|
|  | 186 | for (int i=nx-1; i>=0; i--) | 
|---|
|  | 187 | delete ranY[i]; | 
|---|
|  | 188 |  | 
|---|
|  | 189 | delete[] ranY; | 
|---|
|  | 190 |  | 
|---|
|  | 191 | delete ranX; | 
|---|
|  | 192 | } | 
|---|
|  | 193 |  | 
|---|
|  | 194 | //++ | 
|---|
|  | 195 | void FunRan2D::BinRandom(int& x, int& y) | 
|---|
|  | 196 | // | 
|---|
|  | 197 | //      Tirage avec retour du numeros de bin. | 
|---|
|  | 198 | //-- | 
|---|
|  | 199 | { | 
|---|
|  | 200 | x = ranX->BinRandom(); | 
|---|
|  | 201 | //  FAILNIL(ranY[x]);  Ne compile pas $CHECK$ Reza 22/04/99 | 
|---|
|  | 202 | y = ranY[x]->BinRandom(); | 
|---|
|  | 203 | } | 
|---|
|  | 204 |  | 
|---|
|  | 205 | //++ | 
|---|
|  | 206 | void FunRan2D::Random(double& x, double& y) | 
|---|
|  | 207 | // | 
|---|
|  | 208 | //      Tirage avec retour abscisse et ordonnee | 
|---|
|  | 209 | //      du bin interpole. | 
|---|
|  | 210 | //-- | 
|---|
|  | 211 | { | 
|---|
|  | 212 | x = ranX->Random(); | 
|---|
|  | 213 | int i = int(ceil(x)); | 
|---|
|  | 214 | //  FAILNIL(ranY[i]);  Ne compile pas $CHECK$ Reza 22/04/99 | 
|---|
|  | 215 | y = ranY[i]->Random(); | 
|---|
|  | 216 | } | 
|---|