source: Sophya/trunk/SophyaLib/NTools/perandom.cc@ 2838

Last change on this file since 2838 was 2838, checked in by cmv, 20 years ago

add FurRan constructor from Histo cmv 09/11/2005

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