38 #define BOOST_TEST_MODULE ( FastMatrixMath_test ) 39 #include "boost/test/unit_test.hpp" 61 template <
typename Array>
65 #ifdef FASTMATRIXMATH_TEST_DEBUG 66 const size_t Dim = size_t(std::sqrt(m.size()));
68 out <<
name <<
" " << Dim <<
"x" << Dim <<
":";
69 for (
size_t r = 0;
r < Dim; ++
r) {
71 for (
size_t c = 0;
c < Dim; ++
c)
72 out <<
" " << m[
r * Dim +
c];
76 #endif // FASTMATRIXMATH_TEST_DEBUG 80 template <
typename Array>
82 const size_t Dim = size_t(std::sqrt(m.size()));
84 for (
size_t r = 1;
r < Dim; ++
r)
85 for (
size_t c =
r + 1;
c < Dim; ++
c)
86 BOOST_TEST(m[
r * Dim +
c] == m[
r * Dim +
c], 0.001%
tolerance());
91 template <
typename Array>
94 const size_t Dim = size_t(std::sqrt(a.size()));
95 using Data_t =
typename Array::value_type;
97 BOOST_TEST(a.size() == a_inv.size());
99 for (
size_t r = 0;
r < Dim; ++
r) {
100 for (
size_t c = 0;
c < Dim; ++
c) {
102 for (
size_t k = 0;
k < Dim; ++
k) {
103 v += a[
r * Dim +
k] * a_inv[
k * Dim +
c];
105 if (
r ==
c) BOOST_TEST(v == Data_t(1), 0.01%
tolerance());
113 template <
typename Array>
116 using Data_t =
typename Array::value_type;
119 static_assert((Dim >= 1) && (Dim <= 4),
"Dimension not supported");
121 using FastMatrixOperations
124 Array mat_inv = FastMatrixOperations::InvertMatrix(mat);
125 PrintMatrix(std::cout, mat_inv,
"Alleged inverse matrix");
131 template <
typename Array>
134 using Data_t =
typename Array::value_type;
137 static_assert((Dim >= 1) && (Dim <= 4),
"Dimension not supported");
139 using FastMatrixOperations
142 const Data_t my_det = FastMatrixOperations::Determinant(mat);
145 if (std::isnormal(det)) {
146 Array mat_inv = FastMatrixOperations::InvertMatrix(mat, det);
147 PrintMatrix(std::cout, mat_inv,
"Alleged inverse matrix");
153 template <
typename Array>
156 using Data_t =
typename Array::value_type;
159 static_assert((Dim >= 1) && (Dim <= 4),
"Dimension not supported");
161 using FastMatrixOperations
166 Array mat_inv = FastMatrixOperations::InvertSymmetricMatrix(mat);
167 PrintMatrix(std::cout, mat_inv,
"Alleged inverse matrix");
173 template <
typename Array>
176 using Data_t =
typename Array::value_type;
179 static_assert((Dim >= 1) && (Dim <= 4),
"Dimension not supported");
181 using FastMatrixOperations
184 const Data_t my_det = FastMatrixOperations::Determinant(mat);
187 if (std::isnormal(det)) {
188 Array mat_inv = FastMatrixOperations::InvertSymmetricMatrix(mat, det);
189 PrintMatrix(std::cout, mat_inv,
"Alleged inverse matrix");
197 template <
typename T>
200 constexpr
unsigned int Dim = 2;
204 std::array<Data_t, Dim*Dim> matrix = {{
205 Data_t(2), Data_t(3),
208 const Data_t true_det = Data_t(-10);
215 template <
typename T>
218 constexpr
unsigned int Dim = 2;
222 std::array<Data_t, Dim*Dim> matrix = {{
223 Data_t(2), Data_t(3),
226 const Data_t true_det = Data_t(-7);
228 PrintMatrix(std::cout, matrix,
"Symmetric matrix");
234 template <
typename T>
238 constexpr
unsigned int Dim = 3;
242 std::array<Data_t, Dim*Dim> matrix = {{
243 Data_t(2), Data_t(0), Data_t(3),
244 Data_t(0), Data_t(3), Data_t(0),
245 Data_t(4), Data_t(0), Data_t(1),
247 const Data_t true_det = Data_t(-30);
255 template <
typename T>
259 constexpr
unsigned int Dim = 3;
263 std::array<Data_t, Dim*Dim> matrix = {{
264 Data_t(2), Data_t(4), Data_t(3),
265 Data_t(0), Data_t(3), Data_t(0),
266 Data_t(4), Data_t(0), Data_t(1),
268 const Data_t true_det = Data_t(-30);
277 template <
typename T>
281 constexpr
unsigned int Dim = 3;
285 std::array<Data_t, Dim*Dim> matrix = {{
286 Data_t(2), Data_t(0), Data_t(3),
287 Data_t(0), Data_t(3), Data_t(0),
288 Data_t(3), Data_t(0), Data_t(1),
290 const Data_t true_det = Data_t(-21);
292 PrintMatrix(std::cout, matrix,
"Symmetric matrix");
298 template <
typename T>
302 constexpr
unsigned int Dim = 4;
306 std::array<Data_t, Dim*Dim> matrix = {{
307 Data_t(2), Data_t(0), Data_t(3), Data_t(0),
308 Data_t(0), Data_t(3), Data_t(0), Data_t(6),
309 Data_t(4), Data_t(0), Data_t(1), Data_t(0),
310 Data_t(0), Data_t(2), Data_t(0), Data_t(7)
312 const Data_t true_det = Data_t(-90);
320 template <
typename T>
324 constexpr
unsigned int Dim = 4;
328 std::array<Data_t, Dim*Dim> matrix = {{
329 Data_t(2), Data_t(0), Data_t(3), Data_t(0),
330 Data_t(5), Data_t(3), Data_t(0), Data_t(6),
331 Data_t(4), Data_t(0), Data_t(1), Data_t(0),
332 Data_t(3), Data_t(2), Data_t(0), Data_t(7)
334 const Data_t true_det = Data_t(-90);
342 template <
typename T,
unsigned int Dim>
347 std::default_random_engine engine;
348 std::uniform_real_distribution<Data_t> uniform(Data_t(-10.), Data_t(10.));
350 std::array<Data_t, Dim*Dim> matrix;
351 for (
unsigned int i = 0; i <
N; ++i) {
353 std::generate(matrix.begin(), matrix.end(),
354 [&engine, &uniform] {
return uniform(engine); }
364 template <
typename T,
unsigned int Dim>
369 std::array<Data_t, Dim*Dim> matrix;
370 matrix.fill(Data_t(0));
374 PrintMatrix(std::cout, matrix,
"Empty symmetric matrix");
381 template <
typename T>
385 constexpr
unsigned int Dim = 4;
389 std::array<Data_t, Dim*Dim> matrix = {{
390 Data_t(2), Data_t(0), Data_t(3), Data_t(0),
391 Data_t(0), Data_t(3), Data_t(0), Data_t(2),
392 Data_t(3), Data_t(0), Data_t(1), Data_t(0),
393 Data_t(0), Data_t(2), Data_t(0), Data_t(7)
395 const Data_t true_det = Data_t(-119);
397 PrintMatrix(std::cout, matrix,
"Symmetric matrix");
416 TestMatrix2x2<double>();
417 TestSymmetricMatrix2x2<double>();
419 TestMatrix_N<double, 2>();
420 TestNullMatrix<double, 2>();
424 TestMatrix3x3_1<double>();
425 TestMatrix3x3_2<double>();
426 TestSymmetricMatrix3x3<double>();
428 TestMatrix_N<double, 3>();
429 TestNullMatrix<double, 3>();
433 TestMatrix4x4_1<double>();
434 TestMatrix4x4_2<double>();
435 TestSymmetricMatrix4x4<double>();
437 TestMatrix_N<double, 4>();
438 TestNullMatrix<double, 4>();
void TestSymmetricMatrix4x4()
constexpr unsigned int static_sqrt(unsigned int n)
void TestSymmetricMatrix3x3()
void CheckSymmetric(Array const &m)
void SymmetricMatrixTest(Array const &mat)
Classes with hard-coded (hence "fast") matrix math.
Provides "fast" matrix operations.
void TestSymmetricMatrix2x2()
static int max(int a, int b)
void PrintMatrix(std::ostream &out, Array const &m, std::string name="Matrix")
void MatrixTest(Array const &mat)
BOOST_AUTO_TEST_CASE(Matrix2x2RealTest)
void TestMatrix_N(unsigned int N=100)
void CheckInverse(Array const &a, Array const &a_inv)
QTextStream & endl(QTextStream &s)