Changeset 1092 in Sophya for trunk/SophyaLib/NTools/perandom.cc
- Timestamp:
- Jul 26, 2000, 3:15:52 PM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/NTools/perandom.cc
r244 r1092 15 15 16 16 //++ 17 FunRan::FunRan(FunRan::Func f, float xMin, float xMax, intnBin)17 FunRan::FunRan(FunRan::Func f, r_8 xMin, r_8 xMax, int_4 nBin) 18 18 // 19 19 // Createur. … … 22 22 { 23 23 (*this)(0) = f(BinLowEdge(0)); 24 for(int i=1; i<nBin; i++)24 for(int_4 i=1; i<nBin; i++) 25 25 (*this)(i) = (*this)(i-1) + f(BinLowEdge(i)); 26 26 27 for(int j=0; j<nBin; j++)27 for(int_4 j=0; j<nBin; j++) 28 28 (*this)(j) /= (*this)(nBin-1); 29 29 END_CONSTRUCTOR … … 31 31 32 32 //++ 33 FunRan::FunRan( double *tab, intnBin)34 // 35 // Createur. 36 //-- 37 : Histo(0, ( float)(nBin), nBin)33 FunRan::FunRan(r_8 *tab, int_4 nBin) 34 // 35 // Createur. 36 //-- 37 : Histo(0, (r_8)(nBin), nBin) 38 38 { 39 39 (*this)(0) = tab[0]; 40 for(int i=1; i<nBin; i++)40 for(int_4 i=1; i<nBin; i++) 41 41 (*this)(i) = (*this)(i-1) + tab[i]; 42 42 … … 46 46 } 47 47 48 for(int j=0; j<nBin; j++)48 for(int_4 j=0; j<nBin; j++) 49 49 (*this)(j) /= (*this)(nBin-1); 50 50 END_CONSTRUCTOR 51 51 } 52 52 53 FunRan::FunRan( double *tab, int nBin, float xMin, floatxMax)53 FunRan::FunRan(r_8 *tab, int_4 nBin, r_8 xMin, r_8 xMax) 54 54 : Histo(xMin, xMax, nBin) 55 55 { 56 56 (*this)(0) = tab[0]; 57 for(int i=1; i<nBin; i++)57 for(int_4 i=1; i<nBin; i++) 58 58 (*this)(i) = (*this)(i-1) + tab[i]; 59 59 … … 63 63 } 64 64 65 for(int j=0; j<nBin; j++)65 for(int_4 j=0; j<nBin; j++) 66 66 (*this)(j) /= (*this)(nBin-1); 67 67 END_CONSTRUCTOR … … 69 69 70 70 //++ 71 int FunRan::BinRandom()71 int_4 FunRan::BinRandom() 72 72 // 73 73 // Tirage avec retour du numero de bin. 74 74 //-- 75 75 { 76 doublez=drand01();76 r_8 z=drand01(); 77 77 if (z <= 0) return 0; 78 if (z >= 1) return bins-1;78 if (z >= 1) return mBins-1; 79 79 80 80 // recherche du premier bin plus grand que z 81 int iBin = 0;82 for (; iBin< bins; iBin++)81 int_4 iBin = 0; 82 for (; iBin<mBins; iBin++) 83 83 if (z < (*this)(iBin)) break; 84 84 … … 87 87 88 88 //++ 89 doubleFunRan::Random()89 r_8 FunRan::Random() 90 90 // 91 91 // Tirage avec retour abscisse du bin interpole. 92 92 //-- 93 93 { 94 doublez=drand01();95 if (z <= 0) return m in;96 if (z >= 1) return m ax;94 r_8 z=drand01(); 95 if (z <= 0) return mMin; 96 if (z >= 1) return mMax; 97 97 // cas z <= tab[0] 98 98 if (z <= (*this)(0)) { 99 double t = min + binWidth/(*this)(0) * z;99 r_8 t = mMin + binWidth/(*this)(0) * z; 100 100 return t; 101 101 } 102 102 103 103 // recherche du premier bin plus grand que z 104 int iBin = 0;105 for (; iBin< bins; iBin++)104 int_4 iBin = 0; 105 for (; iBin<mBins; iBin++) 106 106 if (z < (*this)(iBin)) break; 107 107 108 108 // interpolation pour trouver la valeur du tirage aleatoire 109 doublet1 = (*this)(iBin-1);110 doublex1 = BinLowEdge(iBin-1);111 doublet2 = (*this)(iBin);112 doublex2 = x1 + binWidth;113 doublet = x1 + (x2-x1) / (t2-t1) * (z-t1);114 if (t < m in) t = min;115 if (t > m ax) t = max;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 116 return(t); 117 117 } … … 129 129 130 130 //++ 131 FunRan2D::FunRan2D( double *tab, int nBinX, intnBinY)131 FunRan2D::FunRan2D(r_8 *tab, int_4 nBinX, int_4 nBinY) 132 132 // 133 133 // Createur. … … 135 135 { 136 136 // Tirage en X, somme sur les Y. 137 double* tabX = new double[nBinX];138 for (int i=0; i<nBinX; i++) {137 r_8* tabX = new r_8[nBinX]; 138 for (int_4 i=0; i<nBinX; i++) { 139 139 tabX[i] = 0; 140 for (int j=0; j<nBinY; j++) {140 for (int_4 j=0; j<nBinY; j++) { 141 141 tabX[i] += tab[i*nBinY +j]; 142 142 } … … 147 147 ranY = new(FunRan*[nBinX]); 148 148 149 for (int k=0; k<nBinX; k++)149 for (int_4 k=0; k<nBinX; k++) 150 150 ranY[k] = new FunRan(tab + nBinY*k, nBinY); 151 151 … … 155 155 156 156 //++ 157 FunRan2D::FunRan2D( double **tab, int nBinX, intnBinY)157 FunRan2D::FunRan2D(r_8 **tab, int_4 nBinX, int_4 nBinY) 158 158 // 159 159 // Createur. … … 161 161 { 162 162 // Tirage en X, somme sur les Y. 163 double* tabX = new double[nBinX];164 for (int i=0; i<nBinX; i++) {163 r_8* tabX = new r_8[nBinX]; 164 for (int_4 i=0; i<nBinX; i++) { 165 165 tabX[i] = 0; 166 for (int j=0; j<nBinY; j++) {166 for (int_4 j=0; j<nBinY; j++) { 167 167 tabX[i] += tab[i][j]; 168 168 } … … 172 172 ranY = new(FunRan*[nBinX]); 173 173 174 for (int k=0; k<nBinX; k++)174 for (int_4 k=0; k<nBinX; k++) 175 175 if (tabX[k] != 0) 176 176 ranY[k] = new FunRan(tab[k], nBinY); … … 184 184 FunRan2D::~FunRan2D() 185 185 { 186 for (int i=nx-1; i>=0; i--)186 for (int_4 i=nx-1; i>=0; i--) 187 187 delete ranY[i]; 188 188 … … 193 193 194 194 //++ 195 void FunRan2D::BinRandom(int & x, int& y)195 void FunRan2D::BinRandom(int_4& x, int_4& y) 196 196 // 197 197 // Tirage avec retour du numeros de bin. … … 204 204 205 205 //++ 206 void FunRan2D::Random( double& x, double& y)206 void FunRan2D::Random(r_8& x, r_8& y) 207 207 // 208 208 // Tirage avec retour abscisse et ordonnee … … 211 211 { 212 212 x = ranX->Random(); 213 int i = int(ceil(x));213 int_4 i = int_4(ceil(x)); 214 214 // FAILNIL(ranY[i]); Ne compile pas $CHECK$ Reza 22/04/99 215 215 y = ranY[i]->Random();
Note:
See TracChangeset
for help on using the changeset viewer.