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, r_8 xMin, r_8 xMax, int_4 nBin)
|
---|
18 | //
|
---|
19 | // Createur.
|
---|
20 | //--
|
---|
21 | : Histo(xMin, xMax, nBin)
|
---|
22 | {
|
---|
23 | (*this)(0) = f(BinLowEdge(0));
|
---|
24 | for(int_4 i=1; i<nBin; i++)
|
---|
25 | (*this)(i) = (*this)(i-1) + f(BinLowEdge(i));
|
---|
26 |
|
---|
27 | for(int_4 j=0; j<nBin; j++)
|
---|
28 | (*this)(j) /= (*this)(nBin-1);
|
---|
29 | END_CONSTRUCTOR
|
---|
30 | }
|
---|
31 |
|
---|
32 | //++
|
---|
33 | FunRan::FunRan(r_8 *tab, int_4 nBin)
|
---|
34 | //
|
---|
35 | // Createur.
|
---|
36 | //--
|
---|
37 | : Histo(0, (r_8)(nBin), nBin)
|
---|
38 | {
|
---|
39 | (*this)(0) = tab[0];
|
---|
40 | for(int_4 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_4 j=0; j<nBin; j++)
|
---|
49 | (*this)(j) /= (*this)(nBin-1);
|
---|
50 | END_CONSTRUCTOR
|
---|
51 | }
|
---|
52 |
|
---|
53 | FunRan::FunRan(r_8 *tab, int_4 nBin, r_8 xMin, r_8 xMax)
|
---|
54 | : Histo(xMin, xMax, nBin)
|
---|
55 | {
|
---|
56 | (*this)(0) = tab[0];
|
---|
57 | for(int_4 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_4 j=0; j<nBin; j++)
|
---|
66 | (*this)(j) /= (*this)(nBin-1);
|
---|
67 | END_CONSTRUCTOR
|
---|
68 | }
|
---|
69 |
|
---|
70 | //++
|
---|
71 | int_4 FunRan::BinRandom()
|
---|
72 | //
|
---|
73 | // Tirage avec retour du numero de bin.
|
---|
74 | //--
|
---|
75 | {
|
---|
76 | r_8 z=drand01();
|
---|
77 | if (z <= 0) return 0;
|
---|
78 | if (z >= 1) return mBins-1;
|
---|
79 |
|
---|
80 | // recherche du premier bin plus grand que z
|
---|
81 | int_4 iBin = 0;
|
---|
82 | for (; iBin<mBins; iBin++)
|
---|
83 | if (z < (*this)(iBin)) break;
|
---|
84 |
|
---|
85 | return iBin;
|
---|
86 | }
|
---|
87 |
|
---|
88 | //++
|
---|
89 | r_8 FunRan::Random()
|
---|
90 | //
|
---|
91 | // Tirage avec retour abscisse du bin interpole.
|
---|
92 | //--
|
---|
93 | {
|
---|
94 | r_8 z=drand01();
|
---|
95 | if (z <= 0) return mMin;
|
---|
96 | if (z >= 1) return mMax;
|
---|
97 | // cas z <= tab[0]
|
---|
98 | if (z <= (*this)(0)) {
|
---|
99 | r_8 t = mMin + binWidth/(*this)(0) * z;
|
---|
100 | return t;
|
---|
101 | }
|
---|
102 |
|
---|
103 | // recherche du premier bin plus grand que z
|
---|
104 | int_4 iBin = 0;
|
---|
105 | for (; iBin<mBins; iBin++)
|
---|
106 | if (z < (*this)(iBin)) break;
|
---|
107 |
|
---|
108 | // interpolation pour trouver la valeur du tirage aleatoire
|
---|
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;
|
---|
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(r_8 *tab, int_4 nBinX, int_4 nBinY)
|
---|
132 | //
|
---|
133 | // Createur.
|
---|
134 | //--
|
---|
135 | {
|
---|
136 | // Tirage en X, somme sur les Y.
|
---|
137 | r_8* tabX = new r_8[nBinX];
|
---|
138 | for (int_4 i=0; i<nBinX; i++) {
|
---|
139 | tabX[i] = 0;
|
---|
140 | for (int_4 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_4 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(r_8 **tab, int_4 nBinX, int_4 nBinY)
|
---|
158 | //
|
---|
159 | // Createur.
|
---|
160 | //--
|
---|
161 | {
|
---|
162 | // Tirage en X, somme sur les Y.
|
---|
163 | r_8* tabX = new r_8[nBinX];
|
---|
164 | for (int_4 i=0; i<nBinX; i++) {
|
---|
165 | tabX[i] = 0;
|
---|
166 | for (int_4 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_4 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_4 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_4& x, int_4& 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(r_8& x, r_8& y)
|
---|
207 | //
|
---|
208 | // Tirage avec retour abscisse et ordonnee
|
---|
209 | // du bin interpole.
|
---|
210 | //--
|
---|
211 | {
|
---|
212 | x = ranX->Random();
|
---|
213 | int_4 i = int_4(ceil(x));
|
---|
214 | // FAILNIL(ranY[i]); Ne compile pas $CHECK$ Reza 22/04/99
|
---|
215 | y = ranY[i]->Random();
|
---|
216 | }
|
---|