CbmRoot
CbmLitTestMatrixMath.cxx
Go to the documentation of this file.
3 
4 #include "TMatrixT.h"
5 #include "TMatrixTSym.h"
6 #include "TRandom.h"
7 
8 #include <cmath>
9 #include <iostream>
10 #include <sstream>
11 #include <vector>
12 
13 
15 
17 
19  std::cout << "Start matrix math test..." << std::endl;
20 
21  TestInvSym15(1);
22  TestInvSym15(2);
23  TestInvSym15(3);
24 
25  TestMult15On5(1);
26  TestMult15On5(2);
27  TestMult15On5(3);
28 
29  std::cout << "Finish matrix math test..." << std::endl;
30 }
31 
33  std::cout << "Test InvSym15 #" << testId << std::endl;
34  std::vector<litfloat> input1(15);
35  for (int i = 0; i < 15; i++) {
36  input1[i] = gRandom->Rndm();
37  }
38 
39  std::cout << "Input array: " << VectorToString(input1);
40 
41  std::vector<litfloat> a1(input1);
42  InvSym15(a1);
43  std::cout << "Output InvSym15: " << VectorToString(a1);
44 
45  litfloat rd1[25], rd2[15];
46  Convert15To25(&input1[0], rd1);
47  TMatrixTSym<litfloat> r1(5, rd1);
48  r1.InvertFast();
49  Convert25To15(r1.GetMatrixArray(), rd2);
50  std::cout << "Output ROOT: " << ArrayToString(rd2, 15);
51 
52  // Compare output arrays
53  bool testPassed = true;
54  for (int i = 0; i < 15; i++) {
55  if (std::abs(a1[i] - rd2[i]) > fEpsilon) {
56  testPassed = false;
57  break;
58  }
59  }
60  if (testPassed) {
61  std::cout << "Test InvSym15 #" << testId << " PASSED" << std::endl;
62  } else {
63  std::cout << "Test InvSym15 #" << testId << " !!!NOT PASSED!!!"
64  << std::endl;
65  }
66 }
67 
69  std::cout << "Test Mult15On5 #" << testId << std::endl;
70  std::vector<litfloat> input1(15);
71  for (int i = 0; i < 15; i++) {
72  input1[i] = gRandom->Rndm();
73  }
74  std::vector<litfloat> input2(5);
75  for (int i = 0; i < 5; i++) {
76  input2[i] = gRandom->Rndm();
77  }
78 
79  std::cout << "Input array1: " << VectorToString(input1);
80  std::cout << "Input array2: " << VectorToString(input2);
81 
82  std::vector<litfloat> a1(5);
83  Mult15On5(input1, input2, a1);
84  std::cout << "Output Mult15On5: " << VectorToString(a1);
85 
86  litfloat rd1[25], rd2[15];
87  Convert15To25(&input1[0], rd1);
88  TMatrixTSym<litfloat> r1(5, rd1);
89  TMatrixT<litfloat> v1(5, 1, &input2[0]);
90  TMatrixT<litfloat> u1(5, 1);
91  u1 = r1 * v1;
92 
93  litfloat* ro = u1.GetMatrixArray();
94 
95  std::cout << "Output ROOT: " << ArrayToString(ro, 5);
96 
97  // Compare output arrays
98  bool testPassed = true;
99  for (int i = 0; i < 5; i++) {
100  if (std::abs(a1[i] - ro[i]) > fEpsilon) {
101  testPassed = false;
102  break;
103  }
104  }
105  if (testPassed) {
106  std::cout << "Test Mult15On5 #" << testId << " PASSED" << std::endl;
107  } else {
108  std::cout << "Test Mult15On5 #" << testId << " !!!NOT PASSED!!!"
109  << std::endl;
110  }
111 }
112 
114  a25[0] = a15[0];
115  a25[1] = a15[1];
116  a25[2] = a15[2];
117  a25[3] = a15[3];
118  a25[4] = a15[4];
119  a25[5] = a15[1];
120  a25[6] = a15[5];
121  a25[7] = a15[6];
122  a25[8] = a15[7];
123  a25[9] = a15[8];
124  a25[10] = a15[2];
125  a25[11] = a15[6];
126  a25[12] = a15[9];
127  a25[13] = a15[10];
128  a25[14] = a15[11];
129  a25[15] = a15[3];
130  a25[16] = a15[7];
131  a25[17] = a15[10];
132  a25[18] = a15[12];
133  a25[19] = a15[13];
134  a25[20] = a15[4];
135  a25[21] = a15[8];
136  a25[22] = a15[11];
137  a25[23] = a15[13];
138  a25[24] = a15[14];
139 }
140 
142  a15[0] = a25[0];
143  a15[1] = a25[1];
144  a15[2] = a25[2];
145  a15[3] = a25[3];
146  a15[4] = a25[4];
147  a15[5] = a25[6];
148  a15[6] = a25[7];
149  a15[7] = a25[8];
150  a15[8] = a25[9];
151  a15[9] = a25[12];
152  a15[10] = a25[13];
153  a15[11] = a25[14];
154  a15[12] = a25[18];
155  a15[13] = a25[19];
156  a15[14] = a25[24];
157 }
158 
159 std::string
160 CbmLitTestMatrixMath::VectorToString(const std::vector<litfloat>& a) {
161  std::stringstream out;
162  for (int i = 0; i < a.size(); i++) {
163  out << a[i] << " ";
164  }
165  out << std::endl;
166  return out.str();
167 }
168 
169 std::string CbmLitTestMatrixMath::ArrayToString(const litfloat* a, int n) {
170  std::stringstream out;
171  for (int i = 0; i < n; i++) {
172  out << a[i] << " ";
173  }
174  out << std::endl;
175  return out.str();
176 }
litfloat
double litfloat
Definition: CbmLitFloat.h:15
CbmLitTestMatrixMath::TestMult15On5
void TestMult15On5(int testId)
Definition: CbmLitTestMatrixMath.cxx:68
CbmLitTestMatrixMath::~CbmLitTestMatrixMath
virtual ~CbmLitTestMatrixMath()
Definition: CbmLitTestMatrixMath.cxx:16
CbmLitTestMatrixMath::RunTest
void RunTest()
Definition: CbmLitTestMatrixMath.cxx:18
CbmLitTestMatrixMath::TestInvSym15
void TestInvSym15(int testId)
Definition: CbmLitTestMatrixMath.cxx:32
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
CbmLitTestMatrixMath.h
CbmLitTestMatrixMath::Convert25To15
void Convert25To15(const litfloat *a25, litfloat *a15)
Definition: CbmLitTestMatrixMath.cxx:141
InvSym15
bool InvSym15(std::vector< litfloat > &a)
Definition: CbmLitMatrixMath.cxx:41
CbmLitTestMatrixMath::Convert15To25
void Convert15To25(const litfloat *a15, litfloat *a25)
Definition: CbmLitTestMatrixMath.cxx:113
CbmLitTestMatrixMath::VectorToString
std::string VectorToString(const std::vector< litfloat > &a)
Definition: CbmLitTestMatrixMath.cxx:160
Mult15On5
bool Mult15On5(const std::vector< litfloat > &a, const std::vector< litfloat > &b, std::vector< litfloat > &c)
Definition: CbmLitMatrixMath.cxx:351
CbmLitMatrixMath.h
CbmLitTestMatrixMath::CbmLitTestMatrixMath
CbmLitTestMatrixMath()
Definition: CbmLitTestMatrixMath.cxx:14
CbmLitTestMatrixMath::ArrayToString
std::string ArrayToString(const litfloat *a, int n)
Definition: CbmLitTestMatrixMath.cxx:169
CbmLitTestMatrixMath::fEpsilon
litfloat fEpsilon
Definition: CbmLitTestMatrixMath.h:30