[244] | 1 | #include "machdefs.h"
|
---|
| 2 | #include "pexceptions.h"
|
---|
[220] | 3 | #include "perandom.h"
|
---|
| 4 | #include "pemath.h"
|
---|
[2322] | 5 | #include <iostream>
|
---|
[220] | 6 |
|
---|
| 7 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 8 | //++
|
---|
| 9 | // Class FunRan
|
---|
| 10 | // Lib Outils++
|
---|
| 11 | // include perandom.h
|
---|
| 12 | //
|
---|
| 13 | // Tirage aleatoire sur un histogramme 1D.
|
---|
| 14 | //--
|
---|
| 15 |
|
---|
| 16 | //++
|
---|
[1092] | 17 | FunRan::FunRan(FunRan::Func f, r_8 xMin, r_8 xMax, int_4 nBin)
|
---|
[220] | 18 | //
|
---|
| 19 | // Createur.
|
---|
| 20 | //--
|
---|
| 21 | : Histo(xMin, xMax, nBin)
|
---|
| 22 | {
|
---|
| 23 | (*this)(0) = f(BinLowEdge(0));
|
---|
[1092] | 24 | for(int_4 i=1; i<nBin; i++)
|
---|
[220] | 25 | (*this)(i) = (*this)(i-1) + f(BinLowEdge(i));
|
---|
| 26 |
|
---|
[1092] | 27 | for(int_4 j=0; j<nBin; j++)
|
---|
[220] | 28 | (*this)(j) /= (*this)(nBin-1);
|
---|
| 29 | END_CONSTRUCTOR
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | //++
|
---|
[1092] | 33 | FunRan::FunRan(r_8 *tab, int_4 nBin)
|
---|
[220] | 34 | //
|
---|
| 35 | // Createur.
|
---|
| 36 | //--
|
---|
[1092] | 37 | : Histo(0, (r_8)(nBin), nBin)
|
---|
[220] | 38 | {
|
---|
| 39 | (*this)(0) = tab[0];
|
---|
[1092] | 40 | for(int_4 i=1; i<nBin; i++)
|
---|
[220] | 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 |
|
---|
[1092] | 48 | for(int_4 j=0; j<nBin; j++)
|
---|
[220] | 49 | (*this)(j) /= (*this)(nBin-1);
|
---|
| 50 | END_CONSTRUCTOR
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[1092] | 53 | FunRan::FunRan(r_8 *tab, int_4 nBin, r_8 xMin, r_8 xMax)
|
---|
[220] | 54 | : Histo(xMin, xMax, nBin)
|
---|
| 55 | {
|
---|
| 56 | (*this)(0) = tab[0];
|
---|
[1092] | 57 | for(int_4 i=1; i<nBin; i++)
|
---|
[220] | 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 |
|
---|
[1092] | 65 | for(int_4 j=0; j<nBin; j++)
|
---|
[220] | 66 | (*this)(j) /= (*this)(nBin-1);
|
---|
| 67 | END_CONSTRUCTOR
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | //++
|
---|
[1092] | 71 | int_4 FunRan::BinRandom()
|
---|
[220] | 72 | //
|
---|
| 73 | // Tirage avec retour du numero de bin.
|
---|
| 74 | //--
|
---|
| 75 | {
|
---|
[1092] | 76 | r_8 z=drand01();
|
---|
[220] | 77 | if (z <= 0) return 0;
|
---|
[1092] | 78 | if (z >= 1) return mBins-1;
|
---|
[220] | 79 |
|
---|
| 80 | // recherche du premier bin plus grand que z
|
---|
[1092] | 81 | int_4 iBin = 0;
|
---|
| 82 | for (; iBin<mBins; iBin++)
|
---|
[220] | 83 | if (z < (*this)(iBin)) break;
|
---|
| 84 |
|
---|
| 85 | return iBin;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | //++
|
---|
[1092] | 89 | r_8 FunRan::Random()
|
---|
[220] | 90 | //
|
---|
| 91 | // Tirage avec retour abscisse du bin interpole.
|
---|
| 92 | //--
|
---|
| 93 | {
|
---|
[1092] | 94 | r_8 z=drand01();
|
---|
| 95 | if (z <= 0) return mMin;
|
---|
| 96 | if (z >= 1) return mMax;
|
---|
[220] | 97 | // cas z <= tab[0]
|
---|
| 98 | if (z <= (*this)(0)) {
|
---|
[1092] | 99 | r_8 t = mMin + binWidth/(*this)(0) * z;
|
---|
[220] | 100 | return t;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | // recherche du premier bin plus grand que z
|
---|
[1092] | 104 | int_4 iBin = 0;
|
---|
| 105 | for (; iBin<mBins; iBin++)
|
---|
[220] | 106 | if (z < (*this)(iBin)) break;
|
---|
| 107 |
|
---|
| 108 | // interpolation pour trouver la valeur du tirage aleatoire
|
---|
[1092] | 109 | r_8 t1 = (*this)(iBin-1);
|
---|
| 110 | r_8 x1 = BinLowEdge(iBin-1);
|
---|
| 111 | r_8 t2 = (*this)(iBin);
|
---|
| 112 | r_8 x2 = x1 + binWidth;
|
---|
| 113 | r_8 t = x1 + (x2-x1) / (t2-t1) * (z-t1);
|
---|
| 114 | if (t < mMin) t = mMin;
|
---|
| 115 | if (t > mMax) t = mMax;
|
---|
[220] | 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 | //++
|
---|
[1092] | 131 | FunRan2D::FunRan2D(r_8 *tab, int_4 nBinX, int_4 nBinY)
|
---|
[220] | 132 | //
|
---|
| 133 | // Createur.
|
---|
| 134 | //--
|
---|
| 135 | {
|
---|
| 136 | // Tirage en X, somme sur les Y.
|
---|
[1092] | 137 | r_8* tabX = new r_8[nBinX];
|
---|
| 138 | for (int_4 i=0; i<nBinX; i++) {
|
---|
[220] | 139 | tabX[i] = 0;
|
---|
[1092] | 140 | for (int_4 j=0; j<nBinY; j++) {
|
---|
[220] | 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 |
|
---|
[1092] | 149 | for (int_4 k=0; k<nBinX; k++)
|
---|
[220] | 150 | ranY[k] = new FunRan(tab + nBinY*k, nBinY);
|
---|
| 151 |
|
---|
| 152 | nx = nBinX;
|
---|
| 153 | END_CONSTRUCTOR
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | //++
|
---|
[1092] | 157 | FunRan2D::FunRan2D(r_8 **tab, int_4 nBinX, int_4 nBinY)
|
---|
[220] | 158 | //
|
---|
| 159 | // Createur.
|
---|
| 160 | //--
|
---|
| 161 | {
|
---|
| 162 | // Tirage en X, somme sur les Y.
|
---|
[1092] | 163 | r_8* tabX = new r_8[nBinX];
|
---|
| 164 | for (int_4 i=0; i<nBinX; i++) {
|
---|
[220] | 165 | tabX[i] = 0;
|
---|
[1092] | 166 | for (int_4 j=0; j<nBinY; j++) {
|
---|
[220] | 167 | tabX[i] += tab[i][j];
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | ranX = new FunRan(tabX, nBinX);
|
---|
| 171 |
|
---|
| 172 | ranY = new(FunRan*[nBinX]);
|
---|
| 173 |
|
---|
[1092] | 174 | for (int_4 k=0; k<nBinX; k++)
|
---|
[220] | 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 | {
|
---|
[1092] | 186 | for (int_4 i=nx-1; i>=0; i--)
|
---|
[220] | 187 | delete ranY[i];
|
---|
| 188 |
|
---|
| 189 | delete[] ranY;
|
---|
| 190 |
|
---|
| 191 | delete ranX;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | //++
|
---|
[1092] | 195 | void FunRan2D::BinRandom(int_4& x, int_4& y)
|
---|
[220] | 196 | //
|
---|
| 197 | // Tirage avec retour du numeros de bin.
|
---|
| 198 | //--
|
---|
| 199 | {
|
---|
| 200 | x = ranX->BinRandom();
|
---|
[244] | 201 | // FAILNIL(ranY[x]); Ne compile pas $CHECK$ Reza 22/04/99
|
---|
[220] | 202 | y = ranY[x]->BinRandom();
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | //++
|
---|
[1092] | 206 | void FunRan2D::Random(r_8& x, r_8& y)
|
---|
[220] | 207 | //
|
---|
| 208 | // Tirage avec retour abscisse et ordonnee
|
---|
| 209 | // du bin interpole.
|
---|
| 210 | //--
|
---|
| 211 | {
|
---|
| 212 | x = ranX->Random();
|
---|
[1092] | 213 | int_4 i = int_4(ceil(x));
|
---|
[244] | 214 | // FAILNIL(ranY[i]); Ne compile pas $CHECK$ Reza 22/04/99
|
---|
[220] | 215 | y = ranY[i]->Random();
|
---|
| 216 | }
|
---|