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
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);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue