SmallMatrixUnitTest.cpp File Reference
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "Math/SmallMatrix.h"
#include "Math/SmallVector.h"
#include "Logger.h"

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int argc  ,
char **  argv 
)
Todo:
figure out a way to test a LU factorisation
33 {
34  //constructors
35  SmallVector<2> vec0{0., 1.}, vec1{2., 3.}, vec2{4., 5.}, vec3{6., 7.};
37  SmallMatrix<2, 2> A22, count0({vec0, vec1}), count1({vec2, vec3}), copy(count0);
40  SmallMatrix<3, 3> destroy(1);
41  logger.assert_always(destroy.getNumberOfRows() == 3, "Rows in a matrix");
42  logger.assert_always(destroy.getNCols() == 3, "Columns in a matrix");
43  logger.assert_always(destroy.size() == 9, "Size of a matrix");
44  for(std::size_t i = 0; i < destroy.size(); ++i)
45  {
46  logger.assert_always(std::abs(destroy[i] - 1.) < 1e-12, "Entry of a matrix");
47  }
48  for(std::size_t i = 0; i < destroy.getNumberOfRows(); ++i)
49  {
50  for(std::size_t j = 0; j < destroy.getNCols(); ++j)
51  {
52  logger.assert_always(std::abs(destroy(i, j) - 1.) < 1e-12, "Entry of a matrix");
53  }
54  }
55  SmallMatrix<3, 3> moved(std::move(destroy));
56  logger.assert_always(moved.getNumberOfRows() == 3, "Rows in a matrix");
57  logger.assert_always(moved.getNCols() == 3, "Columns in a matrix");
58  logger.assert_always(moved.size() == 9, "Size of a matrix");
59  for(std::size_t i = 0; i < moved.size(); ++i)
60  {
61  logger.assert_always(std::abs(moved[i] - 1.) < 1e-12, "Entry of a matrix");
62  }
63  for(std::size_t i = 0; i < moved.getNumberOfRows(); ++i)
64  {
65  for(std::size_t j = 0; j < moved.getNCols(); ++j)
66  {
67  logger.assert_always(std::abs(moved(i, j) - 1.) < 1e-12, "Entry of a matrix");
68  }
69  }
70  logger.assert_always(A0.getNumberOfRows() == 3, "Rows in a matrix");
71  logger.assert_always(A0.getNCols() == 3, "Columns in a matrix");
72  logger.assert_always(A0.size() == 9, "Size of a matrix");
73  logger.assert_always(A22.getNumberOfRows() == 2, "Rows in a matrix");
74  logger.assert_always(A22.getNCols() == 2, "Columns in a matrix");
75  logger.assert_always(A22.size() == 4, "Size of a matrix");
76  logger.assert_always(A23.getNumberOfRows() == 2, "Rows in a matrix");
77  logger.assert_always(A23.getNCols() == 3, "Columns in a matrix");
78  logger.assert_always(A23.size() == 6, "Size of a matrix");
79  logger.assert_always(A32.getNumberOfRows() == 3, "Rows in a matrix");
80  logger.assert_always(A32.getNCols() == 2, "Columns in a matrix");
81  logger.assert_always(A32.size() == 6, "Size of a matrix");
82  logger.assert_always(count0.getNumberOfRows() == 2, "Rows in a matrix");
83  logger.assert_always(count0.getNCols() == 2, "Columns in a matrix");
84  logger.assert_always(count0.size() == 4, "Size of a matrix");
85  for(std::size_t i = 0; i < count0.size(); ++i)
86  {
87  logger.assert_always(std::abs(count0[i] - i) < 1e-12, "Entry of a matrix");
88  }
89  logger.assert_always(std::abs(count0(0, 0) - 0.) < 1e-12, "Entry of a matrix");
90  logger.assert_always(std::abs(count0(1, 0) - 1.) < 1e-12, "Entry of a matrix");
91  logger.assert_always(std::abs(count0(0, 1) - 2.) < 1e-12, "Entry of a matrix");
92  logger.assert_always(std::abs(count0(1, 1) - 3.) < 1e-12, "Entry of a matrix");
93  logger.assert_always(count1.getNumberOfRows() == 2, "Rows in a matrix");
94  logger.assert_always(count1.getNCols() == 2, "Columns in a matrix");
95  logger.assert_always(count1.size() == 4, "Size of a matrix");
96  for(std::size_t i = 0; i < count1.size(); ++i)
97  {
98  logger.assert_always(std::abs(count1[i] - 4. - i) < 1e-12, "Entry of a matrix");
99  }
100  logger.assert_always(copy.getNumberOfRows() == 2, "Rows in a matrix");
101  logger.assert_always(copy.getNCols() == 2, "Columns in a matrix");
102  logger.assert_always(copy.size() == 4, "Size of a matrix");
103  for(std::size_t i = 0; i < copy.size(); ++i)
104  {
105  logger.assert_always(std::abs(copy[i] - i) < 1e-12, "Entry of a matrix");
106  }
107  A23(0, 0) = 0.0;
108  A23(1, 0) = 0.1;
109  A23(0, 1) = 0.2;
110  A23(1, 1) = 0.3;
111  A23(0, 2) = 0.4;
112  A23(1, 2) = 0.5;
113  A32[0] = 0.0;
114  A32[1] = 0.1;
115  A32[2] = 0.2;
116  A32[3] = 0.3;
117  A32[4] = 0.4;
118  A32[5] = 0.5;
119 
120  //out-of-place operators
121  logger.assert_always(std::abs((count0*count1)(0, 0) - 10.) < 1e-12, "multiply");
122  logger.assert_always(std::abs((count0*count1)(1, 0) - 19.) < 1e-12, "multiply");
123  logger.assert_always(std::abs((count0*count1)(0, 1) - 14.) < 1e-12, "multiply");
124  logger.assert_always(std::abs((count0*count1)(1, 1) - 27.) < 1e-12, "multiply");
125  logger.assert_always(std::abs((count1*count0)(0, 0) - 6.) < 1e-12, "multiply");
126  logger.assert_always(std::abs((count1*count0)(1, 0) - 7.) < 1e-12, "multiply");
127  logger.assert_always(std::abs((count1*count0)(0, 1) - 26.) < 1e-12, "multiply");
128  logger.assert_always(std::abs((count1*count0)(1, 1) - 31.) < 1e-12, "multiply");
129  logger.assert_always(std::abs((count0*A23)(0, 0) - .2) < 1e-12, "multiply");
130  logger.assert_always(std::abs((count0*A23)(1, 0) - .3) < 1e-12, "multiply");
131  logger.assert_always(std::abs((count0*A23)(0, 1) - .6) < 1e-12, "multiply");
132  logger.assert_always(std::abs((count0*A23)(1, 1) - 1.1) < 1e-12, "multiply");
133  logger.assert_always(std::abs((count0*A23)(0, 2) - 1.) < 1e-12, "multiply");
134  logger.assert_always(std::abs((count0*A23)(1, 2) - 1.9) < 1e-12, "multiply");
135  logger.assert_always(std::abs((A32*count0)(0, 0) - .3) < 1e-12, "multiply");
136  logger.assert_always(std::abs((A32*count0)(1, 0) - .4) < 1e-12, "multiply");
137  logger.assert_always(std::abs((A32*count0)(2, 0) - .5) < 1e-12, "multiply");
138  logger.assert_always(std::abs((A32*count0)(0, 1) - .9) < 1e-12, "multiply");
139  logger.assert_always(std::abs((A32*count0)(1, 1) - 1.4) < 1e-12, "multiply");
140  logger.assert_always(std::abs((A32*count0)(2, 1) - 1.9) < 1e-12, "multiply");
141  logger.assert_always(std::abs((A32*A23)(0, 0) - .03) < 1e-12, "multiply");
142  logger.assert_always(std::abs((A32*A23)(1, 0) - .04) < 1e-12, "multiply");
143  logger.assert_always(std::abs((A32*A23)(2, 0) - .05) < 1e-12, "multiply");
144  logger.assert_always(std::abs((A32*A23)(0, 1) - .09) < 1e-12, "multiply");
145  logger.assert_always(std::abs((A32*A23)(1, 1) - .14) < 1e-12, "multiply");
146  logger.assert_always(std::abs((A32*A23)(2, 1) - .19) < 1e-12, "multiply");
147  logger.assert_always(std::abs((A32*A23)(0, 2) - .15) < 1e-12, "multiply");
148  logger.assert_always(std::abs((A32*A23)(1, 2) - .24) < 1e-12, "multiply");
149  logger.assert_always(std::abs((A32*A23)(2, 2) - .33) < 1e-12, "multiply");
150  logger.assert_always(std::abs((A23*A32)(0, 0) - .1) < 1e-12, "multiply");
151  logger.assert_always(std::abs((A23*A32)(1, 0) - .13) < 1e-12, "multiply");
152  logger.assert_always(std::abs((A23*A32)(0, 1) - .28) < 1e-12, "multiply");
153  logger.assert_always(std::abs((A23*A32)(1, 1) - .40) < 1e-12, "multiply");
154  logger.assert_always(std::abs((vec1*count0)*vec1 - 45) < 1e-12, "multiply");
155  logger.assert_always(std::abs(vec1*(count0*vec1) - 45) < 1e-12, "multiply");
156  SmallVector<3> size3 = {{3., 4., 5.}};
157  logger.assert_always(std::abs(size3*(A32*vec0) - 5) < 1e-12, "multiply");
158  logger.assert_always(std::abs((size3*A32)*vec0 - 5) < 1e-12, "multiply");
159  logger.assert_always(std::abs((2*count0*2)(0, 0) - 0.) < 1e-12, "multiply");
160  logger.assert_always(std::abs((2*count0*2)(1, 0) - 4.) < 1e-12, "multiply");
161  logger.assert_always(std::abs((2*count0*2)(0, 1) - 8.) < 1e-12, "multiply");
162  logger.assert_always(std::abs((2*count0*2)(1, 1) - 12.) < 1e-12, "multiply");
163  logger.assert_always(std::abs((count0/2.)(0, 0) - 0.) < 1e-12, "divide");
164  logger.assert_always(std::abs((count0/2.)(1, 0) - .5) < 1e-12, "divide");
165  logger.assert_always(std::abs((count0/2.)(0, 1) - 1.) < 1e-12, "divide");
166  logger.assert_always(std::abs((count0/2.)(1, 1) - 1.5) < 1e-12, "divide");
167  logger.assert_always(std::abs((count0+count1)(0, 0) - 4.) < 1e-12, "add");
168  logger.assert_always(std::abs((count0+count1)(1, 0) - 6.) < 1e-12, "add");
169  logger.assert_always(std::abs((count0+count1)(0, 1) - 8.) < 1e-12, "add");
170  logger.assert_always(std::abs((count0+count1)(1, 1) - 10.) < 1e-12, "add");
171  logger.assert_always(std::abs((count1+count0)(0, 0) - 4.) < 1e-12, "add");
172  logger.assert_always(std::abs((count1+count0)(1, 0) - 6.) < 1e-12, "add");
173  logger.assert_always(std::abs((count1+count0)(0, 1) - 8.) < 1e-12, "add");
174  logger.assert_always(std::abs((count1+count0)(1, 1) - 10.) < 1e-12, "add");
175  logger.assert_always(std::abs((count0-count1)(0, 0) + 4.) < 1e-12, "subtract");
176  logger.assert_always(std::abs((count0-count1)(1, 0) + 4.) < 1e-12, "subtract");
177  logger.assert_always(std::abs((count0-count1)(0, 1) + 4.) < 1e-12, "subtract");
178  logger.assert_always(std::abs((count0-count1)(1, 1) + 4.) < 1e-12, "subtract");
179  logger.assert_always(std::abs((count1-count0)(0, 0) - 4.) < 1e-12, "subtract");
180  logger.assert_always(std::abs((count1-count0)(1, 0) - 4.) < 1e-12, "subtract");
181  logger.assert_always(std::abs((count1-count0)(0, 1) - 4.) < 1e-12, "subtract");
182  logger.assert_always(std::abs((count1-count0)(1, 1) - 4.) < 1e-12, "subtract");
183 
184  //assignent operators
185  SmallMatrix<2, 2> extra = copy;
186  logger.assert_always(extra.getNumberOfRows() == 2, "Rows in a matrix");
187  logger.assert_always(extra.getNCols() == 2, "Columns in a matrix");
188  logger.assert_always(extra.size() == 4, "Size of a matrix");
189  for(std::size_t i = 0; i < extra.size(); ++i)
190  {
191  logger.assert_always(std::abs(extra[i] - i) < 1e-12, "Entry of a matrix");
192  }
193  copy = count1;
194  logger.assert_always(copy.getNumberOfRows() == 2, "Rows in a matrix");
195  logger.assert_always(copy.getNCols() == 2, "Columns in a matrix");
196  logger.assert_always(copy.size() == 4, "Size of a matrix");
197  for(std::size_t i = 0; i < copy.size(); ++i)
198  {
199  logger.assert_always(std::abs(copy[i] - i - 4.) < 1e-12, "Entry of a matrix");
200  }
201  count0*=count1;
202  logger.assert_always(count0.getNumberOfRows() == 2, "Rows in a matrix");
203  logger.assert_always(count0.getNCols() == 2, "Columns in a matrix");
204  logger.assert_always(count0.size() == 4, "Size of a matrix");
205  logger.assert_always(std::abs((count0)(0, 0) - 10.) < 1e-12, "multiply");
206  logger.assert_always(std::abs((count0)(1, 0) - 19.) < 1e-12, "multiply");
207  logger.assert_always(std::abs((count0)(0, 1) - 14.) < 1e-12, "multiply");
208  logger.assert_always(std::abs((count0)(1, 1) - 27.) < 1e-12, "multiply");
209  count0*=4;
210  logger.assert_always(count0.getNumberOfRows() == 2, "Rows in a matrix");
211  logger.assert_always(count0.getNCols() == 2, "Columns in a matrix");
212  logger.assert_always(count0.size() == 4, "Size of a matrix");
213  logger.assert_always(std::abs((count0)(0, 0) - 40.) < 1e-12, "multiply");
214  logger.assert_always(std::abs((count0)(1, 0) - 76.) < 1e-12, "multiply");
215  logger.assert_always(std::abs((count0)(0, 1) - 56.) < 1e-12, "multiply");
216  logger.assert_always(std::abs((count0)(1, 1) - 108.) < 1e-12, "multiply");
217  count0/=2;
218  logger.assert_always(count0.getNumberOfRows() == 2, "Rows in a matrix");
219  logger.assert_always(count0.getNCols() == 2, "Columns in a matrix");
220  logger.assert_always(count0.size() == 4, "Size of a matrix");
221  logger.assert_always(std::abs((count0)(0, 0) - 20.) < 1e-12, "divide");
222  logger.assert_always(std::abs((count0)(1, 0) - 38.) < 1e-12, "divide");
223  logger.assert_always(std::abs((count0)(0, 1) - 28.) < 1e-12, "divide");
224  logger.assert_always(std::abs((count0)(1, 1) - 54.) < 1e-12, "divide");
225  copy+=extra;
226  logger.assert_always(copy.getNumberOfRows() == 2, "Rows in a matrix");
227  logger.assert_always(copy.getNCols() == 2, "Columns in a matrix");
228  logger.assert_always(copy.size() == 4, "Size of a matrix");
229  logger.assert_always(std::abs((copy)(0, 0) - 4.) < 1e-12, "add");
230  logger.assert_always(std::abs((copy)(1, 0) - 6.) < 1e-12, "add");
231  logger.assert_always(std::abs((copy)(0, 1) - 8.) < 1e-12, "add");
232  logger.assert_always(std::abs((copy)(1, 1) - 10.) < 1e-12, "add");
233  extra-=count0;
234  logger.assert_always(extra.getNumberOfRows() == 2, "Rows in a matrix");
235  logger.assert_always(extra.getNCols() == 2, "Columns in a matrix");
236  logger.assert_always(extra.size() == 4, "Size of a matrix");
237  logger.assert_always(std::abs((extra)(0, 0) + 20.) < 1e-12, "subtract");
238  logger.assert_always(std::abs((extra)(1, 0) + 37.) < 1e-12, "subtract");
239  logger.assert_always(std::abs((extra)(0, 1) + 26.) < 1e-12, "subtract");
240  logger.assert_always(std::abs((extra)(1, 1) + 51.) < 1e-12, "subtract");
241  extra.axpy(3., copy);
242  logger.assert_always(extra.getNumberOfRows() == 2, "Rows in a matrix");
243  logger.assert_always(extra.getNCols() == 2, "Columns in a matrix");
244  logger.assert_always(extra.size() == 4, "Size of a matrix");
245  logger.assert_always(std::abs((extra)(0, 0) + 8.) < 1e-12, "ax+y");
246  logger.assert_always(std::abs((extra)(1, 0) + 19.) < 1e-12, "ax+y");
247  logger.assert_always(std::abs((extra)(0, 1) + 2.) < 1e-12, "ax+y");
248  logger.assert_always(std::abs((extra)(1, 1) + 21.) < 1e-12, "ax+y");
249 
250  SmallVector<3> vec3D0{{0., 1., 2.}}, vec3D1{{3., 4., 5.}}, vec3D2{{0., -1., 2.}};
251  SmallVector<4> vec4D0{{0., 1., 2., 3.}}, vec4D1{{4., 5., 6., 7.}}, vec4D2{{0., -1., 2., -3.}}, vec4D3{{0., -1., -2., 3.}};
252 
253  logger.assert_always(SmallMatrix<3, 2>({vec3D0, vec3D1}).getColumn(0).size() == 3, "getColumn");
254  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getColumn(0)) - vec3D0)[0]) < 1e-12, "getColumn");
255  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getColumn(0)) - vec3D0)[1]) < 1e-12, "getColumn");
256  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getColumn(0)) - vec3D0)[2]) < 1e-12, "getColumn");
257  logger.assert_always(SmallMatrix<3, 2>({vec3D0, vec3D1}).getColumn(1).size() == 3, "getColumn");
258  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getColumn(1)) - vec3D1)[0]) < 1e-12, "getColumn");
259  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getColumn(1)) - vec3D1)[1]) < 1e-12, "getColumn");
260  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getColumn(1)) - vec3D1)[2]) < 1e-12, "getColumn");
261  logger.assert_always(SmallMatrix<3, 2>({vec3D0, vec3D1}).getRow(0).size() == 2, "getRow");
262  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getRow(0)))[0] - 0.) < 1e-12, "getRow");
263  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getRow(0)))[1] - 3.) < 1e-12, "getRow");
264  logger.assert_always(SmallMatrix<3, 2>({vec3D0, vec3D1}).getRow(1).size() == 2, "getRow");
265  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getRow(1)))[0] - 1.) < 1e-12, "getRow");
266  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getRow(1)))[1] - 4.) < 1e-12, "getRow");
267  logger.assert_always(SmallMatrix<3, 2>({vec3D0, vec3D1}).getRow(2).size() == 2, "getRow");
268  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getRow(2)))[0] - 2.) < 1e-12, "getRow");
269  logger.assert_always(std::abs(((SmallMatrix<3, 2>({vec3D0, vec3D1}).getRow(2)))[1] - 5.) < 1e-12, "getRow");
270 
272 
273  SmallVector<2> duplicate = vec2;
274  count0.solve(vec2);
275  logger.assert_always(std::abs((vec2 - count0.inverse() * duplicate)[0]) < 1e-12, "inverse and solve");
276  logger.assert_always(std::abs((vec2 - count0.inverse() * duplicate)[1]) < 1e-12, "inverse and solve");
277  logger.assert_always(std::abs((count0 - count0.inverse().inverse())[0]) < 1e-12, "inverse");
278  logger.assert_always(std::abs((count0 - count0.inverse().inverse())[1]) < 1e-12, "inverse");
279  logger.assert_always(std::abs((count0 - count0.inverse().inverse())[2]) < 1e-12, "inverse");
280  logger.assert_always(std::abs((count0 - count0.inverse().inverse())[3]) < 1e-12, "inverse");
281  logger.assert_always(std::abs((count0 - count0.transpose().transpose())[0]) < 1e-12, "transpose");
282  logger.assert_always(std::abs((count0 - count0.transpose().transpose())[1]) < 1e-12, "transpose");
283  logger.assert_always(std::abs((count0 - count0.transpose().transpose())[2]) < 1e-12, "transpose");
284  logger.assert_always(std::abs((count0 - count0.transpose().transpose())[3]) < 1e-12, "transpose");
285  logger.assert_always(std::abs(A23(0, 0) - A23.transpose()(0, 0)) < 1e-12, "transpose");
286  logger.assert_always(std::abs(A23(1, 0) - A23.transpose()(0, 1)) < 1e-12, "transpose");
287  logger.assert_always(std::abs(A23(0, 1) - A23.transpose()(1, 0)) < 1e-12, "transpose");
288  logger.assert_always(std::abs(A23(1, 1) - A23.transpose()(1, 1)) < 1e-12, "transpose");
289  logger.assert_always(std::abs(A23(0, 2) - A23.transpose()(2, 0)) < 1e-12, "transpose");
290  logger.assert_always(std::abs(A23(1, 2) - A23.transpose()(2, 1)) < 1e-12, "transpose");
291 
292  auto data = A23.data();
293  logger.assert_always(std::abs(data[0] - A23[0]) < 1e-12, "data");
294  logger.assert_always(std::abs(data[1] - A23[1]) < 1e-12, "data");
295  logger.assert_always(std::abs(data[2] - A23[2]) < 1e-12, "data");
296  logger.assert_always(std::abs(data[3] - A23[3]) < 1e-12, "data");
297  logger.assert_always(std::abs(data[4] - A23[4]) < 1e-12, "data");
298  logger.assert_always(std::abs(data[5] - A23[5]) < 1e-12, "data");
299  data[2] = 17.3;
300  logger.assert_always(std::abs(A23[2] - 17.3) < 1e-12, "data");
301 
302  logger(INFO, "%", A23);
303 
304  return 0;
305 }
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
Data type for small dense matrix.
Definition: SmallMatrix.h:68
unsigned int getNCols() const
Definition: SmallMatrix.h:298
unsigned int size() const
Get total number of Matrix entries.
Definition: SmallMatrix.h:275
unsigned int getNumberOfRows() const
Get the number of rows.
Definition: SmallMatrix.h:281
void axpy(Mdouble a, const SmallMatrix &x)
Applies the matrix y=ax + y, where x is another matrix and a is a scalar.
Definition: SmallMatrix.h:266
Definition: SmallVector.h:62
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51

References SmallMatrix< numberOfRows, numberOfColumns >::axpy(), SmallMatrix< numberOfRows, numberOfColumns >::data(), SmallMatrix< numberOfRows, numberOfColumns >::getNCols(), SmallMatrix< numberOfRows, numberOfColumns >::getNumberOfRows(), constants::i, INFO, SmallMatrix< numberOfRows, numberOfColumns >::inverse(), logger, SmallMatrix< numberOfRows, numberOfColumns >::size(), SmallMatrix< numberOfRows, numberOfColumns >::solve(), and SmallMatrix< numberOfRows, numberOfColumns >::transpose().