MRPT  1.9.9
matrix_ops4_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://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 
14 #include <gtest/gtest.h>
16 #include <mrpt/math/CMatrixFixed.h>
19 #include <mrpt/math/ops_matrices.h>
20 #include <mrpt/random.h>
21 #include <Eigen/Dense>
22 
23 using namespace mrpt;
24 using namespace mrpt::math;
25 using namespace mrpt::random;
26 using namespace std;
27 
29 {
30  /* meanAndStd: Computes a row with the mean values of each column in the
31  matrix and
32  the associated vector with the standard deviation of each column. */
33 
34  const double dat_A[] = {
35  2.8955668335, 2.3041932983, 1.9002381085, 1.7993158652, 1.8456197228,
36  2.9632296740, 1.9368565578, 2.1988923358, 2.0547605617, 2.5655678993,
37  2.3041932983, 3.8406914364, 2.1811218706, 3.2312564555, 2.4736403918,
38  3.4703311380, 1.4874417483, 3.1073538218, 2.1353324397, 2.9541115932,
39  1.9002381085, 2.1811218706, 2.4942067597, 1.6851007198, 1.4585872052,
40  2.3015952197, 1.0955231591, 2.2979627790, 1.3918738834, 2.1854562572,
41  1.7993158652, 3.2312564555, 1.6851007198, 3.1226161015, 1.6779632687,
42  2.7195826381, 1.2397348013, 2.3757864319, 1.6291224768, 2.4463194915,
43  1.8456197228, 2.4736403918, 1.4585872052, 1.6779632687, 2.8123267839,
44  2.5860688816, 1.4131630919, 2.1914803135, 1.5542420639, 2.7170092067,
45  2.9632296740, 3.4703311380, 2.3015952197, 2.7195826381, 2.5860688816,
46  4.1669180394, 2.1145239023, 3.3214801332, 2.6694845663, 3.0742063088,
47  1.9368565578, 1.4874417483, 1.0955231591, 1.2397348013, 1.4131630919,
48  2.1145239023, 1.8928811570, 1.7097998455, 1.7205860530, 1.8710847505,
49  2.1988923358, 3.1073538218, 2.2979627790, 2.3757864319, 2.1914803135,
50  3.3214801332, 1.7097998455, 3.4592638415, 2.1518695071, 2.8907499694,
51  2.0547605617, 2.1353324397, 1.3918738834, 1.6291224768, 1.5542420639,
52  2.6694845663, 1.7205860530, 2.1518695071, 2.1110960664, 1.6731209980,
53  2.5655678993, 2.9541115932, 2.1854562572, 2.4463194915, 2.7170092067,
54  3.0742063088, 1.8710847505, 2.8907499694, 1.6731209980, 3.9093678727};
56 
57  // Compute mean & std of each column:
58  CVectorDouble result_mean, result_std;
59  mrpt::math::meanAndStdColumns(A, result_mean, result_std);
60 
61  // Result from MATLAB:
62  const double dat_good_M[] = {
63  2.246424086, 2.718547419, 1.899166596, 2.192679825, 2.073010093,
64  2.938742050, 1.648159507, 2.570463898, 1.909148862, 2.628699435};
65  const CVectorDouble good_M(dat_good_M);
66  const double dat_good_S[] = {
67  0.428901371, 0.720352792, 0.468999497, 0.684910097, 0.546595053,
68  0.604303301, 0.328759015, 0.582584159, 0.382009344, 0.644788760};
69  const CVectorDouble good_S(dat_good_S);
70 
71  EXPECT_NEAR((result_mean - good_M).sum_abs(), 0, 1e-4);
72  EXPECT_NEAR((result_std - good_S).sum_abs(), 0, 1e-4);
73 }
74 
75 TEST(Matrices, meanAndStdAll)
76 {
77  /* meanAndStd: Computes a row with the mean values of each column in the
78  matrix and
79  the associated vector with the standard deviation of each column. */
80 
81  const double dat_A[] = {
82  2.8955668335, 2.3041932983, 1.9002381085, 1.7993158652, 1.8456197228,
83  2.9632296740, 1.9368565578, 2.1988923358, 2.0547605617, 2.5655678993,
84  2.3041932983, 3.8406914364, 2.1811218706, 3.2312564555, 2.4736403918,
85  3.4703311380, 1.4874417483, 3.1073538218, 2.1353324397, 2.9541115932,
86  1.9002381085, 2.1811218706, 2.4942067597, 1.6851007198, 1.4585872052,
87  2.3015952197, 1.0955231591, 2.2979627790, 1.3918738834, 2.1854562572,
88  1.7993158652, 3.2312564555, 1.6851007198, 3.1226161015, 1.6779632687,
89  2.7195826381, 1.2397348013, 2.3757864319, 1.6291224768, 2.4463194915,
90  1.8456197228, 2.4736403918, 1.4585872052, 1.6779632687, 2.8123267839,
91  2.5860688816, 1.4131630919, 2.1914803135, 1.5542420639, 2.7170092067,
92  2.9632296740, 3.4703311380, 2.3015952197, 2.7195826381, 2.5860688816,
93  4.1669180394, 2.1145239023, 3.3214801332, 2.6694845663, 3.0742063088,
94  1.9368565578, 1.4874417483, 1.0955231591, 1.2397348013, 1.4131630919,
95  2.1145239023, 1.8928811570, 1.7097998455, 1.7205860530, 1.8710847505,
96  2.1988923358, 3.1073538218, 2.2979627790, 2.3757864319, 2.1914803135,
97  3.3214801332, 1.7097998455, 3.4592638415, 2.1518695071, 2.8907499694,
98  2.0547605617, 2.1353324397, 1.3918738834, 1.6291224768, 1.5542420639,
99  2.6694845663, 1.7205860530, 2.1518695071, 2.1110960664, 1.6731209980,
100  2.5655678993, 2.9541115932, 2.1854562572, 2.4463194915, 2.7170092067,
101  3.0742063088, 1.8710847505, 2.8907499694, 1.6731209980, 3.9093678727};
102  CMatrixDouble A(10, 10, dat_A);
103 
104  // Compute mean & std of each column:
105  double result_mean, result_std;
106  mrpt::math::meanAndStd(A, result_mean, result_std);
107 
108  // Result from MATLAB:
109  const double good_M = 2.282504177034;
110  const double good_S = 0.660890754096;
111 
112  EXPECT_NEAR(std::abs(result_mean - good_M), 0, 1e-4);
113  EXPECT_NEAR(std::abs(result_std - good_S), 0, 1e-4);
114 }
115 
116 TEST(Matrices, laplacian)
117 {
118  // The laplacian matrix of W is L = D - W. (D:diagonals with degrees of
119  // nodes)
120  const double W_vals[6 * 6] = {0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0,
121  0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1,
122  1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0};
123  const CMatrixDouble W(6, 6, W_vals);
124 
125  CMatrixDouble L;
126  mrpt::math::laplacian(W, L);
127 
128  const double real_laplacian_vals[6 * 6] = {
129  2, -1, 0, 0, -1, 0, -1, 3, -1, 0, -1, 0, 0, -1, 2, -1, 0, 0,
130  0, 0, -1, 3, -1, -1, -1, -1, 0, -1, 3, 0, 0, 0, 0, -1, 0, 1};
131  const CMatrixDouble GT_L(6, 6, real_laplacian_vals);
132 
133  EXPECT_NEAR((GT_L - L).sum_abs(), 0, 1e-4);
134 }
135 
136 TEST(Matrices, loadFromTextFile)
137 {
138  {
139  const std::string s1 =
140  "1 2 3\n"
141  "4 5 6";
142  std::stringstream s(s1);
143  CMatrixDouble M;
144  bool retval = false;
145  try
146  {
147  M.loadFromTextFile(s);
148  retval = true;
149  }
150  catch (const std::exception& e)
151  {
152  std::cerr << e.what() << std::endl;
153  }
154  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
155  EXPECT_EQ(M.rows(), 2);
156  EXPECT_EQ(M.cols(), 3);
157  }
158  {
159  const std::string s1 =
160  "1 \t 2\n"
161  " 4 \t\t 1 ";
162  std::stringstream s(s1);
163  CMatrixDouble M;
164  bool retval = false;
165  try
166  {
167  M.loadFromTextFile(s);
168  retval = true;
169  }
170  catch (const std::exception& e)
171  {
172  std::cerr << e.what() << std::endl;
173  }
174  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
175  EXPECT_EQ(M.rows(), 2);
176  EXPECT_EQ(M.cols(), 2);
177  }
178  {
179  const std::string s1 = "1 2";
180  std::stringstream s(s1);
181  CMatrixDouble M;
182  bool retval = false;
183  try
184  {
185  M.loadFromTextFile(s);
186  retval = true;
187  }
188  catch (const std::exception& e)
189  {
190  std::cerr << e.what() << std::endl;
191  }
192  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
193  EXPECT_EQ(M.rows(), 1);
194  EXPECT_EQ(M.cols(), 2);
195  }
196  {
197  const std::string s1 =
198  "1 2 3\n"
199  "0 1 0\n"
200  "4 5 6\n";
201  std::stringstream s(s1);
202  CMatrixDouble33 M;
203  bool retval = false;
204  try
205  {
206  M.loadFromTextFile(s);
207  retval = true;
208  }
209  catch (const std::exception& e)
210  {
211  std::cerr << e.what() << std::endl;
212  }
213  EXPECT_TRUE(retval) << "string:\n" << s1 << endl;
214  EXPECT_EQ(M.rows(), 3);
215  EXPECT_EQ(M.cols(), 3);
216  }
217  {
218  const std::string s1 =
219  "1 2 3\n"
220  "4 5\n";
221  std::stringstream s(s1);
222  CMatrixDouble M;
223  bool retval = false;
224  try
225  {
226  M.loadFromTextFile(s);
227  retval = true;
228  }
229  catch (std::exception&)
230  {
231  }
232  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
233  }
234  {
235  const std::string s1 =
236  "1 2 3\n"
237  "4 5\n";
238  std::stringstream s(s1);
239  CMatrixDouble M;
240  bool retval = false;
241  try
242  {
243  M.loadFromTextFile(s);
244  retval = true;
245  }
246  catch (std::exception&)
247  {
248  }
249  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
250  }
251  {
252  const std::string s1 = " \n";
253  std::stringstream s(s1);
254  CMatrixDouble M;
255  bool retval = false;
256  try
257  {
258  M.loadFromTextFile(s);
259  retval = true;
260  }
261  catch (std::exception&)
262  {
263  }
264  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
265  }
266  {
267  const std::string s1 =
268  "1 2 3\n"
269  "1 2 3\n"
270  "1 2 3";
271  std::stringstream s(s1);
272  CMatrixDouble22 M;
273  bool retval = false;
274  try
275  {
276  M.loadFromTextFile(s);
277  retval = true;
278  }
279  catch (std::exception&)
280  {
281  }
282  EXPECT_FALSE(retval) << "string:\n" << s1 << endl;
283  }
284 }
A namespace of pseudo-random numbers generators of diferent distributions.
A compile-time fixed-size numeric matrix container.
Definition: CMatrixFixed.h:33
EXPECT_TRUE(mrpt::system::fileExists(ini_fil))
const double dat_A[]
void laplacian(const MATIN &g, MATOUT &ret)
Computes the Laplacian of a square graph weight matrix.
Definition: ops_matrices.h:207
This file implements several operations that operate element-wise on individual or pairs of container...
This file implements miscelaneous matrix and matrix/vector operations, and internal functions in mrpt...
STL namespace.
This base provides a set of functions for maths stuff.
void meanAndStdColumns(const MAT_IN &m, VEC &outMeanVector, VEC &outStdVector, const bool unbiased_variance=true)
Computes a row with the mean values of each column in the matrix and the associated vector with the s...
Definition: ops_matrices.h:122
size_type rows() const
Number of rows in the matrix.
size_type cols() const
Number of columns in the matrix.
constexpr size_type rows() const
Number of rows in the matrix.
Definition: CMatrixFixed.h:227
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 (or all elements of a matrix)
TEST(Matrices, meanAndStdColumns)
EXPECT_EQ(out.image_pair_was_used.size(), NUM_IMGS)
constexpr size_type cols() const
Number of columns in the matrix.
Definition: CMatrixFixed.h:230
EXPECT_NEAR(out.cam_params.rightCameraPose.x, 0.1194, 0.005)
void loadFromTextFile(std::istream &f)
Loads a vector/matrix from a text file, compatible with MATLAB text format.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020