Implements block typs and related functions - First part
Co-authored-by: Karen <kkaspar@codereckons.com> Co-authored-by: Joel FALCOU <jfalcou@codereckons.com> See merge request oss/rotgen!9
This commit is contained in:
parent
09be3b4b15
commit
c6b864f247
28 changed files with 1814 additions and 114 deletions
114
test/unit/matrix/arithmetic_functions.cpp
Normal file
114
test/unit/matrix/arithmetic_functions.cpp
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#include "unit/tests.hpp"
|
||||
#include <rotgen/matrix.hpp>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
template<typename MatrixType>
|
||||
void test_matrix_unary_ops(std::size_t rows, std::size_t cols, auto const &init_fn)
|
||||
{
|
||||
MatrixType original(rows, cols);
|
||||
MatrixType transposed_matrix(cols, rows);
|
||||
|
||||
for (std::size_t r = 0; r < rows; ++r)
|
||||
for (std::size_t c = 0; c < cols; ++c)
|
||||
original(r,c) = init_fn(r, c);
|
||||
|
||||
for (std::size_t r = 0; r < rows; ++r)
|
||||
for (std::size_t c = 0; c < cols; ++c)
|
||||
transposed_matrix(c, r) = original(r, c);
|
||||
|
||||
TTS_EQUAL(original.transpose(), transposed_matrix);
|
||||
TTS_EQUAL(original.conjugate(), original);
|
||||
TTS_EQUAL(original.adjoint(), transposed_matrix);
|
||||
|
||||
original.transposeInPlace();
|
||||
TTS_EQUAL(original, transposed_matrix);
|
||||
|
||||
original.transposeInPlace();
|
||||
original.adjointInPlace();
|
||||
TTS_EQUAL(original, transposed_matrix);
|
||||
}
|
||||
|
||||
TTS_CASE_TPL("Test transpotion related operations", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
std::vector<rotgen::tests::matrix_descriptor> test_matrices =
|
||||
{
|
||||
{3, 3, [](auto r, auto c) { return r + 3 * c - 2.5; }},
|
||||
{4, 9, [](auto r, auto c) { return r*r + 3.12 * c + 6.87; }},
|
||||
{2, 7, [](auto r, auto c) { return 1.1 * (r - c); }},
|
||||
{1, 5, [](auto , auto ) { return 9.99; }},
|
||||
{4, 2, [](auto , auto ) { return 0.0; }},
|
||||
{3, 3, [](auto r, auto c) { return (r == c) ? 1.0 : 0.0; }},
|
||||
{2, 2, [](auto r, auto c) { return (r + c) * 1e-10; }},
|
||||
{2, 2, [](auto r, auto ) { return (r + 1) * 1e+10; }}
|
||||
};
|
||||
|
||||
for (const auto& [rows, cols, fn] : test_matrices)
|
||||
{
|
||||
test_matrix_unary_ops<rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>>(rows, cols, fn);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename MatrixType>
|
||||
void test_matrix_reductions(std::size_t rows, std::size_t cols, auto const& init_fn)
|
||||
{
|
||||
using EigenMatrix = Eigen::Matrix<typename MatrixType::scalar_type, Eigen::Dynamic, Eigen::Dynamic>;
|
||||
|
||||
MatrixType original(rows, cols);
|
||||
EigenMatrix ref(rows, cols);
|
||||
|
||||
for (std::size_t r = 0; r < rows; ++r)
|
||||
for (std::size_t c = 0; c < cols; ++c)
|
||||
ref(r, c) = original(r,c) = init_fn(r, c);
|
||||
|
||||
TTS_EQUAL(original.sum(), ref.sum());
|
||||
TTS_EQUAL(original.prod(), ref.prod());
|
||||
TTS_EQUAL(original.mean(), ref.mean());
|
||||
TTS_EQUAL(original.maxCoeff(), ref.maxCoeff());
|
||||
TTS_EQUAL(original.minCoeff(), ref.minCoeff());
|
||||
TTS_EQUAL(original.trace(), ref.trace());
|
||||
|
||||
std::ptrdiff_t row, col, ref_row, ref_col;
|
||||
|
||||
double cmin = original.minCoeff(&row, &col);
|
||||
double rmin = ref.minCoeff(&ref_row, &ref_col);
|
||||
|
||||
TTS_EQUAL(cmin, rmin);
|
||||
TTS_EQUAL(row, ref_row);
|
||||
TTS_EQUAL(col, ref_col);
|
||||
|
||||
double cmax = original.maxCoeff(&row, &col);
|
||||
double rmax = ref.maxCoeff(&ref_row, &ref_col);
|
||||
|
||||
TTS_EQUAL(cmax, rmax);
|
||||
TTS_EQUAL(row, ref_row);
|
||||
TTS_EQUAL(col, ref_col);
|
||||
}
|
||||
|
||||
TTS_CASE_TPL("Test reductions", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
std::vector<rotgen::tests::matrix_descriptor> test_matrices =
|
||||
{
|
||||
{3, 3, [](auto r, auto c) {return r + c; }},
|
||||
{3, 3, [](auto , auto ) {return 0.0; }},
|
||||
{2, 4, [](auto r, auto c) {return -r -c*c - 1234; }},
|
||||
{4, 4, [](auto , auto ) {return 7.0; }},
|
||||
{1, 1, [](auto , auto ) {return 42.0; }},
|
||||
{4, 2, [](auto r, auto c) {return std::sin(r + c); }},
|
||||
{1, 5, [](auto r, auto c) {return -1.5 * r + 2.56 * c; }},
|
||||
{5, 7, [](auto r, auto c) {return (r == c ? 1.0 : 0.0); }}
|
||||
};
|
||||
|
||||
for (const auto& [rows, cols, fn] : test_matrices)
|
||||
{
|
||||
test_matrix_reductions<rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>>(rows, cols, fn);
|
||||
}
|
||||
};
|
||||
187
test/unit/matrix/constructors.cpp
Normal file
187
test/unit/matrix/constructors.cpp
Normal file
|
|
@ -0,0 +1,187 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#include "unit/tests.hpp"
|
||||
#include <rotgen/matrix.hpp>
|
||||
|
||||
TTS_CASE_TPL("Default matrix dynamic constructor", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> matrix;
|
||||
|
||||
TTS_EQUAL(matrix.rows(), 0ULL);
|
||||
TTS_EQUAL(matrix.cols(), 0ULL);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Default matrix static constructor", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,4,9,O::value> matrix;
|
||||
|
||||
TTS_EQUAL(matrix.rows(), 4ULL);
|
||||
TTS_EQUAL(matrix.cols(), 9ULL);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Dynamic matrix constructor with row and columns", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> matrix(10, 5);
|
||||
|
||||
TTS_EQUAL(matrix.rows(), 10ULL);
|
||||
TTS_EQUAL(matrix.cols(), 5ULL);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Static matrix constructor with row and columns", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,6,11,O::value> matrix(6, 11);
|
||||
|
||||
TTS_EQUAL(matrix.rows(), 6ULL);
|
||||
TTS_EQUAL(matrix.cols(), 11ULL);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Copy constructor produces identical but independent matrix", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> a(3, 3);
|
||||
|
||||
for(std::size_t r=0;r<a.rows();r++)
|
||||
for(std::size_t c=0;c<a.cols();c++)
|
||||
a(r,c) = r + c;
|
||||
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> b = a;
|
||||
|
||||
TTS_EQUAL(b.rows(), a.rows());
|
||||
TTS_EQUAL(b.cols(), a.cols());
|
||||
|
||||
for(std::size_t r=0;r<a.rows();r++)
|
||||
for(std::size_t c=0;c<a.cols();c++)
|
||||
TTS_EQUAL(b(r,c), a(r,c));
|
||||
|
||||
TTS_EQUAL(b(0, 0), 0.0);
|
||||
TTS_EQUAL(b(1, 0), 1.0);
|
||||
TTS_EQUAL(b(2, 1), 3.0);
|
||||
|
||||
a(0,0) = 42.0;
|
||||
TTS_NOT_EQUAL(b(0,0), a(0,0));
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Copy constructor on default matrix", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> a;
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> b = a;
|
||||
TTS_EQUAL(b.rows(), 0ULL);
|
||||
TTS_EQUAL(b.cols(), 0ULL);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Copy constructor from const matrix", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
const rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> a(2, 2);
|
||||
auto b = a;
|
||||
TTS_EQUAL(b.rows(), 2ULL);
|
||||
TTS_EQUAL(b.cols(), 2ULL);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Copy constructor on static matrix", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T, 2, 5> a;
|
||||
rotgen::matrix<T, 2, 5> b = a;
|
||||
TTS_EQUAL(b.rows(), 2ULL);
|
||||
TTS_EQUAL(b.cols(), 5ULL);
|
||||
};
|
||||
|
||||
/*
|
||||
TTS_CASE_TPL("Copy constructor on static/dynamic matrix", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T, 11, 4,O::value> a;
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> b = a;
|
||||
TTS_EQUAL(b.rows(), 11);
|
||||
TTS_EQUAL(b.cols(), 4);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Copy constructor on dynamic/static matrix", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> a(5, 7);
|
||||
rotgen::matrix<T, 5, 7,O::value> b = a;
|
||||
TTS_EQUAL(b.rows(), 5);
|
||||
TTS_EQUAL(b.cols(), 7);
|
||||
};*/
|
||||
|
||||
TTS_CASE_TPL("Move constructor transfers contents", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> a(3, 3);
|
||||
a(1,1) = 7;
|
||||
auto ptr = a.data();
|
||||
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> b = std::move(a);
|
||||
|
||||
TTS_EQUAL(b(1,1), 7);
|
||||
TTS_EQUAL(b.rows(), 3ULL);
|
||||
TTS_EQUAL(b.cols(), 3ULL);
|
||||
TTS_EXPECT(b.data() == ptr);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Move constructor from Rvalue", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> b = rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>(2, 2);
|
||||
TTS_EQUAL(b.rows(), 2ULL);
|
||||
TTS_EQUAL(b.cols(), 2ULL);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Constructor from Initializer list", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,1,1,O::value> b1 = {3.5};
|
||||
TTS_EQUAL(b1.rows(), 1ULL);
|
||||
TTS_EQUAL(b1.cols(), 1ULL);
|
||||
TTS_EQUAL(b1(0) , 3.5);
|
||||
|
||||
rotgen::matrix<T,5,1,O::value> b9 = {0.25, 0.5, 1, 2, 4};
|
||||
TTS_EQUAL(b9.rows(), 5ULL);
|
||||
TTS_EQUAL(b9.cols(), 1ULL);
|
||||
|
||||
T i = 0.25;
|
||||
for(std::size_t r=0;r<b9.rows();++r) {
|
||||
TTS_EQUAL(b9(r,0), i);
|
||||
i *= 2;
|
||||
}
|
||||
|
||||
rotgen::matrix<T,1,3,O::value> b13 = {1.2,2.3,3.4};
|
||||
TTS_EQUAL(b13.rows(), 1ULL);
|
||||
TTS_EQUAL(b13.cols(), 3ULL);
|
||||
TTS_EQUAL(b13(0) , T(1.2));
|
||||
TTS_EQUAL(b13(1) , T(2.3));
|
||||
TTS_EQUAL(b13(2) , T(3.4));
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Constructor from Initializer list of rows", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> b1 = {{3.5}};
|
||||
TTS_EQUAL(b1.rows(), 1ULL);
|
||||
TTS_EQUAL(b1.cols(), 1ULL);
|
||||
TTS_EQUAL(b1(0,0) , T(3.5));
|
||||
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> b23 = { {1.2,2.3,3.4}
|
||||
, {10,200,3000}
|
||||
};
|
||||
TTS_EQUAL(b23.rows(), 2ULL);
|
||||
TTS_EQUAL(b23.cols(), 3ULL);
|
||||
TTS_EQUAL(b23(0,0) , T(1.2));
|
||||
TTS_EQUAL(b23(0,1) , T(2.3));
|
||||
TTS_EQUAL(b23(0,2) , T(3.4));
|
||||
TTS_EQUAL(b23(1,0) , T(10));
|
||||
TTS_EQUAL(b23(1,1) , T(200));
|
||||
TTS_EQUAL(b23(1,2) , T(3000));
|
||||
};
|
||||
164
test/unit/matrix/functions.cpp
Normal file
164
test/unit/matrix/functions.cpp
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#include "unit/tests.hpp"
|
||||
#include <rotgen/matrix.hpp>
|
||||
|
||||
TTS_CASE_TPL("Function size", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> empty_matrix;
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> matrix(3,4);
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> row_vector(9,1);
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> column_vector(1,5);
|
||||
|
||||
TTS_EQUAL(empty_matrix.size(), 0ULL);
|
||||
TTS_EQUAL(matrix.size(), 12ULL);
|
||||
TTS_EQUAL(row_vector.size(), 9ULL);
|
||||
TTS_EQUAL(column_vector.size(), 5ULL);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Resizing dynamic matrix", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> a(2, 3);
|
||||
|
||||
for(std::size_t r=0;r<a.rows();++r)
|
||||
for(std::size_t c=0;c<a.cols();++c)
|
||||
a(r,c) = 42 + 2*c + r;
|
||||
|
||||
a.resize(3, 2);
|
||||
TTS_EQUAL(a.rows(), 3ULL);
|
||||
TTS_EQUAL(a.cols(), 2ULL);
|
||||
|
||||
for(std::size_t r=0;r<a.rows();++r)
|
||||
for(std::size_t c=0;c<a.cols();++c)
|
||||
TTS_GREATER(a(r,c),0);
|
||||
|
||||
a.resize(2,2);
|
||||
TTS_EQUAL(a.rows(), 2ULL);
|
||||
TTS_EQUAL(a.cols(), 2ULL);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Resizing static matrix", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<double, 3, 3> a;
|
||||
TTS_EXPECT_NOT_COMPILES(a, { a.resize(4, 5); });
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Dynamix matrix conservative resizing", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> a(2, 3);
|
||||
|
||||
int i = 1;
|
||||
for(std::size_t r=0;r<a.rows();++r)
|
||||
for(std::size_t c=0;c<a.cols();++c)
|
||||
a(r, c) = i++;
|
||||
|
||||
a.conservativeResize(2, 3);
|
||||
TTS_EQUAL(a.rows(), 2ULL);
|
||||
TTS_EQUAL(a.cols(), 3ULL);
|
||||
|
||||
i = 1;
|
||||
for(std::size_t r=0;r<a.rows();++r)
|
||||
for(std::size_t c=0;c<a.cols();++c)
|
||||
TTS_EQUAL(a(r,c),i++);
|
||||
|
||||
a.conservativeResize(3, 2);
|
||||
TTS_EQUAL(a.rows(), 3ULL);
|
||||
TTS_EQUAL(a.cols(), 2ULL);
|
||||
int expected[3][2] = { {1, 2}, {4, 5} };
|
||||
for(std::size_t r = 0; r < 2; ++r)
|
||||
for(std::size_t c = 0; c < 2; ++c)
|
||||
TTS_EQUAL(a(r, c), expected[r][c]);
|
||||
|
||||
a.conservativeResize(4, 4);
|
||||
TTS_EQUAL(a.rows(), 4ULL);
|
||||
TTS_EQUAL(a.cols(), 4ULL);
|
||||
TTS_EQUAL(a(0,0), 1);
|
||||
TTS_EQUAL(a(3,3), 0);
|
||||
|
||||
a.conservativeResize(2, 2);
|
||||
TTS_EQUAL(a.rows(), 2ULL);
|
||||
TTS_EQUAL(a.cols(), 2ULL);
|
||||
TTS_EQUAL(a(0,0), 1);
|
||||
TTS_EQUAL(a(1,1), 5);
|
||||
|
||||
a.conservativeResize(1, 2);
|
||||
TTS_EQUAL(a.rows(), 1ULL);
|
||||
TTS_EQUAL(a.cols(), 2ULL);
|
||||
TTS_EQUAL(a(0,0), 1);
|
||||
TTS_EQUAL(a(0,1), 2);
|
||||
|
||||
a.conservativeResize(0, 0);
|
||||
TTS_EQUAL(a.rows(), 0ULL);
|
||||
TTS_EQUAL(a.cols(), 0ULL);
|
||||
|
||||
a.conservativeResize(3, 3);
|
||||
TTS_EQUAL(a.rows(), 3ULL);
|
||||
TTS_EQUAL(a.cols(), 3ULL);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Static matrix conservative resizing", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<double, 3, 3> a;
|
||||
TTS_EXPECT_NOT_COMPILES(a, { a.conservativeResize(4, 5); });
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Test coefficient accessors", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> a(3, 5);
|
||||
|
||||
for(std::size_t r=0;r<a.rows();++r)
|
||||
for(std::size_t c=0;c<a.cols();++c)
|
||||
a(r, c) = r + 2 * c + 3;
|
||||
|
||||
TTS_EQUAL(a(0,0), 3);
|
||||
TTS_EQUAL(a(1,1), 6);
|
||||
TTS_EQUAL(a(1,0), 4);
|
||||
TTS_EQUAL(a(2,2), 9);
|
||||
TTS_EQUAL(a(2,4), 13);
|
||||
|
||||
a(1, 1) = 42;
|
||||
TTS_EQUAL(a(1,1), 42);
|
||||
|
||||
T& ref = a(2, 2);
|
||||
ref = 17;
|
||||
assert(a(2, 2) == 17);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Test one index coefficient accessors", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> a(2, 4);
|
||||
|
||||
int i = 1;
|
||||
if constexpr(!O::value)
|
||||
{
|
||||
for(std::size_t c=0;c<a.cols();++c)
|
||||
for(std::size_t r=0;r<a.rows();++r)
|
||||
a(r, c) = i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(std::size_t r=0;r<a.rows();++r)
|
||||
for(std::size_t c=0;c<a.cols();++c)
|
||||
a(r, c) = i++;
|
||||
}
|
||||
|
||||
i = 1;
|
||||
for(std::size_t r=0;r<a.rows()*a.cols();++r)
|
||||
TTS_EQUAL(a(r), i++) << a;
|
||||
|
||||
T& ref = a(2);
|
||||
ref = 999.5;
|
||||
TTS_EQUAL(a(2), 999.5);
|
||||
};
|
||||
20
test/unit/matrix/io.cpp
Normal file
20
test/unit/matrix/io.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#include "unit/tests.hpp"
|
||||
#include <rotgen/matrix.hpp>
|
||||
#include <sstream>
|
||||
|
||||
TTS_CASE_TPL("Sample test", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> x({ {1,2} , {3,4} });
|
||||
std::ostringstream os;
|
||||
os << x;
|
||||
|
||||
TTS_EQUAL(os.str(), std::string{"1 2\n3 4"});
|
||||
};
|
||||
47
test/unit/matrix/norms.cpp
Normal file
47
test/unit/matrix/norms.cpp
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#include "unit/tests.hpp"
|
||||
#include <rotgen/matrix.hpp>
|
||||
#include <Eigen/Dense>
|
||||
|
||||
TTS_CASE_TPL("Matrix norm-related operations", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
std::vector<rotgen::tests::matrix_descriptor> test_matrices =
|
||||
{
|
||||
{ 3, 3, [](int r, int c) { return r + c; }},
|
||||
{ 2, 3, [](int , int ) { return T{0}; }},
|
||||
{ 2, 7, [](int r, int c) { return -r*r*r - c*c - T(1.23); }},
|
||||
{17, 3, [](int r, int c) { return r*c + T(0.98); }},
|
||||
{ 1, 1, [](int , int ) { return 42; }},
|
||||
{10, 11, [](int r, int c) { return std::tan(T(r + c)); }},
|
||||
{ 1, 5, [](int r, int c) { return T(-1.5)*r + T(2.56)*c + T(3.33); }},
|
||||
{ 7, 1, [](int r, int c) { return r == c ? T(1) : T(0); }},
|
||||
{ 0, 0, [](int r, int c) { return r == c ? T(1) : T(0); }},
|
||||
};
|
||||
|
||||
for (const auto& [rows, cols, fn] : test_matrices)
|
||||
{
|
||||
rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value> matrix(rows, cols);
|
||||
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic,O::value> ref(rows, cols);
|
||||
|
||||
for (std::size_t r = 0; r < rows; ++r)
|
||||
{
|
||||
for (std::size_t c = 0; c < cols; ++c)
|
||||
{
|
||||
ref(r, c) = matrix(r, c) = fn(r,c);
|
||||
}
|
||||
}
|
||||
|
||||
TTS_EQUAL(matrix.norm(), ref.norm());
|
||||
TTS_EQUAL(matrix.squaredNorm(), ref.squaredNorm());
|
||||
TTS_EQUAL(matrix.template lp_norm<1>() , ref.template lpNorm<1>());
|
||||
TTS_EQUAL(matrix.template lp_norm<2>() , ref.template lpNorm<2>());
|
||||
TTS_EQUAL(matrix.template lp_norm<rotgen::Infinity>(), ref.template lpNorm<Eigen::Infinity>());
|
||||
}
|
||||
};
|
||||
193
test/unit/matrix/operators.cpp
Normal file
193
test/unit/matrix/operators.cpp
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#include "unit/tests.hpp"
|
||||
#include <rotgen/matrix.hpp>
|
||||
|
||||
template <typename MatrixType>
|
||||
void test_matrix_operations(std::size_t rows, std::size_t cols, auto a_init_fn, auto b_init_fn, auto ops, auto self_ops)
|
||||
{
|
||||
MatrixType a(rows, cols);
|
||||
MatrixType b(rows, cols);
|
||||
MatrixType ref(rows, cols);
|
||||
|
||||
for (std::size_t r = 0; r < rows; ++r)
|
||||
{
|
||||
for (std::size_t c = 0; c < cols; ++c)
|
||||
{
|
||||
a(r,c) = a_init_fn(r, c);
|
||||
b(r,c) = b_init_fn(r, c);
|
||||
ref(r, c) = ops(a(r,c),b(r,c));
|
||||
}
|
||||
}
|
||||
|
||||
TTS_EQUAL(ops(a, b), ref);
|
||||
self_ops(a,b);
|
||||
TTS_EQUAL(a, ref);
|
||||
}
|
||||
|
||||
template <typename MatrixType>
|
||||
void test_scalar_operations(std::size_t rows, std::size_t cols, auto a_init_fn, auto s, auto ops, auto self_ops)
|
||||
{
|
||||
MatrixType a(rows, cols);
|
||||
MatrixType ref(rows, cols);
|
||||
|
||||
for (std::size_t r = 0; r < rows; ++r)
|
||||
{
|
||||
for (std::size_t c = 0; c < cols; ++c)
|
||||
{
|
||||
a(r,c) = a_init_fn(r, c);
|
||||
ref(r, c) = ops(a(r,c),s);
|
||||
}
|
||||
}
|
||||
|
||||
TTS_EQUAL(ops(a, s), ref);
|
||||
self_ops(a,s);
|
||||
TTS_EQUAL(a, ref);
|
||||
}
|
||||
|
||||
template <typename MatrixType>
|
||||
void test_scalar_multiplications(std::size_t rows, std::size_t cols, auto fn, auto s)
|
||||
{
|
||||
MatrixType a(rows, cols);
|
||||
MatrixType ref(rows, cols);
|
||||
|
||||
for (std::size_t r = 0; r < rows; ++r)
|
||||
{
|
||||
for (std::size_t c = 0; c < cols; ++c)
|
||||
{
|
||||
a(r,c) = fn(r, c);
|
||||
ref(r, c) = a(r,c) * s;
|
||||
}
|
||||
}
|
||||
|
||||
TTS_EQUAL(a * s, ref);
|
||||
TTS_EQUAL(s * a, ref);
|
||||
a *= s;
|
||||
TTS_EQUAL(a, ref);
|
||||
}
|
||||
|
||||
template <typename MatrixType>
|
||||
void test_matrix_multiplication(std::size_t rows, std::size_t cols, auto a_init_fn, auto b_init_fn)
|
||||
{
|
||||
MatrixType a(rows, cols);
|
||||
MatrixType b(cols, rows);
|
||||
MatrixType ref(rows, rows);
|
||||
|
||||
for (std::size_t r = 0; r < a.rows(); ++r)
|
||||
for (std::size_t c = 0; c < a.cols(); ++c)
|
||||
a(r,c) = a_init_fn(r, c);
|
||||
|
||||
for (std::size_t r = 0; r < b.rows(); ++r)
|
||||
for (std::size_t c = 0; c < b.cols(); ++c)
|
||||
b(r,c) = b_init_fn(r, c);
|
||||
|
||||
for (std::size_t i = 0; i < a.rows(); ++i)
|
||||
{
|
||||
for (std::size_t j = 0; j < b.cols(); ++j)
|
||||
{
|
||||
ref(i, j) = 0;
|
||||
for (std::size_t k = 0; k < a.cols(); ++k)
|
||||
ref(i, j) += a(i, k) * b(k, j);
|
||||
}
|
||||
}
|
||||
|
||||
TTS_EQUAL(a * b, ref);
|
||||
}
|
||||
|
||||
// Basic initializers
|
||||
inline constexpr auto init_a = [](auto r, auto c) { return 9.9*r*r*r - 6*c -12; };
|
||||
inline constexpr auto init_b = [](auto r, auto c) { return 3.1*r + 4.2*c - 12.3; };
|
||||
inline constexpr auto init_0 = [](auto , auto ) { return 0; };
|
||||
|
||||
TTS_CASE_TPL("Check matrix addition", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
using mat_t = rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>;
|
||||
auto op = [](auto a, auto b) { return a + b; };
|
||||
auto s_op = [](auto& a, auto b) { return a += b; };
|
||||
|
||||
test_matrix_operations<mat_t>(1 , 1, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(3 , 5, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(5 , 3, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(5 , 5, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(5 , 5, init_b, init_a, op, s_op);
|
||||
test_matrix_operations<mat_t>(10, 1, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(1 ,10, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(5 , 5, init_0, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(5 , 5, init_a, init_0, op, s_op);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Check matrix substraction", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
using mat_t = rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>;
|
||||
auto op = [](auto a, auto b) { return a - b; };
|
||||
auto s_op = [](auto& a, auto b) { return a -= b; };
|
||||
|
||||
test_matrix_operations<mat_t>(1 , 1, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(3 , 5, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(5 , 3, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(5 , 5, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(5 , 5, init_b, init_a, op, s_op);
|
||||
test_matrix_operations<mat_t>(10, 1, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(1 ,10, init_a, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(5 , 5, init_0, init_b, op, s_op);
|
||||
test_matrix_operations<mat_t>(5 , 5, init_a, init_0, op, s_op);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Check matrix multiplications", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
using mat_t = rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>;
|
||||
auto init_id = [](auto r, auto c) { return r == c ? 1 : 0; };
|
||||
|
||||
test_matrix_multiplication<mat_t>(1 , 1, init_a , init_b );
|
||||
test_matrix_multiplication<mat_t>(3 , 5, init_a , init_b );
|
||||
test_matrix_multiplication<mat_t>(5 , 3, init_a , init_b );
|
||||
test_matrix_multiplication<mat_t>(5 , 5, init_a , init_b );
|
||||
test_matrix_multiplication<mat_t>(5 , 5, init_b , init_a );
|
||||
test_matrix_multiplication<mat_t>(5 , 5, init_a , init_a );
|
||||
test_matrix_multiplication<mat_t>(5 , 5, init_a , init_id);
|
||||
test_matrix_multiplication<mat_t>(5 , 5, init_id, init_a );
|
||||
test_matrix_multiplication<mat_t>(10, 1, init_a , init_b );
|
||||
test_matrix_multiplication<mat_t>(1 ,10, init_a , init_b );
|
||||
test_matrix_multiplication<mat_t>(5 , 5, init_0 , init_b );
|
||||
test_matrix_multiplication<mat_t>(5 , 5, init_a , init_0 );
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Check matrix multiplication with scalar", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
using mat_t = rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>;
|
||||
|
||||
test_scalar_multiplications<mat_t>(1 , 1, init_a, T{ 3.5});
|
||||
test_scalar_multiplications<mat_t>(3 , 5, init_a, T{-2.5});
|
||||
test_scalar_multiplications<mat_t>(5 , 3, init_a, T{ 4. });
|
||||
test_scalar_multiplications<mat_t>(5 , 5, init_a, T{-5. });
|
||||
test_scalar_multiplications<mat_t>(5 , 5, init_a, T{ 1. });
|
||||
test_scalar_multiplications<mat_t>(5 , 5, init_a, T{ 6. });
|
||||
test_scalar_multiplications<mat_t>(10, 1, init_a, T{ 10.});
|
||||
test_scalar_multiplications<mat_t>(1 ,10, init_a, T{-0.5});
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Check matrix division with scalar", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
using mat_t = rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic,O::value>;
|
||||
auto op = [](auto a, auto b) { return a / b; };
|
||||
auto s_op = [](auto& a, auto b) { return a /= b; };
|
||||
|
||||
test_scalar_operations<mat_t>(1 , 1, init_a, T{ 3.5}, op, s_op);
|
||||
test_scalar_operations<mat_t>(3 , 5, init_a, T{-2.5}, op, s_op);
|
||||
test_scalar_operations<mat_t>(5 , 3, init_a, T{ 4. }, op, s_op);
|
||||
test_scalar_operations<mat_t>(5 , 5, init_a, T{-5. }, op, s_op);
|
||||
test_scalar_operations<mat_t>(5 , 5, init_a, T{ 1. }, op, s_op);
|
||||
test_scalar_operations<mat_t>(5 , 5, init_a, T{ 6. }, op, s_op);
|
||||
test_scalar_operations<mat_t>(10, 1, init_a, T{ 10.}, op, s_op);
|
||||
test_scalar_operations<mat_t>(1 ,10, init_a, T{-0.5}, op, s_op);
|
||||
};
|
||||
81
test/unit/matrix/static_functions.cpp
Normal file
81
test/unit/matrix/static_functions.cpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
//==================================================================================================
|
||||
/*
|
||||
ROTGEN - Runtime Overlay for Eigen
|
||||
Copyright : CODE RECKONS
|
||||
SPDX-License-Identifier: BSL-1.0
|
||||
*/
|
||||
//==================================================================================================
|
||||
#include "unit/tests.hpp"
|
||||
#include <rotgen/matrix.hpp>
|
||||
|
||||
void test_value(const auto& matrix, std::size_t rows, std::size_t cols, auto constant)
|
||||
{
|
||||
for(std::size_t r=0;r<rows;++r)
|
||||
for(std::size_t c=0;c<cols;++c)
|
||||
TTS_EQUAL(matrix(r, c), constant);
|
||||
}
|
||||
|
||||
void test_random(const auto& matrix, std::size_t rows, std::size_t cols)
|
||||
{
|
||||
for(std::size_t r=0;r<rows;++r)
|
||||
for(std::size_t c=0;c<cols;++c)
|
||||
{
|
||||
TTS_GREATER_EQUAL(matrix(r, c), -1.0);
|
||||
TTS_LESS_EQUAL(matrix(r, c), 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
void test_identity(const auto& matrix, std::size_t rows, std::size_t cols)
|
||||
{
|
||||
for(std::size_t r=0;r<rows;++r)
|
||||
for(std::size_t c=0;c<cols;++c)
|
||||
TTS_EQUAL(matrix(r, c), r==c ? 1 : 0);
|
||||
}
|
||||
|
||||
TTS_CASE_TPL("Test zero", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
test_value(rotgen::matrix<T, 3, 4, O::value>::Zero(), 3, 4, 0);
|
||||
test_value(rotgen::matrix<T, 1, 1, O::value>::Zero(), 1, 1, 0);
|
||||
test_value(rotgen::matrix<T, 10, 10, O::value>::Zero(), 10, 10, 0);
|
||||
test_value(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>::Zero(3, 4), 3, 4, 0);
|
||||
test_value(rotgen::matrix<T, 7, 5, O::value>::Zero(7, 5), 7, 5, 0);
|
||||
test_value(rotgen::matrix<T, 9,rotgen::Dynamic, O::value>::Zero(9, 3), 9, 3, 0);
|
||||
test_value(rotgen::matrix<T,rotgen::Dynamic, 3, O::value>::Zero(2, 3), 2, 3, 0);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Test constant", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
test_value(rotgen::matrix<T, 3, 8, O::value>::Constant(5.12), 3, 8, T(5.12));
|
||||
test_value(rotgen::matrix<T, 1, 1, O::value>::Constant(2.2), 1, 1, T(2.2));
|
||||
test_value(rotgen::matrix<T, 11, 12, O::value>::Constant(13), 11, 12, T(13));
|
||||
test_value(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>::Constant(2, 7, 5.6), 2, 7, T(5.6));
|
||||
test_value(rotgen::matrix<T, 2, 2, O::value>::Constant(2, 2, 2.0), 2, 2, T(2.0));
|
||||
test_value(rotgen::matrix<T, 9,rotgen::Dynamic, O::value>::Constant(9, 3, 1.1), 9, 3, T(1.1));
|
||||
test_value(rotgen::matrix<T,rotgen::Dynamic, 9, O::value>::Constant(5, 9, 42), 5, 9,T(42));
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Test random", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
test_random(rotgen::matrix<T, 2, 3, O::value>::Random(), 2, 3);
|
||||
test_random(rotgen::matrix<T, 1, 1, O::value>::Random(), 1, 1);
|
||||
test_random(rotgen::matrix<T, 11, 17, O::value>::Random(), 11, 17);
|
||||
test_random(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>::Random(7, 3), 7, 3);
|
||||
test_random(rotgen::matrix<T, 2, 2, O::value>::Random(2, 2), 2, 2);
|
||||
test_random(rotgen::matrix<T, 4,rotgen::Dynamic, O::value>::Random(4, 3), 4, 3);
|
||||
test_random(rotgen::matrix<T,rotgen::Dynamic, 5, O::value>::Random(5, 5), 5, 5);
|
||||
};
|
||||
|
||||
TTS_CASE_TPL("Test identity", rotgen::tests::types)
|
||||
<typename T, typename O>( tts::type< tts::types<T,O>> )
|
||||
{
|
||||
test_identity(rotgen::matrix<T, 4, 5, O::value>::Identity(), 4, 5);
|
||||
test_identity(rotgen::matrix<T, 1, 1, O::value>::Identity(), 1, 1);
|
||||
test_identity(rotgen::matrix<T, 21, 3, O::value>::Identity(), 21, 3);
|
||||
test_identity(rotgen::matrix<T,rotgen::Dynamic,rotgen::Dynamic, O::value>::Identity(2, 7), 2, 7);
|
||||
test_identity(rotgen::matrix<T, 2, 2, O::value>::Identity(2, 2), 2, 2);
|
||||
test_identity(rotgen::matrix<T, 3,rotgen::Dynamic, O::value>::Identity(3, 3), 3, 3);
|
||||
test_identity(rotgen::matrix<T,rotgen::Dynamic, 11, O::value>::Identity(5, 11), 5, 11);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue