Main MRPT website > C++ reference for MRPT 1.5.9
matrix_ops3_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 
15 #include <mrpt/math/utils.h>
18 #include <mrpt/random.h>
19 #include <gtest/gtest.h>
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 #define CHECK_AND_RET_ERROR(_COND_,_MSG_) EXPECT_FALSE(_COND_) << _MSG_;
29 
30 TEST(Matrices,HCHt_3x2_2x2_2x3)
31 {
32  const double dat_H[] = { 0.2, -0.3, 0.1, 0.9, -0.07, 1.2 };
33  CMatrixDouble H(3,2, dat_H);
34 
35  const double dat_C[] = { 0.8, -0.1, -0.1, 0.8 };
36  CMatrixDouble C(2,2, dat_C);
37 
38  const double dat_R[] = { 0.11600 , -0.21500, -0.32530, -0.21500 , 0.63800 , 0.85270, -0.32530 , 0.85270 , 1.17272 };
39  CMatrixDouble R_REAL(3,3, dat_R);
40 
42  H.multiply_HCHt(C,R);
43 
44  EXPECT_NEAR( (R_REAL-R).array().abs().sum(),0,1e-4);
45 }
46 
47 TEST(Matrices,HCHt_scalar_1x2_2x2_2x1)
48 {
49  const double dat_H[] = { 0.2, -0.3 };
50  CMatrixDouble H(1,2, dat_H);
51 
52  const double dat_C[] = { 0.8, -0.1, -0.1, 0.8 };
53  CMatrixDouble C(2,2, dat_C);
54 
55  const double r = H.multiply_HCHt_scalar(C);
56 
57  const double r2 = (H*C*H.transpose()).eval()(0,0);
58  CHECK_AND_RET_ERROR(fabs(r-r2)>1e-4, "Error in HCHt_scalar: 1x2 * 2x2 * 2x1")
59 }
60 
61 TEST(Matrices,det_2x2_dyn)
62 {
63  const double dat_A[] = { 0.8, -0.3, -0.7, 0.1 };
64  CMatrixDouble A(2,2, dat_A);
65 
66  const double d = A.det();
67  const double d_R =-0.13;
68 
69  CHECK_AND_RET_ERROR(fabs(d-d_R) >1e-4, "Error in Determinant: 2x2 dyn")
70 }
71 
72 TEST(Matrices,det_2x2_fix)
73 {
74  const double dat_A[] = { 0.8, -0.3, -0.7, 0.1 };
76 
77  const double d = A.det();
78  const double d_R =-0.13;
79 
80  CHECK_AND_RET_ERROR(fabs(d-d_R) >1e-4, "Error in Determinant: 2x2 fix")
81 }
82 
83 TEST(Matrices,det_3x3_dyn)
84 {
85  const double dat_A[] = { -3.3304e-01, -2.0585e-01, 6.2026e-05, 1.4631e+00,6.0985e-01,2.3746e+00, -3.6451e-01, 4.8169e-01, -8.4419e-01};
86  CMatrixDouble A(3,3, dat_A);
87  const double d = A.det();
88  const double d_R =0.476380435871666;
89  //cout << "d: " << d << endl;
90  CHECK_AND_RET_ERROR(fabs(d-d_R)>1e-4, "Error in Determinant: 3x3 dyn")
91 }
92 
93 TEST(Matrices,det_3x3_fix)
94 {
95  const double dat_A[] = { -3.3304e-01, -2.0585e-01, 6.2026e-05, 1.4631e+00,6.0985e-01,2.3746e+00, -3.6451e-01, 4.8169e-01, -8.4419e-01};
97  const double d = A.det();
98  const double d_R =0.476380435871666;
99  CHECK_AND_RET_ERROR(fabs(d-d_R) >1e-4, "Error in Determinant: 3x3 fix")
100 }
101 
102 TEST(Matrices,det_4x4_dyn)
103 {
104  const double dat_A[] = {0.773931,-0.336130,1.131764,0.385890,1.374906,-0.540629,-0.952902,0.659769,-0.387254,-1.557355,0.139683,-2.056635,-0.750078,-0.653811,0.872027,0.217554};
105  CMatrixDouble A(4,4,dat_A);
106  const double d = A.det();
107  const double d_R = -6.29527837425056;
108  CHECK_AND_RET_ERROR(fabs(d-d_R) >1e-4, "Error in Determinant: 4x4 dyn")
109 }
110 
111 TEST(Matrices,det_4x4_fix)
112 {
113  const double dat_A[] = {0.773931,-0.336130,1.131764,0.385890,1.374906,-0.540629,-0.952902,0.659769,-0.387254,-1.557355,0.139683,-2.056635,-0.750078,-0.653811,0.872027,0.217554};
115  const double d = A.det();
116  const double d_R = -6.29527837425056;
117  CHECK_AND_RET_ERROR(fabs(d-d_R) >1e-4, "Error in Determinant: 4x4 fix")
118 }
119 
120 TEST(Matrices,det_10x10_dyn)
121 {
122  const double dat_A[] = {1.2305462976,-0.2944257811,0.8176140437,-0.0487601371,0.4418235581,-0.0088466980,-1.4100223408,-0.6219629815,1.1089237266,-0.6450262619,-2.0862614547,0.2699762709,-0.0705918517,1.1763963161,-0.3461819597,-1.3013222580,-0.3310621595,-0.2595069675,-0.5188213591,1.2261476224,-1.1334297957,2.1452881319,1.7856021357,0.5406722888,0.5497545623,0.4282217402,-1.6175210256,-0.3522824764,0.2773929603,0.8507134453,0.4046854117,-2.1638696195,1.0044939778,0.9755939720,0.9640788301,0.5641138097,0.7382236207,-0.4422212587,0.8507041571,1.3764399072,0.3446492224,1.1681336612,-1.3440052449,1.0120691406,-0.0430604384,0.4823901171,0.0881769800,0.3984805283,-1.9988153178,0.9509748328,0.3202853059,1.9688559025,0.4020581289,-1.5558616735,-0.8753527614,0.1207830427,0.0457715031,-0.1557123759,-0.3161307172,-0.0759276933,-0.0417386037,1.2079564736,-2.5839030155,-0.7648863647,1.1541464803,0.2127569446,-1.4882083860,-0.7630836781,0.8550884427,-0.8440402465,-0.4903597050,-0.1457982930,0.5893448560,-0.2353784687,0.3474655757,2.5874616045,0.6608448038,-1.0105315509,-1.5276853710,-0.1400026815,-1.7630264416,2.4048579514,-0.3111046623,0.7463774799,-0.2800404492,-1.4175124130,-0.5708536580,-1.2085107661,0.8169107561,-1.1659481510,-0.1406355512,2.3507381980,2.6346742737,-1.1028788167,-0.0533115044,0.3752684649,-1.3799576309,-0.7274190037,1.1188847602,-0.6624231096};
123  CMatrixDouble A(10,10,dat_A);
124  const double d = A.det();
125  const double d_R = 330.498518199239;
126  CHECK_AND_RET_ERROR(fabs(d-d_R) >1e-4, "Error in Determinant: 10x10 dyn")
127 }
128 
129 TEST(Matrices,det_10x10_fix)
130 {
131  const double dat_A[] = {1.2305462976,-0.2944257811,0.8176140437,-0.0487601371,0.4418235581,-0.0088466980,-1.4100223408,-0.6219629815,1.1089237266,-0.6450262619,-2.0862614547,0.2699762709,-0.0705918517,1.1763963161,-0.3461819597,-1.3013222580,-0.3310621595,-0.2595069675,-0.5188213591,1.2261476224,-1.1334297957,2.1452881319,1.7856021357,0.5406722888,0.5497545623,0.4282217402,-1.6175210256,-0.3522824764,0.2773929603,0.8507134453,0.4046854117,-2.1638696195,1.0044939778,0.9755939720,0.9640788301,0.5641138097,0.7382236207,-0.4422212587,0.8507041571,1.3764399072,0.3446492224,1.1681336612,-1.3440052449,1.0120691406,-0.0430604384,0.4823901171,0.0881769800,0.3984805283,-1.9988153178,0.9509748328,0.3202853059,1.9688559025,0.4020581289,-1.5558616735,-0.8753527614,0.1207830427,0.0457715031,-0.1557123759,-0.3161307172,-0.0759276933,-0.0417386037,1.2079564736,-2.5839030155,-0.7648863647,1.1541464803,0.2127569446,-1.4882083860,-0.7630836781,0.8550884427,-0.8440402465,-0.4903597050,-0.1457982930,0.5893448560,-0.2353784687,0.3474655757,2.5874616045,0.6608448038,-1.0105315509,-1.5276853710,-0.1400026815,-1.7630264416,2.4048579514,-0.3111046623,0.7463774799,-0.2800404492,-1.4175124130,-0.5708536580,-1.2085107661,0.8169107561,-1.1659481510,-0.1406355512,2.3507381980,2.6346742737,-1.1028788167,-0.0533115044,0.3752684649,-1.3799576309,-0.7274190037,1.1188847602,-0.6624231096};
133  const double d = A.det();
134  const double d_R = 330.498518199239;
135  CHECK_AND_RET_ERROR(fabs(d-d_R) >1e-4, "Error in Determinant: 10x10 fix")
136 }
137 
138 TEST(Matrices,chol_2x2_dyn)
139 {
140  const double dat_A[] = {1.0727710178,0.6393375593,0.6393375593,0.8262219720};
141  CMatrixDouble A(2,2, dat_A);
142  CMatrixDouble C;
143  A.chol(C);
144  const double dat_CHOL[] = {1.0357465992,0.6172721781,0.0000000000,0.6672308672};
145  CMatrixDouble CHOL(2,2,dat_CHOL);
146  CHECK_AND_RET_ERROR( (CHOL-C).array().abs().sum() >1e-4, "Error in Choleski, 2x2 dyn")
147 }
148 
149 TEST(Matrices,chol_2x2_fix)
150 {
151  const double dat_A[] = {1.0727710178,0.6393375593,0.6393375593,0.8262219720};
153  CMatrixDouble22 C;
154  A.chol(C);
155  const double dat_CHOL[] = {1.0357465992,0.6172721781,0.0000000000,0.6672308672};
156  CMatrixDouble CHOL(2,2,dat_CHOL);
157  CHECK_AND_RET_ERROR( (CHOL-C).array().abs().sum() >1e-4, "Error in Choleski, 2x2 fix")
158 }
159 
160 
161 TEST(Matrices,chol_3x3_dyn)
162 {
163  const double dat_A[] = {0.515479426556448,0.832723636299236,0.249691538245735,0.832723636299236,1.401081397506934,0.385539356127255,0.249691538245735,0.385539356127255,0.128633962591437};
164  CMatrixDouble A(3,3, dat_A);
165  CMatrixDouble C;
166  A.chol(C);
167 
168  const double dat_CHOL[] = {0.717968959326549,1.159832365288224,0.347774837619643,0.000000000000000,0.236368952988455,-0.075395504153773,0.000000000000000,0.000000000000000,0.044745311077990};
169  CMatrixDouble CHOL(3,3,dat_CHOL);
170  CHECK_AND_RET_ERROR( (CHOL-C).array().abs().sum() >1e-4, "Error in Choleski, 3x3 dyn")
171 }
172 
173 TEST(Matrices,chol_3x3_fix)
174 {
175  const double dat_A[] = {0.515479426556448,0.832723636299236,0.249691538245735,0.832723636299236,1.401081397506934,0.385539356127255,0.249691538245735,0.385539356127255,0.128633962591437};
177  CMatrixDouble33 C;
178  A.chol(C);
179 
180  const double dat_CHOL[] = {0.717968959326549,1.159832365288224,0.347774837619643,0.000000000000000,0.236368952988455,-0.075395504153773,0.000000000000000,0.000000000000000,0.044745311077990};
181  CMatrixDouble33 CHOL(dat_CHOL);
182  CHECK_AND_RET_ERROR( (CHOL-C).array().abs().sum() >1e-4, "Error in Choleski, 3x3 fix")
183 }
184 
185 
186 TEST(Matrices,chol_10x10_dyn)
187 {
188  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};
189  CMatrixDouble A(10,10, dat_A);
190  CMatrixDouble C;
191  A.chol(C);
192 
193  const double dat_CHOL[] = {1.7016365163,1.3541042851,1.1167121124,1.0574031810,1.0846145491,1.7413999087,1.1382316607,1.2922221137,1.2075202560,1.5077061845,0.0000000000,1.4167191047,0.4722017314,1.2701334167,0.7093566960,0.7851196867,-0.0380051491,0.9582353452,0.3530862859,0.6441080558,0.0000000000,0.0000000000,1.0120209201,-0.0943393725,-0.0865342379,-0.0136183214,-0.1557357390,0.3976620401,-0.1218419159,0.1952860421,0.0000000000,0.0000000000,0.0000000000,0.6183654266,-0.6113744707,-0.1944977093,0.1127886805,-0.2752173394,-0.1741275611,0.0847171764,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.8668818973,0.0234194680,0.3011475111,-0.0272963639,-0.1417917925,0.8000162775,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.6924364129,0.2527445784,0.3919505633,0.3715689962,-0.0817608778,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.6358623279,0.4364121485,0.4859857603,-0.0313828244,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.5408375843,-0.1995475524,0.6258606925,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.2213262214,-0.2367037013,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.2838575216};
194  CMatrixDouble CHOL(10,10,dat_CHOL);
195  CHECK_AND_RET_ERROR( (CHOL-C).array().abs().sum() >1e-4, "Error in Choleski, 10x10 dyn")
196 }
197 
198 TEST(Matrices,chol_10x10_fix)
199 {
200  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};
202  CMatrixDouble C;
203  A.chol(C);
204 
205  const double dat_CHOL[] = {1.7016365163,1.3541042851,1.1167121124,1.0574031810,1.0846145491,1.7413999087,1.1382316607,1.2922221137,1.2075202560,1.5077061845,0.0000000000,1.4167191047,0.4722017314,1.2701334167,0.7093566960,0.7851196867,-0.0380051491,0.9582353452,0.3530862859,0.6441080558,0.0000000000,0.0000000000,1.0120209201,-0.0943393725,-0.0865342379,-0.0136183214,-0.1557357390,0.3976620401,-0.1218419159,0.1952860421,0.0000000000,0.0000000000,0.0000000000,0.6183654266,-0.6113744707,-0.1944977093,0.1127886805,-0.2752173394,-0.1741275611,0.0847171764,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.8668818973,0.0234194680,0.3011475111,-0.0272963639,-0.1417917925,0.8000162775,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.6924364129,0.2527445784,0.3919505633,0.3715689962,-0.0817608778,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.6358623279,0.4364121485,0.4859857603,-0.0313828244,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.5408375843,-0.1995475524,0.6258606925,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.2213262214,-0.2367037013,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.0000000000,0.2838575216};
206  CMatrixDouble CHOL(10,10,dat_CHOL);
207  CHECK_AND_RET_ERROR( (CHOL-C).array().abs().sum() >1e-4, "Error in Choleski, 10x10 fix")
208 }
209 
210 
A namespace of pseudo-random numbers genrators of diferent distributions.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
const double dat_A[]
STL namespace.
A numeric matrix of compile-time fixed size.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
TEST(Matrices, HCHt_3x2_2x2_2x3)
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
A set of utility objects for metaprogramming with STL algorithms.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
const float R
#define CHECK_AND_RET_ERROR(_COND_, _MSG_)



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020