Main MRPT website > C++ reference for 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 #include <mrpt/gui.h>
11 #include <mrpt/math/CMatrix.h>
12 #include <mrpt/math/utils.h>
13 #include <mrpt/math/ops_vectors.h>
14 #include <mrpt/math/ops_matrices.h>
15 #include <mrpt/system/CTicTac.h>
16 #include <iostream>
17 
18 using namespace mrpt;
19 using namespace mrpt::math;
20 using namespace mrpt::system;
21 using namespace std;
22 
23 #include <mrpt/examples_config.h>
24 string myDataDir(MRPT_EXAMPLES_BASE_DIRECTORY + string("math_matrix_example/"));
25 
26 // ------------------------------------------------------
27 // TestChol
28 // ------------------------------------------------------
29 void TestChol()
30 {
31  CMatrixFloat A, B;
32  A.loadFromTextFile(myDataDir + string("in_for_cholesky.txt"));
33  A.chol(B);
34 
35  cout << "Cholesky decomposition result:" << endl << B;
36 }
37 
38 void TestInitMatrix()
39 {
40  // Initialize a matrix from a C array:
41  const double numbers[] = {1, 2, 3, 4, 5, 6};
42  CMatrixDouble M(2, 3, numbers);
43  cout << "Initialized matrix (I): " << endl << M << endl;
44 
45  const double numbers2[] = {0.5, 4.5, 6.7, 8.9, 15.2};
47  loadVector(v1, numbers2);
48  cout << "Initialized double vector: " << v1 << endl;
49 
50  std::vector<int> v2;
51  loadVector(v2, numbers2);
52  cout << "Initialized int vector: " << v2 << endl;
53 
54  /* // I/O Test
55  CMatrixD B(M);
56  CFileOutputStream("mat.bin") << B;
57  CMatrixD A;
58  CFileInputStream("mat.bin") >> A;
59  cout << "B:" << endl << B;
60  cout << "A:" << endl << A;
61  */
62 }
63 
64 // ------------------------------------------------------
65 // TestHCH
66 // ------------------------------------------------------
67 void TestHCH()
68 {
69  CMatrixFloat H, C, RES;
70 
71  cout << "reading H.txt...";
72  H.loadFromTextFile(myDataDir + string("H.txt"));
73  cout << "ok" << endl;
74 
75  cout << "reading C.txt...";
76  C.loadFromTextFile(myDataDir + string("C.txt"));
77  cout << "ok" << endl;
78 
79  // RES = H * C * ~H
80  H.multiply_HCHt(C, RES);
81  cout << "Saving RES.txt ...";
82  RES.saveToTextFile("RES.txt");
83  cout << "ok" << endl;
84 
85  // The same for a column vector:
86  H.loadFromTextFile(myDataDir + string("H_col.txt"));
87  cout << "H*C*(~H) = " << H.multiply_HCHt_scalar(C) << endl;
88  cout << "Should be= 31.434 " << endl;
89 
90  // The same for a row vector:
91  H.loadFromTextFile(myDataDir + string("H_row.txt"));
92  cout << "Loaded H: " << endl << H;
93  cout << "H*C*(~H) = " << H.multiply_HCHt_scalar(C) << endl;
94  cout << "Should be= 31.434" << endl;
95 
97  Hfix.loadFromTextFile(myDataDir + string("H_row.txt"));
98  cout << "Again, loaded as a fixed matrix: " << endl << Hfix;
99 }
100 
101 // ------------------------------------------------------
102 // TestMatrixs
103 // ------------------------------------------------------
104 void TestMatrixTemplate()
105 {
106  CTicTac tictac;
107  CMatrixDouble M, Z, D, RES;
108 
109  // --------------------------------------
110  M.loadFromTextFile(myDataDir + string("matrixA.txt"));
111  cout << M << "\n";
112 
113  M.eigenVectors(Z, D);
114  // cout << "Z:\n" << Z << "D:\n" << D;
115 
116  int I, N = 1000;
117  tictac.Tic();
118  for (I = 0; I < N; I++) RES = Z * D * (~Z);
119  printf(
120  "Operation 'RES= Z * D * (~Z)' done in %.03fus\n",
121  1e6 * tictac.Tac() / N);
122 
123  // cout << "RES (1):\n" << RES;
124 
125  tictac.Tic();
126  for (I = 0; I < N; I++) Z.multiply_HCHt(D, RES);
127  printf(
128  "Operation 'Z.multiply_HCHt(D,RES)' done in %.03fus\n",
129  1e6f * tictac.Tac() / N);
130 
131  // cout << "RES (2):\n" << RES;
132 
133  CMatrixDouble Q(M);
134  // cout << "A:\n" << Q;
135 }
136 
137 // ------------------------------------------------------
138 // TestEigenvector
139 // ------------------------------------------------------
140 void TestEigenvector()
141 {
142  CMatrixFloat A, eigen; // Adjacency matrix
143 
144  // -------
145  CMatrixFloat Z, D;
146  A.loadFromTextFile(myDataDir + string("matrixA.txt"));
147  printf("A matrix loaded:\n");
148  cout << A << "\n";
149 
150  A.eigenVectors(Z, D);
151  cout << "Z:\n" << Z << "D:\n" << D;
152 
153  D = D.array().sqrt().matrix();
154  Z = Z * D;
155  cout << "Z:\n" << Z;
156 }
157 
158 // ------------------------------------------------------
159 // TestMatrixs
160 // ------------------------------------------------------
161 void TestMatrixs()
162 {
163  CMatrixFloat m, l;
164  CTicTac tictac;
165  double t;
166 
167  m.setSize(4, 4);
168  m(0, 0) = 4;
169  m(0, 1) = -2;
170  m(0, 2) = -1;
171  m(0, 3) = 0;
172  m(1, 0) = -2;
173  m(1, 1) = 4;
174  m(1, 2) = 0;
175  m(1, 3) = -1;
176  m(2, 0) = -1;
177  m(2, 1) = 0;
178  m(2, 2) = 4;
179  m(2, 3) = -2;
180  m(3, 0) = 0;
181  m(3, 1) = -1;
182  m(3, 2) = -2;
183  m(3, 3) = 4;
184 
185  cout << "Matrix:\n" << m << endl;
186 
187  // I/O test through a text file:
188  m.saveToTextFile("matrix1.txt");
189  tictac.Tic();
190  l.loadFromTextFile(myDataDir + string("matrix1.txt"));
191  t = tictac.Tac();
192  cout << "Read (text file) in " << 1e6 * t << "us:\n" << l << endl;
193 
194  m.laplacian(l);
195 
196  cout << "Laplacian:\n" << l << endl;
197 
198  CMatrixFloat Z, D;
199  m.eigenVectors(Z, D);
200 
201  cout << "Eigenvectors: M = Z * D * Z':\n Z=\n" << Z << endl;
202  cout << "D=\n" << D << endl;
203 
204  cout << "Z * D * Z'=\n" << Z * D * (~Z) << endl;
205 }
206 
207 void TestCov()
208 {
209  // Initialize a matrix from a C array:
210  const double numbers[] = {1, 2, 3, 10, 4, 5, 6, 14, 10, -5, -3, 1};
211  CMatrixDouble Mdyn(4, 3, numbers);
212  CMatrixFixedNumeric<double, 4, 3> Mfix(numbers);
213 
214  vector<CVectorDouble> samples(4);
215  for (size_t i = 0; i < 4; i++)
216  {
217  samples[i].resize(3);
218  for (size_t j = 0; j < 3; j++) samples[i][j] = Mdyn(i, j);
219  }
220 
221  cout << "COV (vector of vectors): " << endl
222  << mrpt::math::covVector<vector<CVectorDouble>, Eigen::MatrixXd>(
223  samples)
224  << endl;
225  cout << "COV (mat fix): " << endl << mrpt::math::cov(Mfix) << endl;
226  cout << "COV (mat dyn): " << endl << mrpt::math::cov(Mdyn) << endl;
227 }
228 
229 // ------------------------------------------------------
230 // MAIN
231 // ------------------------------------------------------
232 int main()
233 {
234  try
235  {
236  TestInitMatrix();
238  TestMatrixs();
239  TestHCH();
240  TestChol();
241  TestEigenvector();
242  TestCov();
243 
244  return 0;
245  }
246  catch (exception& e)
247  {
248  cout << "MRPT exception caught: " << e.what() << endl;
249  return -1;
250  }
251  catch (...)
252  {
253  printf("Untyped exception!!");
254  return -1;
255  }
256 }
samples
GLsizei samples
Definition: glext.h:8068
t
GLdouble GLdouble t
Definition: glext.h:3689
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
mrpt::math::dynamic_vector
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction.
Definition: eigen_frwds.h:44
CMatrix.h
myDataDir
std::string myDataDir
Definition: vision_stereo_rectify/test.cpp:23
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
TestChol
void TestChol()
Definition: vision_stereo_rectify/test.cpp:29
mrpt::math::cov
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
mrpt::math::loadVector
bool loadVector(std::istream &f, std::vector< int > &d)
Loads one row of a text file as a numerical std::vector.
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
mrpt::math::CMatrixTemplateNumeric
A matrix of dynamic size.
Definition: CMatrixTemplateNumeric.h:37
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
v1
GLfloat GLfloat v1
Definition: glext.h:4105
ops_vectors.h
v2
GLfloat GLfloat GLfloat v2
Definition: glext.h:4107
TestEigenvector
void TestEigenvector()
Definition: vision_stereo_rectify/test.cpp:140
ops_matrices.h
TestMatrixs
void TestMatrixs()
Definition: vision_stereo_rectify/test.cpp:161
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
TestHCH
void TestHCH()
Definition: vision_stereo_rectify/test.cpp:67
utils.h
mrpt::math::CMatrixFixedNumeric
A numeric matrix of compile-time fixed size.
Definition: CMatrixFixedNumeric.h:40
TestInitMatrix
void TestInitMatrix()
Definition: vision_stereo_rectify/test.cpp:38
CTicTac.h
TestCov
void TestCov()
Definition: vision_stereo_rectify/test.cpp:207
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
gui.h
TestMatrixTemplate
void TestMatrixTemplate()
Definition: vision_stereo_rectify/test.cpp:104
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST