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

Last change on this file since 2615 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

File size: 4.0 KB
RevLine 
[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]18FunRan::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]33FunRan::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]52FunRan::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
68//++
[1092]69int_4 FunRan::BinRandom()
[220]70//
71// Tirage avec retour du numero de bin.
72//--
73{
[1092]74 r_8 z=drand01();
[220]75 if (z <= 0) return 0;
[1092]76 if (z >= 1) return mBins-1;
[220]77
78 // recherche du premier bin plus grand que z
[1092]79 int_4 iBin = 0;
80 for (; iBin<mBins; iBin++)
[220]81 if (z < (*this)(iBin)) break;
82
83 return iBin;
84}
85
86//++
[1092]87r_8 FunRan::Random()
[220]88//
89// Tirage avec retour abscisse du bin interpole.
90//--
91{
[1092]92 r_8 z=drand01();
93 if (z <= 0) return mMin;
94 if (z >= 1) return mMax;
[220]95 // cas z <= tab[0]
96 if (z <= (*this)(0)) {
[1092]97 r_8 t = mMin + binWidth/(*this)(0) * z;
[220]98 return t;
99 }
100
101 // recherche du premier bin plus grand que z
[1092]102 int_4 iBin = 0;
103 for (; iBin<mBins; iBin++)
[220]104 if (z < (*this)(iBin)) break;
105
106 // interpolation pour trouver la valeur du tirage aleatoire
[1092]107 r_8 t1 = (*this)(iBin-1);
108 r_8 x1 = BinLowEdge(iBin-1);
109 r_8 t2 = (*this)(iBin);
110 r_8 x2 = x1 + binWidth;
111 r_8 t = x1 + (x2-x1) / (t2-t1) * (z-t1);
112 if (t < mMin) t = mMin;
113 if (t > mMax) t = mMax;
[220]114 return(t);
115}
116
117
118
119////////////////////////////////////////////////////////////////////////////
120//++
121// Class FunRan2D
122// Lib Outils++
123// include perandom.h
124//
125// Tirage aleatoire sur un histogramme 2D.
126//--
127
128//++
[1092]129FunRan2D::FunRan2D(r_8 *tab, int_4 nBinX, int_4 nBinY)
[220]130//
131// Createur.
132//--
133{
134 // Tirage en X, somme sur les Y.
[1092]135 r_8* tabX = new r_8[nBinX];
136 for (int_4 i=0; i<nBinX; i++) {
[220]137 tabX[i] = 0;
[1092]138 for (int_4 j=0; j<nBinY; j++) {
[220]139 tabX[i] += tab[i*nBinY +j];
140 }
141 }
142 ranX = new FunRan(tabX, nBinX);
143 delete[] tabX;
144
145 ranY = new(FunRan*[nBinX]);
146
[1092]147 for (int_4 k=0; k<nBinX; k++)
[220]148 ranY[k] = new FunRan(tab + nBinY*k, nBinY);
149
150 nx = nBinX;
151}
152
153//++
[1092]154FunRan2D::FunRan2D(r_8 **tab, int_4 nBinX, int_4 nBinY)
[220]155//
156// Createur.
157//--
158{
159 // Tirage en X, somme sur les Y.
[1092]160 r_8* tabX = new r_8[nBinX];
161 for (int_4 i=0; i<nBinX; i++) {
[220]162 tabX[i] = 0;
[1092]163 for (int_4 j=0; j<nBinY; j++) {
[220]164 tabX[i] += tab[i][j];
165 }
166 }
167 ranX = new FunRan(tabX, nBinX);
168
169 ranY = new(FunRan*[nBinX]);
170
[1092]171 for (int_4 k=0; k<nBinX; k++)
[220]172 if (tabX[k] != 0)
173 ranY[k] = new FunRan(tab[k], nBinY);
174 else ranY[k] = NULL;
175
176 delete[] tabX;
177 nx = nBinX;
178}
179
180FunRan2D::~FunRan2D()
181{
[1092]182 for (int_4 i=nx-1; i>=0; i--)
[220]183 delete ranY[i];
184
185 delete[] ranY;
186
187 delete ranX;
188}
189
190//++
[1092]191void FunRan2D::BinRandom(int_4& x, int_4& y)
[220]192//
193// Tirage avec retour du numeros de bin.
194//--
195{
196 x = ranX->BinRandom();
[244]197 // FAILNIL(ranY[x]); Ne compile pas $CHECK$ Reza 22/04/99
[220]198 y = ranY[x]->BinRandom();
199}
200
201//++
[1092]202void FunRan2D::Random(r_8& x, r_8& y)
[220]203//
204// Tirage avec retour abscisse et ordonnee
205// du bin interpole.
206//--
207{
208 x = ranX->Random();
[1092]209 int_4 i = int_4(ceil(x));
[244]210 // FAILNIL(ranY[i]); Ne compile pas $CHECK$ Reza 22/04/99
[220]211 y = ranY[i]->Random();
212}
Note: See TracBrowser for help on using the repository browser.