Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleRandom2D.cpp
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  @example ExampleRandom2D.cpp
27  @relates Random
28  @brief Example2D for random number generator
29  @ingroup g_examples
30  @author MIP
31 */
32 
33 #ifdef WIN32
34 # include "Base/Common/W32Compat.hh"
35 # include <math.h>
36 #endif
37 
38 #include <cmath>
39 #include <cstring>
40 #include <Base/Math/Random.hh>
41 
42 using namespace BIAS;
43 using namespace std;
44 
45 #define COUNT 9000
46 /**
47  @example ExampleRandom2D.cpp
48  @brief Example2D for random number generator
49  @ingroup g_examples
50  @author MIP
51 */
52 int main()
53 {
54  Random ran;
55 
56  ran.Reset();
57 
58  // generate random covarinace matrix
60  double cov_max=100.0;
61  Vector2<double> mean;
62  cov.SetZero();
63 
64  double sigma1, sigma2;
65  sigma1=ran.GetUniformDistributed(0.0, cov_max);
66  sigma2=ran.GetUniformDistributed(0.0, cov_max);
67  double ang=ran.GetUniformDistributed(0.0, M_PI*2.0);
68  //ang=0;
69  // covariance is transformed by R * cov * R.Transpose()
70  cov[0][0] = cos(ang) * cos(ang) * sigma1 + sin(ang) * sin(ang) * sigma2;
71  cov[1][1] = cos(ang) * cos(ang) * sigma2 + sin(ang) * sin(ang) * sigma1;
72  cov[0][1] = (sigma1-sigma2)*cos(ang)*sin(ang);
73  cov[1][0] = (sigma1-sigma2)*cos(ang)*sin(ang);
74  //double a, b, c, d;
75  // a = cov[0][0];
76  // c = cov[1][1];
77  // b = cov[0][1];
78  // d = cov[1][0];
79  // cout << "a "<<a <<"\tb "<<b<<"\tc "<<c<<"\td "<<d<<endl;
80  // cout << sigma1*sigma1+sigma1*(-c-a)+(a*c-b*d) << endl;
81  // cout << sigma2*sigma2+sigma2*(-c-a)+(a*c-b*d) << endl;
82  // cout << (a-sigma1)*(c-sigma1)-b*d << endl;
83  // cout << (a-sigma2)*(c-sigma2)-b*d << endl;
84 
85  mean[0] = ran.GetUniformDistributed(-cov_max, cov_max);
86  mean[1] = ran.GetUniformDistributed(-cov_max, cov_max);
87  //mean.SetZero();
88  cout << "input mean: "<<mean<<endl;
89  //cout << "sigma1: "<<sigma1<<"\tsigma2: "<<sigma2<<"\tangle: "<<ang<<endl;
90  cout << "input covariance: "<<cov<<endl;
91 
92  vector<Vector2<double> > points;
93 
94  if (ran.GetNormalDistributed(mean, cov, COUNT, points)!=0){
95  BIASERR("error");
96  return -1;
97  }
98 
99  // now calculate
100  double dx, dy;
101  Matrix2x2<double> ocov;
102  Vector2<double> omean;
103 
104  // zero mean and var
105  omean.SetZero();
106  ocov.SetZero();
107 
108 
109  // calculate mean
110  for (register unsigned int i=0; i<COUNT; i++){
111  //cout << points[i] << endl;
112  omean[0]+=points[i][0];
113  omean[1]+=points[i][1];
114  }
115 
116  omean[0]/=COUNT;
117  omean[1]/=COUNT;
118  cout << "output mean:" << omean << endl;
119 
120  // calculate covariance matrix
121  for (register unsigned int i=0; i<COUNT; i++){
122  dx = points[i][0]-omean[0];
123  dy = points[i][1]-omean[1];
124  ocov[0][0] += dx*dx;
125  ocov[1][1] += dy*dy;
126  ocov[0][1] += dx*dy;
127  }
128  ocov[1][0]=ocov[0][1];
129  ocov/=(double)(COUNT-1);
130 
131  // output of the covariance matrix
132  cout << "output covariance:" << ocov << endl;
133 
134  return 0;
135 }
136 
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 Reset()
calls srand() with a seed generated from system call time, also initializes some other variables ...
Definition: Random.cpp:113
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
void SetZero()
set the elements of this matrix to zero
Definition: Matrix2x2.hh:258
class for producing random numbers from different distributions
Definition: Random.hh:51
void SetZero()
Definition: Vector2.hh:196