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