Main MRPT website > C++ reference for MRPT 1.5.7
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-2017, 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 
17 #include <mrpt/random.h>
18 #include <gtest/gtest.h>
19 #include <numeric> // std::accumulate()
20 
21 using namespace mrpt;
22 using namespace mrpt::utils;
23 using namespace mrpt::math;
24 using namespace mrpt::random;
25 using namespace mrpt::utils::metaprogramming;
26 using namespace std;
27 
28 
29 TEST(Matrices,meanAndStd)
30 {
31  /* meanAndStd: Computes a row with the mean values of each column in the matrix and
32  the associated vector with the standard deviation of each column. */
33 
34  const double dat_A[] = {2.8955668335,2.3041932983,1.9002381085,1.7993158652,1.8456197228,2.9632296740,1.9368565578,2.1988923358,2.0547605617,2.5655678993,2.3041932983,3.8406914364,2.1811218706,3.2312564555,2.4736403918,3.4703311380,1.4874417483,3.1073538218,2.1353324397,2.9541115932,1.9002381085,2.1811218706,2.4942067597,1.6851007198,1.4585872052,2.3015952197,1.0955231591,2.2979627790,1.3918738834,2.1854562572,1.7993158652,3.2312564555,1.6851007198,3.1226161015,1.6779632687,2.7195826381,1.2397348013,2.3757864319,1.6291224768,2.4463194915,1.8456197228,2.4736403918,1.4585872052,1.6779632687,2.8123267839,2.5860688816,1.4131630919,2.1914803135,1.5542420639,2.7170092067,2.9632296740,3.4703311380,2.3015952197,2.7195826381,2.5860688816,4.1669180394,2.1145239023,3.3214801332,2.6694845663,3.0742063088,1.9368565578,1.4874417483,1.0955231591,1.2397348013,1.4131630919,2.1145239023,1.8928811570,1.7097998455,1.7205860530,1.8710847505,2.1988923358,3.1073538218,2.2979627790,2.3757864319,2.1914803135,3.3214801332,1.7097998455,3.4592638415,2.1518695071,2.8907499694,2.0547605617,2.1353324397,1.3918738834,1.6291224768,1.5542420639,2.6694845663,1.7205860530,2.1518695071,2.1110960664,1.6731209980,2.5655678993,2.9541115932,2.1854562572,2.4463194915,2.7170092067,3.0742063088,1.8710847505,2.8907499694,1.6731209980,3.9093678727};
36 
37  // Compute mean & std of each column:
38  CVectorDouble result_mean, result_std;
39  A.meanAndStd(result_mean, result_std);
40 
41  // Result from MATLAB:
42  const double dat_good_M[] = { 2.246424086, 2.718547419, 1.899166596, 2.192679825, 2.073010093, 2.938742050, 1.648159507, 2.570463898, 1.909148862, 2.628699435 };
43  const Eigen::Matrix<double,10,1> good_M(dat_good_M);
44  const double dat_good_S[] = { 0.428901371, 0.720352792, 0.468999497, 0.684910097, 0.546595053, 0.604303301, 0.328759015, 0.582584159, 0.382009344, 0.644788760 };
45  const Eigen::Matrix<double,10,1> good_S(dat_good_S);
46 
47  EXPECT_NEAR((result_mean-good_M).array().abs().sum(),0,1e-4);
48  EXPECT_NEAR((result_std-good_S).array().abs().sum(),0,1e-4);
49 }
50 
52 {
53  /* meanAndStd: Computes a row with the mean values of each column in the matrix and
54  the associated vector with the standard deviation of each column. */
55 
56  const double dat_A[] = {2.8955668335,2.3041932983,1.9002381085,1.7993158652,1.8456197228,2.9632296740,1.9368565578,2.1988923358,2.0547605617,2.5655678993,2.3041932983,3.8406914364,2.1811218706,3.2312564555,2.4736403918,3.4703311380,1.4874417483,3.1073538218,2.1353324397,2.9541115932,1.9002381085,2.1811218706,2.4942067597,1.6851007198,1.4585872052,2.3015952197,1.0955231591,2.2979627790,1.3918738834,2.1854562572,1.7993158652,3.2312564555,1.6851007198,3.1226161015,1.6779632687,2.7195826381,1.2397348013,2.3757864319,1.6291224768,2.4463194915,1.8456197228,2.4736403918,1.4585872052,1.6779632687,2.8123267839,2.5860688816,1.4131630919,2.1914803135,1.5542420639,2.7170092067,2.9632296740,3.4703311380,2.3015952197,2.7195826381,2.5860688816,4.1669180394,2.1145239023,3.3214801332,2.6694845663,3.0742063088,1.9368565578,1.4874417483,1.0955231591,1.2397348013,1.4131630919,2.1145239023,1.8928811570,1.7097998455,1.7205860530,1.8710847505,2.1988923358,3.1073538218,2.2979627790,2.3757864319,2.1914803135,3.3214801332,1.7097998455,3.4592638415,2.1518695071,2.8907499694,2.0547605617,2.1353324397,1.3918738834,1.6291224768,1.5542420639,2.6694845663,1.7205860530,2.1518695071,2.1110960664,1.6731209980,2.5655678993,2.9541115932,2.1854562572,2.4463194915,2.7170092067,3.0742063088,1.8710847505,2.8907499694,1.6731209980,3.9093678727};
58 
59  // Compute mean & std of each column:
60  double result_mean, result_std;
61  A.meanAndStdAll(result_mean, result_std);
62 
63  // Result from MATLAB:
64  const double good_M = 2.282504177034;
65  const double good_S = 0.660890754096;
66 
67  EXPECT_NEAR(std::abs(result_mean-good_M),0,1e-4);
68  EXPECT_NEAR(std::abs(result_std-good_S),0,1e-4);
69 }
70 
71 TEST(Matrices,laplacian)
72 {
73  // The laplacian matrix of W is L = D - W. (D:diagonals with degrees of nodes)
74  const double W_vals[6*6]={
75  0, 1, 0, 0, 1, 0,
76  1, 0, 1, 0, 1, 0,
77  0, 1, 0, 1, 0, 0,
78  0, 0, 1, 0, 1, 1,
79  1, 1, 0, 1, 0, 0,
80  0, 0, 0, 1, 0, 0 };
81  const CMatrixDouble W(6,6, W_vals);
82 
83  CMatrixDouble L;
84  W.laplacian(L);
85 
86  const double real_laplacian_vals[6*6]={
87  2, -1, 0, 0, -1, 0,
88  -1, 3, -1, 0, -1, 0,
89  0, -1, 2, -1, 0, 0,
90  0, 0, -1, 3, -1, -1,
91  -1, -1, 0, -1, 3, 0,
92  0, 0, 0, -1, 0, 1 };
93  const CMatrixDouble GT_L(6,6, real_laplacian_vals);
94 
95  EXPECT_NEAR( (GT_L-L).array().abs().sum(), 0, 1e-4);
96 }
97 
99 {
100  {
101  const double dat_C1[] = {
102  13.737245,10.248641,-5.839599,11.108320,
103  10.248641,14.966139,-5.259922,11.662222,
104  -5.839599,-5.259922,9.608822,-4.342505,
105  11.108320,11.662222,-4.342505,12.121940 };
106  const CMatrixDouble44 C1(dat_C1);
107 
108  const double dat_REAL_EIGVEC[] = { 0.54800 , 0.57167, -0.29604 , 0.53409 };
109  const Eigen::Matrix<double,4,1> REAL_EIGVEC(dat_REAL_EIGVEC);
110  //const double REAL_LARGEST_EIGENVALUE = 38.40966;
111 
113  C1.largestEigenvector(lev,1e-3, 20);
114  EXPECT_NEAR( (REAL_EIGVEC-lev).array().abs().sum(), 0, 1e-3);
115  }
116 }
117 
119 {
120  {
121  const std::string s1 =
122  "1 2 3\n"
123  "4 5 6";
124  std::stringstream s(s1);
125  CMatrixDouble M;
126  bool retval = false;
127  try { M.loadFromTextFile(s); retval=true; } catch(std::exception &e) { std::cerr << e.what() << std::endl; }
128  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
129  EXPECT_EQ(M.rows(),2);
130  EXPECT_EQ(M.cols(),3);
131  }
132  {
133  const std::string s1 =
134  "1 \t 2\n"
135  " 4 \t\t 1 ";
136  std::stringstream s(s1);
137  CMatrixDouble M;
138  bool retval = false;
139  try { M.loadFromTextFile(s); retval=true; } catch(std::exception &e) { std::cerr << e.what() << std::endl; }
140  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
141  EXPECT_EQ(M.rows(),2);
142  EXPECT_EQ(M.cols(),2);
143  }
144  {
145  const std::string s1 =
146  "1 2";
147  std::stringstream s(s1);
148  CMatrixDouble M;
149  bool retval = false;
150  try { M.loadFromTextFile(s); retval=true; } catch(std::exception &e) { std::cerr << e.what() << std::endl; }
151  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
152  EXPECT_EQ(M.rows(),1);
153  EXPECT_EQ(M.cols(),2);
154  }
155  {
156  const std::string s1 =
157  "1 2 3\n"
158  "4 5 6\n";
159  std::stringstream s(s1);
161  bool retval = false;
162  try { M.loadFromTextFile(s); retval=true; } catch(std::exception &e) { std::cerr << e.what() << std::endl; }
163  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
164  EXPECT_EQ(M.rows(),2);
165  EXPECT_EQ(M.cols(),3);
166  }
167  {
168  const std::string s1 =
169  "1 2 3\n"
170  "4 5\n";
171  std::stringstream s(s1);
173  bool retval = false;
174  try { M.loadFromTextFile(s); retval=true; } catch(std::exception &) { }
175  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
176  }
177  {
178  const std::string s1 =
179  "1 2 3\n"
180  "4 5\n";
181  std::stringstream s(s1);
182  CMatrixDouble M;
183  bool retval = false;
184  try { M.loadFromTextFile(s); retval=true; } catch(std::exception &) { }
185  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
186  }
187  {
188  const std::string s1 =
189  " \n";
190  std::stringstream s(s1);
192  bool retval = false;
193  try { M.loadFromTextFile(s); retval=true; } catch(std::exception &) { }
194  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
195  }
196  {
197  const std::string s1 =
198  "1 2 3\n"
199  "1 2 3\n"
200  "1 2 3";
201  std::stringstream s(s1);
203  bool retval = false;
204  try { M.loadFromTextFile(s); retval=true; } catch(std::exception &) { }
205  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
206  }
207 }
208 
209 
A namespace of pseudo-random numbers genrators of diferent distributions.
void meanAndStd(VEC &outMeanVector, VEC &outStdVector, const bool unbiased_variance=true) const
Computes a row with the mean values of each column in the matrix and the associated vector with the s...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
const double dat_A[]
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:35
STL namespace.
GLdouble s
Definition: glext.h:3602
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.
Definition: CArrayNumeric.h:19
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
A set of utility objects for metaprogramming with STL algorithms.
GLsizei const GLchar ** string
Definition: glext.h:3919
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 largestEigenvector(OUTVECT &x, Scalar resolution=Scalar(0.01), size_t maxIterations=6, int *out_Iterations=NULL, float *out_estimatedResolution=NULL) const
Efficiently computes only the biggest eigenvector of the matrix using the Power Method, and returns it in the passed vector "x".



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019