//================================================================================================== /* ROTGEN - Runtime Overlay for Eigen Copyright : CODE RECKONS SPDX-License-Identifier: BSL-1.0 */ //================================================================================================== #include "unit/tests.hpp" #include #include #include #include template void compare_reductions(const Block1& block, const Block2& ref) { constexpr double epsilon = 0.0001; TTS_RELATIVE_EQUAL(block.sum(), ref.sum(), epsilon); TTS_RELATIVE_EQUAL(block.prod(), ref.prod(), epsilon); TTS_RELATIVE_EQUAL(block.mean(), ref.mean(), epsilon); TTS_EQUAL(block.trace(), ref.trace()); TTS_EQUAL(block.minCoeff(), ref.minCoeff()); TTS_EQUAL(block.maxCoeff(), ref.maxCoeff()); std::ptrdiff_t row, col, ref_row, ref_col; TTS_EQUAL(block.minCoeff(&row, &col), ref.minCoeff(&ref_row, &ref_col)); TTS_EQUAL(row, ref_row); TTS_EQUAL(col, ref_col); TTS_EQUAL(block.maxCoeff(&row, &col), ref.maxCoeff(&ref_row, &ref_col)); TTS_EQUAL(row, ref_row); TTS_EQUAL(col, ref_col); } template void test_dynamic_block_reductions(rotgen::tests::matrix_block_test_case const& matrix_construct) { using EigenMatrix = Eigen::Matrix; MatrixType original(matrix_construct.rows, matrix_construct.cols); EigenMatrix ref(matrix_construct.rows, matrix_construct.cols); for (std::size_t r = 0; r < matrix_construct.rows; ++r) for (std::size_t c = 0; c < matrix_construct.cols; ++c) ref(r, c) = original(r,c) = static_cast(matrix_construct.init_fn(r, c)); std::vector, Eigen::Block>> test_cases { { rotgen::extract(original, matrix_construct.i0, matrix_construct.j0, matrix_construct.ni, matrix_construct.nj), ref.block(matrix_construct.i0, matrix_construct.j0, matrix_construct.ni, matrix_construct.nj) }, { rotgen::topLeftCorner(original, matrix_construct.ni, matrix_construct.nj), ref.topLeftCorner(matrix_construct.ni, matrix_construct.nj) }, { rotgen::topRightCorner(original, matrix_construct.ni, matrix_construct.nj), ref.topRightCorner(matrix_construct.ni, matrix_construct.nj) }, { rotgen::bottomLeftCorner(original, matrix_construct.ni, matrix_construct.nj), ref.bottomLeftCorner(matrix_construct.ni, matrix_construct.nj) }, { rotgen::bottomRightCorner(original, matrix_construct.ni, matrix_construct.nj), ref.bottomRightCorner(matrix_construct.ni, matrix_construct.nj) }, { rotgen::topRows(original, matrix_construct.ni), ref.topRows(matrix_construct.ni) }, { rotgen::middleRows(original, matrix_construct.i0, matrix_construct.ni), ref.middleRows(matrix_construct.i0, matrix_construct.ni) }, { rotgen::bottomRows(original, matrix_construct.ni), ref.bottomRows(matrix_construct.ni) }, }; for (const auto& [original_block, ref_block] : test_cases) compare_reductions(original_block, ref_block); } TTS_CASE_TPL("Test dynamic block reductions", rotgen::tests::types) ( tts::type< tts::types> ) { using mat_t = rotgen::matrix; std::vector> test_cases = { {6, 5, [](auto r, auto c) {return r + c; }, 1, 2, 3, 2}, {9, 11, [](auto r, auto c) {return r + c; }, 0, 1, 4, 9}, {3, 3, [](auto , auto ) {return 0.0; }, 1, 1, 1, 1}, {1, 4, [](auto r, auto c) {return -r -c*c - 1234; }, 0, 0, 1, 1}, {4, 1, [](auto , auto ) {return 7.0; }, 2, 0, 2, 1}, {1, 1, [](auto , auto ) {return 42.0; }, 0, 0, 1, 1}, {12, 13, [](auto r, auto c) {return std::sin(r + c); }, 2, 3, 4, 5 }, {4, 9, [](auto r, auto c) {return -1.5 * r + 2.56 * c; }, 0, 1, 2, 3 }, {2, 5, [](auto r, auto c) {return (r == c ? 1.0 : 0.0); }, 1, 1, 1, 1}, }; for (const auto& test_case : test_cases) test_dynamic_block_reductions(test_case); };