Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Random.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #ifndef __BIAS_Random_hh__
27 #define __BIAS_Random_hh__
28 
29 #include <Base/Common/BIASpragmaStart.hh>
30 
31 #include <ctime>
32 #include <vector>
33 
34 #include <Base/Debug/Error.hh>
35 #include <Base/Debug/Debug.hh>
36 #include "Vector2.hh"
37 #include "Matrix2x2.hh"
38 
39 
40 namespace BIAS{
41 /** @class Random
42  @ingroup g_mathalgo
43  @brief class for producing random numbers from different distributions
44 
45  IMPORTANT: Problems arise when multiple of these instances are used
46  since every call to Reset() results in a call to srand() (from
47  stdlib) with a specific seed. Calling srand() is a global action
48  that will affect all other instances as well
49 
50  @author woelk 2002 */
51  class BIASMathBase_EXPORT Random : public Debug
52  {
53  public:
54  /** initializes vars/seed with deterministic values, NOT calling reset */
55  Random();
56 
57  /** calls Reset(seed) and therefore srand() */
58  Random(const long int seed);
59 
60  ~Random();
61 
62  /** calls srand() with a seed generated from system call time,
63  also initializes some other variables */
64  void Reset();
65 
66  /** calls srand with seed, also initializes some other variables */
67  void Reset(const long int seed);
68 
69  /** on succesive calls return normal distributed random variable
70  with mean and standard deviation sigma */
71  inline double GetNormalDistributed(const double mean, const double sigma)
72  { return (mean + (gasdevf_(&LongIdum_) * sigma)); };
73 
74  /** interface to GSL for comparison purposes */
75  double GetNormalDistributedGSL(const double mean, const double sigma);
76 
77  /** calculates num normal ditributed random variables and stores
78  them in res */
79  void GetNormalDistributed(const double mean, const double sigma,
80  const int num, std::vector<double>& res);
81 
82  /** on succesive calls return uniform distributed random variable
83  between min and max */
84  inline double GetUniformDistributed(const double min, const double max)
85  { return (min + (ran2_(&LongIdum_) * (max-min))); };
86 
87  /** calculates num uniform ditributed random variables and stores
88  them in res */
89  void GetUniformDistributed(const double min, const double max,
90  const int num, std::vector<double>& res);
91 
92  /** get uniform distributed random variable
93  including min/max */
94  int GetUniformDistributedInt(const int min, const int max);
95 
96  /** get uniform distributed random variable
97  including min/max */
98  unsigned GetUniformDistributedInt(const unsigned min, const unsigned max);
99 
100  /** See GetNormalDistributed().
101  @author woelk 07/2005 */
102  int GetNormalDistributed(const Vector2<double>& mean,
103  const Matrix2x2<double>& cov,
104  Vector2<double>& ran);
105 
106  /** Generates a vector of length num containing 2D (dependent) normal
107  distributed random variables. The 2D distribution is described by the
108  covariance matrix cov.
109 
110  @returns 0 on success , negative on error
111  @author woelk 07/2005 */
112  int GetNormalDistributed(const Vector2<double>& mean,
113  const Matrix2x2<double>& cov,
114  const int num,
115  std::vector<Vector2<double> >& ran);
116 
117  /** @brief On subsequent calls returns a Sobol' sequence of quasi random
118  numbers 'uniformly distributed' in a N-dimensional cube between min
119  and max. The dimension of the space must not exceed 6.
120  min[i] >= max[i] must hold for all i
121  @author woelk 11/2006 */
122  int GetQuasiUniformDistributed(const int dim,
123  const Vector<double>& min,
124  const Vector<double>& max,
125  Vector<double>& res);
126 
127  /** @brief Returns a Sobol' sequence of num quasi random numbers
128  'uniformly distributed' in the N-dimensional cube between min and max.
129  min[i] >= max[i] must hold for all i
130  @author woelk 11/2006 */
131  int GetQuasiUniformDistributed(const int dim,
132  const Vector<double>& min,
133  const Vector<double>& max,
134  const unsigned num,
135  std::vector<Vector<double> >& res);
136 
137  /** @brief On subsequent calls returns a Sobol' sequence of quasi random
138  numbers 'normal distributed' with mean 0 and Covariance matrix identity
139  Input: dim - dimension of requested sequqnce, in range [1,6]
140  Output: res - quasi random vector
141  See GetQuasiUniformDistributed() and sobseq_() for details.
142  @author woelk 11/2006 */
143  int GetQuasiNormalDistributed(const int dim, Vector<double>& res);
144 
145  /** @brief returns a quasi random seuqence of 'normal dsitributed'
146  numbres.
147  Input: dim - dimension of requested sequqnce, in range [1,6]
148  num - length of requested sequence
149  Output: res - quasi random vector sequence
150  See GetQuasiUniformDistributed() and sobseq_() for details.
151  @author woelk 11/2006 */
152  int GetQuasiNormalDistributed(const int dim, const unsigned num,
153  std::vector<Vector<double> >& res);
154 
155 
156  /// old deprectade interfaces
157  inline void GetNormalDistributed(const double mean, const double sigma,
158  const int num, double* res)
159  {
160  BIASERR("you are using a deprecated interface");
161  std::vector<double> mres;
162  GetNormalDistributed(mean, sigma, num, mres);
163  BIASASSERT((int)mres.size()==num);
164  for (int i=0; i<num; i++) res[i] = mres[i];
165  }
166  inline void GetUniformDistributed(const double min, const double max,
167  const int num, double* res)
168  {
169  BIASERR("you are using a deprecated interface");
170  std::vector<double> mres;
171  GetUniformDistributed(min, max, num, mres);
172  BIASASSERT((int)mres.size()==num);
173  for (int i=0; i<num; i++) res[i] = mres[i];
174  }
175 
176  /** @brief check-function to compute mean and cov from samples */
177  void GetMeanAndCovariance(const std::vector<double>& samples,
178  double &mean, double &covariance) const;
179 
180  /** @brief check-function to compute mean and cov from samples */
181  void GetMeanAndCovariance(const std::vector<BIAS::Vector<double> >&samples,
182  BIAS::Vector<double> &mean,
183  BIAS::Matrix<double> &covariance) const;
184 
185  protected:
186  unsigned int Seed_;
187  int Idum_;
188  long LongIdum_;
189 
190  /// produces uniform random variable between 0 and 1 exclusive
191  double ran1_(int *idum); // from nr pdf
192  double ran1c_(int *idum); // from D_RAN1.c
193  double ran2_(long *idum); // from D_RAN2.c
194  /// produces normal distributed random variable with unit variance and
195  /// zero mean
196  double gasdev_(int *idum);
197  // from makenois.c
198  double gasdev3_(int *idum);
199  // selbst gebastelt
200  double gasdevf_(long *idum);
201 
202  /** Generates a sobol sequence of n dimensional 'quasi' random vectors
203  on succesive calls.
204  When n is negative, internally intializes a set of MAXBIT=6 direction
205  numbers for each MAXDIM different Sobol' sequences. When n is positive
206  (but <=MAXDIM), returns as thevector x[0..n-1] the next values from n
207  of theses sequneces. (n must not be changed between initializations. )
208  See Press, "Numerical Recipes in C", Chapter 7.7, p. 312
209  http://www.nrbook.com/a/bookcpdf.html
210  @author woelk 11/2006 */
211  void sobseq_(int *n, double x[]);
212 
213 
214  /** @brief transfroms a uniform disributed random variable into a
215  normal distributed random variable
216 
217  The algiorithm is based on the inversion Principle, see
218  L. Devroye "Non-Uniform Random Variate Generation", 1986, Springer,
219  chapter II.2 for details
220 
221  Input: src - uniform deviat in range [-1.0, +1.0]
222  Output: dst - normal distributed deviate
223 
224  returns 0 on success
225  -1 when src is out of bounds
226 
227  @author woelk 11/2006 */
228  int Uniform2Normal_(const double src, double& dst) const;
229 
230  /** @brief Box-Muller transform generating 2 normal distributed
231  random variables from two uniform distributed random variables.
232 
233  Input: src1, src2 - uniform deviate in range [-1, +1] with
234  src1*src1+src2*src2<1.0 and
235  src1*src1+src2*src2!=0.0
236  Output: dst1, dst2 - normal deviates with sigma 1
237 
238  returns 0 on success,
239  -2 when src1 or src2 out of bounds
240  -1 either src1*src1+src2*src2>=1.0 or
241  rc1*src1+src2*src2 == 0.0
242 
243  See Press, "Numerical Recipes in C", Chapter 7, p. 289
244  http://www.nrbook.com/a/bookcpdf.html
245  for details.
246  @author woelk 11/2006 */
247  int Uniform2Normal_(const double src1, const double src2,
248  double& dst1, double& dst2) const;
249  };
250 
251 
252 } // namespace
253 
254 #include <Base/Common/BIASpragmaEnd.hh>
255 
256 #endif // __Random_hh__
257 
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
void GetUniformDistributed(const double min, const double max, const int num, double *res)
Definition: Random.hh:166
long LongIdum_
Definition: Random.hh:188
unsigned int Seed_
Definition: Random.hh:186
double GetNormalDistributed(const double mean, const double sigma)
on succesive calls return normal distributed random variable with mean and standard deviation sigma ...
Definition: Random.hh:71
class for producing random numbers from different distributions
Definition: Random.hh:51
void GetNormalDistributed(const double mean, const double sigma, const int num, double *res)
old deprectade interfaces
Definition: Random.hh:157