Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleRandom2.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 ExampleRandom2.cpp
27  @relates Random
28  @brief Example2 for random number generator
29  @ingroup g_examples
30  @author MIP
31 */
32 #include <cmath>
33 #include <cstring>
34 #include <Base/Math/Random.hh>
35 
36 using namespace BIAS;
37 using namespace std;
38 
39 #define COUNT 1000
40 
41 int main()
42 {
43  Random ran1, ran2;
44  double num1, num2;
45  double mean1 = 10.0, mean2 = -30.0;
46  double sigma1 = 0.5, sigma2 = 10.0;
47  double angle = 0.16;
48  double dist[COUNT][2];
49  double covm[2][2];
50  double mean[2], var[2];
51 
52  // zero mean and var
53  for (int i=0; i<2; i++){
54  mean[i] = var[i] = 0.0;
55  for (int j=0; j<2; j++)
56  covm[i][j]=0.0;
57  }
58 
59  // calculate a "rotated" 2dimensional distribution for PCA
60  for (register unsigned int i=0; i<COUNT; i++){
61  num1=ran1.GetNormalDistributed(mean1, sigma1);
62  num2=ran2.GetNormalDistributed(mean2, sigma2);
63  dist[i][0]=num1 * cos(angle) - num2 * sin(angle);
64  dist[i][1]=num1 * sin(angle) + num2 * cos(angle);
65  mean[0]+=dist[i][0];
66  mean[1]+=dist[i][1];
67  cout << dist[i][0] << "\t" << dist[i][1] << endl;
68  }
69 
70  mean[0]/=COUNT;
71  mean[1]/=COUNT;
72  cerr << "mean:" << endl;
73  cerr << mean[0] << "\t" << mean[1] << endl;
74 
75  // calculate covariance matrix
76  for (register unsigned int i=0; i<COUNT; i++){
77  covm[0][0]+=(dist[i][0]-mean[0])*(dist[i][0]-mean[0]);
78  covm[1][1]+=(dist[i][1]-mean[1])*(dist[i][1]-mean[1]);
79  covm[0][1]+=(dist[i][1]-mean[1])*(dist[i][0]-mean[0]);
80  }
81  covm[0][0]/=COUNT-1;
82  covm[1][1]/=COUNT-1;
83  covm[0][1]/=COUNT-1;
84  covm[1][0]=covm[0][1];
85 
86  // output of the covariance matrix
87  cerr << "covariance matrix:" << endl;
88  for (int i=0; i<2; i++){
89  for (int j=0; j<2; j++){
90  cerr << covm[i][j] << "\t";
91  }
92  cerr << endl;
93  }
94 
95  return 0;
96 }
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