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