library of assembled shared sources

http://lass.cocamware.com

distribution.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 
00045 /** @defgroup Distribution
00046  *
00047  *  A set of distribution for random numbers
00048  *
00049  *  distribution classes:
00050  *
00051  *  @arg @ref DistributionUniform
00052  *  @arg @ref DistributionExponential
00053  *  @arg @ref DistributionNormal
00054  *
00055  *  backwards compatible functions:
00056  *  
00057  *  @arg @ref uniform
00058  *  @arg @ref unitGauss
00059  *  @arg @ref gauss
00060  */
00061 
00062 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_DISTRIBUTION_H
00063 #define LASS_GUARDIAN_OF_INCLUSION_NUM_DISTRIBUTION_H
00064 
00065 #include "num_common.h"
00066 #include "num_traits.h"
00067 
00068 namespace lass
00069 {
00070 namespace num
00071 {
00072 
00073 /** @ingroup Distribution
00074  *  enumeration indicating how a real-number range must be closed.
00075  */
00076 enum RangeType
00077 {
00078     rtClosed    = 0x0,  ///< range is closed on both sides: [inf, sup]
00079     rtRightOpen = 0x1,  ///< range is open to the right: [inf, sup)
00080     rtLeftOpen  = 0x2,  ///< range is open to the left: (inf, sup]
00081     rtOpen      = 0x3,  ///< range is open to both sides: (inf, sup)
00082 };
00083 
00084 
00085 
00086 /** @ingroup Distribution
00087  *  a uniform distribution mapper.
00088  *
00089  *  template arguments:
00090  *  @arg @a T result type
00091  *  @arg @a RandomGenerator type of random number generator, see @ref Random.
00092  *  @arg @a rangeType value of RangeType declaring if the range must be open or closed.
00093  */
00094 template <typename T, class RandomGenerator, RangeType rangeType = rtClosed>
00095 class DistributionUniform
00096 {
00097 public:
00098 
00099     typedef RandomGenerator TGenerator;                     ///< generator type
00100 
00101     typedef num::NumTraits<T> TNumTraits;                   ///< numeric traits of value type
00102     typedef typename util::CallTraits<T>::TValue TValue;    ///< value type
00103     typedef typename util::CallTraits<T>::TParam TParam;    ///< parameter value type
00104 
00105     DistributionUniform();
00106     DistributionUniform(TGenerator& generator,
00107         TParam infimum = TNumTraits::zero, TParam supremum = TNumTraits::one);
00108 
00109     TValue operator()() const;
00110 
00111 private:
00112 
00113     TGenerator* generator_;
00114     TValue infimum_;
00115     TValue supremum_;
00116     long double scale_;
00117 };
00118 
00119 template <typename T, typename RandomGenerator> 
00120 T distributeUniform(RandomGenerator& generator, T min = T(0), T max = T(1));
00121 
00122 
00123 
00124 /** @ingroup Distribution
00125  *  an exponential distribution mapper.
00126  *
00127  *  template arguments:
00128  *  @arg @a T result type
00129  *  @arg @a RandomGenerator type of random number generator, see @ref Random.
00130  *
00131  *  reference: Numerical Recipes in C: The Art of Scientific Computing, section 7.2
00132  */
00133 template <typename T, class RandomGenerator>
00134 class DistributionExponential
00135 {
00136 public:
00137 
00138     typedef RandomGenerator TGenerator;                     ///< generator type
00139 
00140     typedef num::NumTraits<T> TNumTraits;                   ///< numeric traits of value type
00141     typedef typename util::CallTraits<T>::TValue TValue;    ///< value type
00142     typedef typename util::CallTraits<T>::TParam TParam;    ///< parameter value type
00143 
00144     DistributionExponential();
00145     DistributionExponential(TGenerator& generator,
00146         TParam rateOfChange = TNumTraits::one);
00147 
00148     TValue operator()() const;
00149 
00150 private:
00151 
00152     TGenerator* generator_;
00153     TValue rateOfChange_;
00154 };
00155 
00156 template <typename T, typename RandomGenerator> 
00157 T distributeExponential(RandomGenerator& generator, T rateOfChange = T(1));
00158 
00159 
00160 
00161 /** @ingroup Distribution
00162  *  a normal distribution mapper (aka guassian distribution)
00163  *
00164  *  template arguments:
00165  *  @arg @a T result type
00166  *  @arg @a RandomGenerator type of random number generator, see @ref Random.
00167  *
00168  *  reference: Numerical Recipes in C: The Art of Scientific Computing, section 7.2
00169  */
00170 template <typename T, class RandomGenerator>
00171 class DistributionNormal
00172 {
00173 public:
00174 
00175     typedef RandomGenerator TGenerator;                     ///< generator type
00176 
00177     typedef num::NumTraits<T> TNumTraits;                   ///< numeric traits of value type
00178     typedef typename util::CallTraits<T>::TValue TValue;    ///< value type
00179     typedef typename util::CallTraits<T>::TParam TParam;    ///< parameter value type
00180 
00181     DistributionNormal();
00182     DistributionNormal(TGenerator& generator,
00183         TParam mean = TNumTraits::zero, TParam standardDeviation = TNumTraits::one);
00184 
00185     TValue operator()() const;
00186 
00187 private:
00188 
00189     TGenerator* generator_;
00190     TValue mean_;
00191     TValue standardDeviation_;
00192     mutable TValue gset_;
00193     mutable bool iset_;
00194 };
00195 
00196 template <typename T, typename RandomGenerator> 
00197 T distributeExponential(RandomGenerator& generator, T mean = T(0), T standardDeviation = T(1));
00198 
00199 
00200 
00201 // backwards compatibility functions
00202 
00203 template<class T,class RG> T uniform(RG& generator);
00204 template<class T,class RG> T unitGauss(RG& generator);
00205 template<class T,class RG> T gauss(RG& generator, typename util::CallTraits<T>::TParam mean,
00206                                    typename util::CallTraits<T>::TParam stddev);
00207 
00208 }
00209 
00210 }
00211 
00212 #include "distribution.inl"
00213 
00214 #endif
00215 
00216 // EOF

Generated on Mon Nov 10 14:20:04 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo