MRPT  1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /**
11  * random
12  * The example demonstrates the use of the random library.
13  */
14 
15 #include <mrpt/gui.h>
16 #include <mrpt/random.h>
18 #include <iostream>
19 
20 using namespace mrpt;
21 using namespace mrpt::gui;
22 using namespace mrpt::math;
23 using namespace mrpt::random;
24 using namespace mrpt::system;
25 using namespace std;
26 
27 // not run by default. Uncomment corresponding line in the main function.
28 void TestHist()
29 {
30  CHistogram hist(0.0, 100.0, 10u);
31  hist.add(86);
32  hist.add(7);
33  hist.add(45);
34 
35  cout << "Histogram test:" << endl;
36  cout << "Should be 1: " << hist.getBinCount(0) << endl; // Result: "1"
37  cout << "Should be 0.33: " << hist.getBinRatio(0)
38  << endl; // Result: "0.33"
39 }
40 
41 // ------------------------------------------------------
42 // TestRandomGenerators
43 // ------------------------------------------------------
45 {
46  vector<double> x, y;
47 
49 
50  // Uniform numbers integers:
51  CDisplayWindowPlots win1("Unif(0,5) (integers)");
52  win1.setPos(10, 10);
53  win1.resize(400, 400);
54  {
55  // CVectorDouble v1(100000);
56  std::vector<size_t> v1(100000);
58 
59  CHistogram hist(-2, 15, 100);
60  hist.add(v1);
61  hist.getHistogramNormalized(x, y);
62 
63  win1.plot(x, y, "b");
64 
65  win1.axis_fit();
66  }
67 
68  // Normalized Gauss:
69  CDisplayWindowPlots win2("N(mean=0,std=1)");
70  win2.setPos(420, 10);
71  win2.resize(400, 400);
72  {
73  CVectorDouble v1(100000);
75 
76  CHistogram hist(-5, 5, 100);
77  hist.add(v1);
78  hist.getHistogramNormalized(x, y);
79 
80  win2.plot(x, y, "b");
81 
82  CVectorDouble y_real(y.size());
83  for (CVectorDouble::Index k = 0; k < y_real.size(); k++)
84  y_real[k] = mrpt::math::normalPDF(x[k], 0, 1);
85  win2.plot(x, y_real, "k-", "real");
86 
87  win2.axis_fit();
88  }
89 
90  // Example Gauss:
91  CDisplayWindowPlots win3("N(mean=3,std=2)");
92  win3.setPos(10, 430);
93  win3.resize(400, 400);
94  {
95  CVectorDouble v1(100000);
97 
98  CHistogram hist(-5, 15, 100);
99  hist.add(v1);
100  hist.getHistogramNormalized(x, y);
101 
102  win3.plot(x, y, "b");
103 
104  CVectorDouble y_real(y.size());
105  for (CVectorDouble::Index k = 0; k < y_real.size(); k++)
106  y_real[k] = mrpt::math::normalPDF(x[k], 3, 2);
107  win3.plot(x, y_real, "k-", "real");
108 
109  win3.axis_fit();
110  }
111 
112  // Example multi-variate Gauss:
113  CDisplayWindowPlots win4("N(mean=[3 4],var=[4 -2;-2 4])");
114  win4.setPos(420, 430);
115  win4.resize(400, 400);
116  {
117  vector<CVectorDouble> v1;
118  CVectorDouble Mean(2);
119  Mean[0] = 3;
120  Mean[1] = 2;
121 
122  // CMatrixDouble cov(2,2);
124  cov.fromMatlabStringFormat("[7.5 -7;-7 8]");
125 
127  v1, 10000, cov, &Mean);
128 
129 #if 0
130  CVectorDouble m;
132  mrpt::math::meanAndCov(v1,m,c);
133  cout << "Mean: " << m << endl;
134  cout << "Std: " << endl << c << endl;
135 #endif
136 
137  // pass to (x,y) vectors:
138  CVectorDouble x(v1.size()), y(v1.size());
139  for (size_t i = 0; i < v1.size(); i++)
140  {
141  x[i] = v1[i][0];
142  y[i] = v1[i][1];
143  }
144 
145  win4.plot(x, y, "b.3");
146 
147  win4.plotEllipse(
148  Mean[0], Mean[1], cov, 3.0, "k-2", "99% ellipse", true);
149 
150  win4.axis_fit();
151  }
152 
154 }
155 
156 // ------------------------------------------------------
157 // MAIN
158 // ------------------------------------------------------
159 int main()
160 {
161  try
162  {
163  // TestHist();
165 
166  return 0;
167  }
168  catch (std::exception& e)
169  {
170  std::cout << "MRPT exception caught: " << e.what() << std::endl;
171  return -1;
172  }
173  catch (...)
174  {
175  printf("Untyped exception!!");
176  return -1;
177  }
178 }
A namespace of pseudo-random numbers generators of diferent distributions.
This class provides an easy way of computing histograms for unidimensional real valued variables...
Definition: CHistogram.h:33
Create a GUI window and display plots with MATLAB-like interfaces and commands.
void drawGaussian1DVector(VEC &v, const double mean=0, const double std=1)
Fills the given vector with independent, 1D-normally distributed samples.
void randomize(const uint32_t seed)
Initialize the PRNG from the given random seed.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:44
STL namespace.
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
const GLubyte * c
Definition: glext.h:6313
void drawGaussianMultivariateMany(VECTOR_OF_VECTORS &ret, size_t desiredSamples, const COVMATRIX &cov, const typename VECTOR_OF_VECTORS::value_type *mean=nullptr)
Generate a given number of multidimensional random samples according to a given covariance matrix...
void pause(const std::string &msg=std::string("Press any key to continue...")) noexcept
Shows the message "Press any key to continue" (or other custom message) to the current standard outpu...
Definition: os.cpp:428
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLfloat GLfloat v1
Definition: glext.h:4105
GLenum GLint GLint y
Definition: glext.h:3538
Eigen::Matrix< typename MATRIX::Scalar, MATRIX::ColsAtCompileTime, MATRIX::ColsAtCompileTime > cov(const MATRIX &v)
Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...
Definition: ops_matrices.h:148
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
GLenum GLint x
Definition: glext.h:3538
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
double normalPDF(double x, double mu, double std)
Evaluates the univariate normal (Gaussian) distribution at a given point "x".
Definition: math.cpp:33
void drawUniformVector(VEC &v, const double unif_min=0, const double unif_max=1)
Fills the given vector with independent, uniformly distributed samples.
void TestRandomGenerators()



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020