00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_DISTRIBUTION_INL
00046 #define LASS_GUARDIAN_OF_INCLUSION_NUM_DISTRIBUTION_INL
00047
00048 #include "num_common.h"
00049 #include "distribution.h"
00050
00051 namespace lass
00052 {
00053 namespace num
00054 {
00055 namespace impl
00056 {
00057
00058 template <RangeType rangeType> struct RangePolicy;
00059
00060 template <>
00061 struct RangePolicy<rtClosed>
00062 {
00063 template <typename T>
00064 static bool isInRange(const T& x, const T& inf, const T& sup) { return x >= inf && x <= sup; }
00065 };
00066
00067 template <>
00068 struct RangePolicy<rtLeftOpen>
00069 {
00070 template <typename T>
00071 static bool isInRange(const T& x, const T& inf, const T& sup) { return x > inf && x <= sup; }
00072 };
00073
00074 template <>
00075 struct RangePolicy<rtRightOpen>
00076 {
00077 template <typename T>
00078 static bool isInRange(const T& x, const T& inf, const T& sup) { return x >= inf && x < sup; }
00079 };
00080
00081 template <>
00082 struct RangePolicy<rtOpen>
00083 {
00084 template <typename T>
00085 static bool isInRange(const T& x, const T& inf, const T& sup) { return x > inf && x < sup; }
00086 };
00087
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 template <typename T, class RG, RangeType RT>
00099 DistributionUniform<T, RG, RT>::DistributionUniform():
00100 generator_(0)
00101 {
00102 }
00103
00104
00105
00106
00107
00108
00109
00110 template <typename T, class RG, RangeType RT>
00111 DistributionUniform<T, RG, RT>::DistributionUniform(
00112 TGenerator& generator, TParam infimum, TParam supremum):
00113 generator_(&generator),
00114 infimum_(infimum),
00115 supremum_(supremum),
00116 scale_(static_cast<long double>(supremum - infimum) / TGenerator::max)
00117 {
00118 }
00119
00120
00121
00122 template <typename T, class RG, RangeType RT> inline
00123 typename DistributionUniform<T, RG, RT>::TValue
00124 DistributionUniform<T, RG, RT>::operator()() const
00125 {
00126 LASS_ASSERT(generator_);
00127 TValue result;
00128 do
00129 {
00130 result = infimum_ + static_cast<TValue>(scale_ * (*generator_)());
00131 }
00132 while (!impl::RangePolicy<RT>::isInRange(result, infimum_, supremum_));
00133 return result;
00134 }
00135
00136
00137
00138
00139
00140
00141 template <typename T, typename RandomGenerator> inline
00142 T distributeUniform(RandomGenerator& generator, T infimum, T supremum)
00143 {
00144 DistributionUniform<T, RandomGenerator> distribution(generator, infimum, supremum);
00145 return distribution();
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 template <typename T, class RG>
00157 DistributionExponential<T, RG>::DistributionExponential():
00158 generator_(0)
00159 {
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 template <typename T, class RG>
00171 DistributionExponential<T, RG>::DistributionExponential(TGenerator& generator, TParam rateOfChange):
00172 generator_(&generator),
00173 rateOfChange_(rateOfChange)
00174 {
00175 }
00176
00177
00178
00179 template <typename T, class RG> inline
00180 typename DistributionExponential<T, RG>::TValue
00181 DistributionExponential<T, RG>::operator()() const
00182 {
00183 LASS_ASSERT(generator_);
00184 TValue temp;
00185 do
00186 {
00187 temp = static_cast<TValue>((*generator_)()) / TGenerator::max;
00188 }
00189 while (temp == TNumTraits::zero);
00190 return -num::log(temp) / rateOfChange_;
00191 }
00192
00193
00194
00195
00196
00197
00198 template <typename T, typename RandomGenerator> inline
00199 T distributeExponential(RandomGenerator& generator, T rateOfChange)
00200 {
00201 DistributionExponential<T, RandomGenerator> distribution(generator, rateOfChange);
00202 return distribution();
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 template <typename T, class RG>
00214 DistributionNormal<T, RG>::DistributionNormal():
00215 generator_(0)
00216 {
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 template <typename T, class RG>
00228 DistributionNormal<T, RG>::DistributionNormal(
00229 TGenerator& generator, TParam mean, TParam standardDeviation):
00230 generator_(&generator),
00231 mean_(mean),
00232 standardDeviation_(standardDeviation),
00233 iset_(false)
00234 {
00235 }
00236
00237
00238
00239 template <typename T, class RG> inline
00240 typename DistributionNormal<T, RG>::TValue
00241 DistributionNormal<T, RG>::operator()() const
00242 {
00243 LASS_ASSERT(generator_);
00244
00245 if (iset_)
00246 {
00247 iset_ = false;
00248 return mean_ + standardDeviation_ * gset_;
00249 }
00250
00251 const TValue scale = static_cast<TValue>(2) / TGenerator::max;
00252 TValue rsq,v1,v2;
00253 do
00254 {
00255 v1 = scale * (*generator_)() - TNumTraits::one;
00256 v2 = scale * (*generator_)() - TNumTraits::one;
00257 rsq = v1 * v1 + v2 * v2;
00258 }
00259 while (rsq >= 1.0 || rsq == 0.0);
00260 const TValue fac = num::sqrt(-2 * num::log(rsq) / rsq);
00261
00262 gset_ = v1 * fac;
00263 iset_ = true;
00264 return mean_ + standardDeviation_ * v2 * fac;
00265 }
00266
00267
00268
00269
00270
00271
00272 template <typename T, typename RandomGenerator> inline
00273 T distributeNormal(RandomGenerator& generator, T mean, T standardDeviation)
00274 {
00275 DistributionNormal<T, RandomGenerator> distribution(generator, mean, standardDeviation);
00276 return distribution();
00277 }
00278
00279
00280
00281
00282
00283
00284
00285
00286 template<class T,class RG> T uniform(RG& generator)
00287 {
00288 return distributeUniform(generator);
00289 }
00290
00291
00292
00293
00294
00295
00296 template<class T,class RG> T unitGauss(RG& generator)
00297 {
00298 return distributeNormal(generator);
00299 }
00300
00301
00302
00303
00304
00305
00306
00307 template<class T,class RG> T gauss(
00308 RG& generator, typename util::CallTraits<T>::TParam mean,
00309 typename util::CallTraits<T>::TParam stddev)
00310 {
00311 return distributeNormal(generator, mean, stddev);
00312 }
00313
00314
00315
00316 }
00317
00318 }
00319
00320 #endif
00321
00322