MRPT  1.9.9
matrix_ops4_unittest.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 // Note: Matrices unit tests have been split in different files since
11 // building them with eigen3 eats a lot of RAM and may be a problem while
12 // compiling in small systems.
13 
16 #include <mrpt/random.h>
17 #include <gtest/gtest.h>
18 
19 using namespace mrpt;
20 using namespace mrpt::math;
21 using namespace mrpt::random;
22 using namespace std;
23 
24 TEST(Matrices, meanAndStd)
25 {
26  /* meanAndStd: Computes a row with the mean values of each column in the
27  matrix and
28  the associated vector with the standard deviation of each column. */
29 
30  const double dat_A[] = {
31  2.8955668335, 2.3041932983, 1.9002381085, 1.7993158652, 1.8456197228,
32  2.9632296740, 1.9368565578, 2.1988923358, 2.0547605617, 2.5655678993,
33  2.3041932983, 3.8406914364, 2.1811218706, 3.2312564555, 2.4736403918,
34  3.4703311380, 1.4874417483, 3.1073538218, 2.1353324397, 2.9541115932,
35  1.9002381085, 2.1811218706, 2.4942067597, 1.6851007198, 1.4585872052,
36  2.3015952197, 1.0955231591, 2.2979627790, 1.3918738834, 2.1854562572,
37  1.7993158652, 3.2312564555, 1.6851007198, 3.1226161015, 1.6779632687,
38  2.7195826381, 1.2397348013, 2.3757864319, 1.6291224768, 2.4463194915,
39  1.8456197228, 2.4736403918, 1.4585872052, 1.6779632687, 2.8123267839,
40  2.5860688816, 1.4131630919, 2.1914803135, 1.5542420639, 2.7170092067,
41  2.9632296740, 3.4703311380, 2.3015952197, 2.7195826381, 2.5860688816,
42  4.1669180394, 2.1145239023, 3.3214801332, 2.6694845663, 3.0742063088,
43  1.9368565578, 1.4874417483, 1.0955231591, 1.2397348013, 1.4131630919,
44  2.1145239023, 1.8928811570, 1.7097998455, 1.7205860530, 1.8710847505,
45  2.1988923358, 3.1073538218, 2.2979627790, 2.3757864319, 2.1914803135,
46  3.3214801332, 1.7097998455, 3.4592638415, 2.1518695071, 2.8907499694,
47  2.0547605617, 2.1353324397, 1.3918738834, 1.6291224768, 1.5542420639,
48  2.6694845663, 1.7205860530, 2.1518695071, 2.1110960664, 1.6731209980,
49  2.5655678993, 2.9541115932, 2.1854562572, 2.4463194915, 2.7170092067,
50  3.0742063088, 1.8710847505, 2.8907499694, 1.6731209980, 3.9093678727};
52 
53  // Compute mean & std of each column:
54  CVectorDouble result_mean, result_std;
55  A.meanAndStd(result_mean, result_std);
56 
57  // Result from MATLAB:
58  const double dat_good_M[] = {
59  2.246424086, 2.718547419, 1.899166596, 2.192679825, 2.073010093,
60  2.938742050, 1.648159507, 2.570463898, 1.909148862, 2.628699435};
61  const Eigen::Matrix<double, 10, 1> good_M(dat_good_M);
62  const double dat_good_S[] = {
63  0.428901371, 0.720352792, 0.468999497, 0.684910097, 0.546595053,
64  0.604303301, 0.328759015, 0.582584159, 0.382009344, 0.644788760};
65  const Eigen::Matrix<double, 10, 1> good_S(dat_good_S);
66 
67  EXPECT_NEAR((result_mean - good_M).array().abs().sum(), 0, 1e-4);
68  EXPECT_NEAR((result_std - good_S).array().abs().sum(), 0, 1e-4);
69 }
70 
71 TEST(Matrices, meanAndStdAll)
72 {
73  /* meanAndStd: Computes a row with the mean values of each column in the
74  matrix and
75  the associated vector with the standard deviation of each column. */
76 
77  const double dat_A[] = {
78  2.8955668335, 2.3041932983, 1.9002381085, 1.7993158652, 1.8456197228,
79  2.9632296740, 1.9368565578, 2.1988923358, 2.0547605617, 2.5655678993,
80  2.3041932983, 3.8406914364, 2.1811218706, 3.2312564555, 2.4736403918,
81  3.4703311380, 1.4874417483, 3.1073538218, 2.1353324397, 2.9541115932,
82  1.9002381085, 2.1811218706, 2.4942067597, 1.6851007198, 1.4585872052,
83  2.3015952197, 1.0955231591, 2.2979627790, 1.3918738834, 2.1854562572,
84  1.7993158652, 3.2312564555, 1.6851007198, 3.1226161015, 1.6779632687,
85  2.7195826381, 1.2397348013, 2.3757864319, 1.6291224768, 2.4463194915,
86  1.8456197228, 2.4736403918, 1.4585872052, 1.6779632687, 2.8123267839,
87  2.5860688816, 1.4131630919, 2.1914803135, 1.5542420639, 2.7170092067,
88  2.9632296740, 3.4703311380, 2.3015952197, 2.7195826381, 2.5860688816,
89  4.1669180394, 2.1145239023, 3.3214801332, 2.6694845663, 3.0742063088,
90  1.9368565578, 1.4874417483, 1.0955231591, 1.2397348013, 1.4131630919,
91  2.1145239023, 1.8928811570, 1.7097998455, 1.7205860530, 1.8710847505,
92  2.1988923358, 3.1073538218, 2.2979627790, 2.3757864319, 2.1914803135,
93  3.3214801332, 1.7097998455, 3.4592638415, 2.1518695071, 2.8907499694,
94  2.0547605617, 2.1353324397, 1.3918738834, 1.6291224768, 1.5542420639,
95  2.6694845663, 1.7205860530, 2.1518695071, 2.1110960664, 1.6731209980,
96  2.5655678993, 2.9541115932, 2.1854562572, 2.4463194915, 2.7170092067,
97  3.0742063088, 1.8710847505, 2.8907499694, 1.6731209980, 3.9093678727};
99 
100  // Compute mean & std of each column:
101  double result_mean, result_std;
102  A.meanAndStdAll(result_mean, result_std);
103 
104  // Result from MATLAB:
105  const double good_M = 2.282504177034;
106  const double good_S = 0.660890754096;
107 
108  EXPECT_NEAR(std::abs(result_mean - good_M), 0, 1e-4);
109  EXPECT_NEAR(std::abs(result_std - good_S), 0, 1e-4);
110 }
111 
112 TEST(Matrices, laplacian)
113 {
114  // The laplacian matrix of W is L = D - W. (D:diagonals with degrees of
115  // nodes)
116  const double W_vals[6 * 6] = {0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0,
117  0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1,
118  1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0};
119  const CMatrixDouble W(6, 6, W_vals);
120 
121  CMatrixDouble L;
122  W.laplacian(L);
123 
124  const double real_laplacian_vals[6 * 6] = {
125  2, -1, 0, 0, -1, 0, -1, 3, -1, 0, -1, 0, 0, -1, 2, -1, 0, 0,
126  0, 0, -1, 3, -1, -1, -1, -1, 0, -1, 3, 0, 0, 0, 0, -1, 0, 1};
127  const CMatrixDouble GT_L(6, 6, real_laplacian_vals);
128 
129  EXPECT_NEAR((GT_L - L).array().abs().sum(), 0, 1e-4);
130 }
131 
133 {
134  {
135  const double dat_C1[] = {13.737245, 10.248641, -5.839599, 11.108320,
136  10.248641, 14.966139, -5.259922, 11.662222,
137  -5.839599, -5.259922, 9.608822, -4.342505,
138  11.108320, 11.662222, -4.342505, 12.121940};
139  const CMatrixDouble44 C1(dat_C1);
140 
141  const double dat_REAL_EIGVEC[] = {0.54800, 0.57167, -0.29604, 0.53409};
142  const Eigen::Matrix<double, 4, 1> REAL_EIGVEC(dat_REAL_EIGVEC);
143  // const double REAL_LARGEST_EIGENVALUE = 38.40966;
144 
146  C1.largestEigenvector(lev, 1e-3, 20);
147  EXPECT_NEAR((REAL_EIGVEC - lev).array().abs().sum(), 0, 1e-3);
148  }
149 }
150 
152 {
153  {
154  const std::string s1 =
155  "1 2 3\n"
156  "4 5 6";
157  std::stringstream s(s1);
158  CMatrixDouble M;
159  bool retval = false;
160  try
161  {
162  M.loadFromTextFile(s);
163  retval = true;
164  }
165  catch (std::exception& e)
166  {
167  std::cerr << e.what() << std::endl;
168  }
169  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
170  EXPECT_EQ(M.rows(), 2);
171  EXPECT_EQ(M.cols(), 3);
172  }
173  {
174  const std::string s1 =
175  "1 \t 2\n"
176  " 4 \t\t 1 ";
177  std::stringstream s(s1);
178  CMatrixDouble M;
179  bool retval = false;
180  try
181  {
182  M.loadFromTextFile(s);
183  retval = true;
184  }
185  catch (std::exception& e)
186  {
187  std::cerr << e.what() << std::endl;
188  }
189  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
190  EXPECT_EQ(M.rows(), 2);
191  EXPECT_EQ(M.cols(), 2);
192  }
193  {
194  const std::string s1 = "1 2";
195  std::stringstream s(s1);
196  CMatrixDouble M;
197  bool retval = false;
198  try
199  {
200  M.loadFromTextFile(s);
201  retval = true;
202  }
203  catch (std::exception& e)
204  {
205  std::cerr << e.what() << std::endl;
206  }
207  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
208  EXPECT_EQ(M.rows(), 1);
209  EXPECT_EQ(M.cols(), 2);
210  }
211  {
212  const std::string s1 =
213  "1 2 3\n"
214  "4 5 6\n";
215  std::stringstream s(s1);
217  bool retval = false;
218  try
219  {
220  M.loadFromTextFile(s);
221  retval = true;
222  }
223  catch (std::exception& e)
224  {
225  std::cerr << e.what() << std::endl;
226  }
227  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
228  EXPECT_EQ(M.rows(), 2);
229  EXPECT_EQ(M.cols(), 3);
230  }
231  {
232  const std::string s1 =
233  "1 2 3\n"
234  "4 5\n";
235  std::stringstream s(s1);
237  bool retval = false;
238  try
239  {
240  M.loadFromTextFile(s);
241  retval = true;
242  }
243  catch (std::exception&)
244  {
245  }
246  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
247  }
248  {
249  const std::string s1 =
250  "1 2 3\n"
251  "4 5\n";
252  std::stringstream s(s1);
253  CMatrixDouble M;
254  bool retval = false;
255  try
256  {
257  M.loadFromTextFile(s);
258  retval = true;
259  }
260  catch (std::exception&)
261  {
262  }
263  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
264  }
265  {
266  const std::string s1 = " \n";
267  std::stringstream s(s1);
269  bool retval = false;
270  try
271  {
272  M.loadFromTextFile(s);
273  retval = true;
274  }
275  catch (std::exception&)
276  {
277  }
278  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
279  }
280  {
281  const std::string s1 =
282  "1 2 3\n"
283  "1 2 3\n"
284  "1 2 3";
285  std::stringstream s(s1);
287  bool retval = false;
288  try
289  {
290  M.loadFromTextFile(s);
291  retval = true;
292  }
293  catch (std::exception&)
294  {
295  }
296  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
297  }
298 }
A namespace of pseudo-random numbers generators of diferent distributions.
const double dat_A[]
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:44
STL namespace.
GLdouble s
Definition: glext.h:3676
TEST(Matrices, meanAndStd)
EIGEN_STRONG_INLINE void laplacian(Eigen::MatrixBase< OtherDerived > &ret) const
Computes the laplacian of this square graph weight matrix.
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
void largestEigenvector(OUTVECT &x, Scalar resolution=Scalar(0.01), size_t maxIterations=6, int *out_Iterations=nullptr, float *out_estimatedResolution=nullptr) const
Efficiently computes only the biggest eigenvector of the matrix using the Power Method, and returns it in the passed vector "x".
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
GLsizei const GLchar ** string
Definition: glext.h:4101
void meanAndStdAll(double &outMean, double &outStd, const bool unbiased_variance=true) const
Computes the mean and standard deviation of all the elements in the matrix as a whole.
void loadFromTextFile(const std::string &file)
Load matrix from a text file, compatible with MATLAB text format.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void meanAndStd(const VECTORLIKE &v, double &out_mean, double &out_std, bool unbiased=true)
Computes the standard deviation of a vector.



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