testErrorPropagator.cc
Go to the documentation of this file.
1 
9 #include <tst/Verify.h>
10 
11 #include <utl/AugerUnits.h>
12 #include <utl/AugerException.h>
13 
14 #include <iostream>
15 #include <cmath>
16 #include <vector>
17 
18 #include <cppunit/extensions/HelperMacros.h>
19 #include <boost/tuple/tuple.hpp>
20 
21 #include <utl/NumericalErrorPropagator.h>
22 #include <utl/CovarianceMatrix.h>
23 #include <utl/AugerException.h>
24 
25 using namespace std;
26 using namespace utl;
27 using namespace tst;
28 
29 #define ASSERT_EQUAL(x, y) CPPUNIT_ASSERT(x == y)
30 
34 class testErrorPropagator : public CppUnit::TestFixture {
35 
36  CPPUNIT_TEST_SUITE(testErrorPropagator);
37  CPPUNIT_TEST(TestCovarianceMatrix);
38  CPPUNIT_TEST(TestErrorPropagatorFaster);
39  CPPUNIT_TEST(TestErrorPropagatorBetter);
40  CPPUNIT_TEST_SUITE_END();
41 
42 private:
43 
44  template<class Stream>
45  void
46  OutputMatrix(Stream& s, const CovarianceMatrix& c)
47  {
48  for (unsigned int i=0;i<c.GetExtent();++i)
49  for (unsigned int j=0;j<c.GetExtent();++j)
50  s << c(i,j) << (j<c.GetExtent()-1 ? " " : "\n");
51  }
52 
53  // output 3, input 4
55  {
56  public:
57  double m[3][4];
58 
59  void
60  Transform(std::vector<double>& out, const std::vector<double>& inp)
61  const
62  {
63  if (out.size()!=3) out = std::vector<double>(3);
64  out[0] = m[0][0]*inp[0]+m[0][1]*inp[1]+m[0][2]*inp[2]+m[0][3]*inp[3] + 10.;
65  out[1] = m[1][0]*inp[0]+m[1][1]*inp[1]+m[1][2]*inp[2]+m[1][3]*inp[3] + 20.;
66  out[2] = m[2][0]*inp[0]+m[2][1]*inp[1]+m[2][2]*inp[2]+m[2][3]*inp[3] + 30.;
67  }
68 
69  };
70 
71 public:
72 
73  void
75  {
76  { // test with nPar = 2
78  c(0,0) = 0.1;
79  c(0,1) = 0.2;
80  c(1,1) = 0.3;
81 
82  ostringstream s;
83  OutputMatrix(s,c);
84 
85  const string truth =
86  "0.1 0.2\n"
87  "0.2 0.3\n";
88 
89  ASSERT_EQUAL(s.str(),truth);
90  }
91 
92  { // test with nPar = 6
94  c[0] = 0;
95  c[1] = 1;
96  c[2] = 2;
97  c[3] = 3;
98  c[4] = 4;
99  c[5] = 5;
100  c(0,1) = 0.1;
101  c(0,2) = 0.2;
102  c(0,3) = 0.3;
103  c(0,4) = 0.4;
104  c(0,5) = 0.5;
105  c(1,2) = 1.2;
106  c(1,3) = 1.3;
107  c(1,4) = 1.4;
108  c(1,5) = 1.5;
109  c(2,3) = 2.3;
110  c(2,4) = 2.4;
111  c(2,5) = 2.5;
112  c(3,4) = 3.4;
113  c(3,5) = 3.5;
114  c(4,5) = 4.5;
115 
116  ostringstream s;
117  OutputMatrix(s,c);
118 
119  const string truth =
120  "0 0.1 0.2 0.3 0.4 0.5\n"
121  "0.1 1 1.2 1.3 1.4 1.5\n"
122  "0.2 1.2 2 2.3 2.4 2.5\n"
123  "0.3 1.3 2.3 3 3.4 3.5\n"
124  "0.4 1.4 2.4 3.4 4 4.5\n"
125  "0.5 1.5 2.5 3.5 4.5 5\n";
126 
127  ASSERT_EQUAL(s.str(),truth);
128  }
129  }
130 
131  void
133  {
134  TestErrorPropagator(NumericalErrorPropagator::eFaster);
135  }
136 
137  void
139  {
140  TestErrorPropagator(NumericalErrorPropagator::eBetter);
141  }
142 
143  void
145  {
146  vector<double> iVec(4);
147  CovarianceMatrix iCov(4);
148 
149  iVec[0] = 1.23;
150  iVec[1] = 4.56;
151  iVec[2] = 7.89;
152  iVec[3] = 2.01;
153 
154  iCov[0] = 0.321;
155  iCov[1] = 0.654;
156  iCov[2] = 0.987;
157  iCov[3] = 0.234;
158  iCov(0,1) = 0.11;
159  iCov(0,2) = -0.12;
160  iCov(0,3) = -0.13;
161  iCov(1,2) = 0.22;
162  iCov(1,3) = -0.23;
163  iCov(2,3) = -0.34;
164 
165  TestFunctor fcn; // does y = m*x + b
166  fcn.m[0][0] = 1.1;
167  fcn.m[0][1] = -2.2;
168  fcn.m[0][2] = 3.3;
169  fcn.m[0][3] = 4.4;
170  fcn.m[1][0] = -4.5;
171  fcn.m[1][1] = -5.6;
172  fcn.m[1][2] = -6.7;
173  fcn.m[1][3] = 7.8;
174  fcn.m[2][0] = 0.1;
175  fcn.m[2][1] = 0.2;
176  fcn.m[2][2] = -0.3;
177  fcn.m[2][3] = 0.5;
178 
179  vector<double> oVec(3);
180  CovarianceMatrix oCov(3);
181  fcn(oVec,oCov,iVec,iCov,quality);
182 
183  // V_out = J V_in J^T, mit J = Jacobi matrix, here for TestFunctor J = m
184 
185  double tmp[3][4];
186  for (int i=0;i<3;++i)
187  for (int j=0;j<4;++j)
188  {
189  tmp[i][j] = 0.0;
190  for (int k=0;k<4;++k)
191  tmp[i][j] += fcn.m[i][k]*iCov(k,j);
192  }
193 
194  ostringstream s1;
195  for (int i=0;i<3;++i)
196  for (int j=0;j<3;++j)
197  {
198  double vout = 0.0;
199  for (int k=0;k<4;++k)
200  vout += tmp[i][k]*fcn.m[j][k]; // fcn.m[j][k] is transposed of fcn.m[k][j]
201  s1 << vout << (j<2 ? " " : "\n");
202  }
203 
204  ostringstream s2;
205  for (int i=0;i<3;++i)
206  for (int j=0;j<3;++j)
207  {
208  s2 << oCov(i,j) << (j<2 ? " " : "\n");
209  }
210 
211  ASSERT_EQUAL(s1.str(),s2.str());
212  }
213 
214 };
215 
216 
vector< t2list > out
output of the algorithm: a list of clusters
Definition: XbAlgo.cc:32
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
constexpr double s
Definition: AugerUnits.h:163
void OutputMatrix(Stream &s, const CovarianceMatrix &c)
#define ASSERT_EQUAL(x, y)
void TestErrorPropagator(NumericalErrorPropagator::Quality quality)
constexpr double m
Definition: AugerUnits.h:121

, generated on Tue Sep 26 2023.