[ARITHMETIC][REDUCTION OPERATIONS] implemented and added tests for all reduction operations
This commit is contained in:
parent
9fc9b94be7
commit
a0a5a197e8
3 changed files with 80 additions and 0 deletions
|
|
@ -8,6 +8,7 @@
|
|||
#define TTS_MAIN
|
||||
#include <rotgen/matrix.hpp>
|
||||
#include "tts.hpp"
|
||||
#include <Eigen/Dense>
|
||||
|
||||
template <typename MatrixType>
|
||||
struct MatrixDescriptor
|
||||
|
|
@ -44,6 +45,46 @@ void test_matrix_unary_ops(std::size_t rows, std::size_t cols,
|
|||
TTS_EQUAL(original, transposed_matrix);
|
||||
}
|
||||
|
||||
template <typename MatrixType>
|
||||
void test_matrix_scalar_reductions(std::size_t rows, std::size_t cols,
|
||||
const std::function<void(MatrixType&, std::size_t, std::size_t)>& init_fn)
|
||||
{
|
||||
using EigenMatrix = Eigen::Matrix<double, 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)
|
||||
{
|
||||
init_fn(original, r, c);
|
||||
ref(r, c) = original(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("Matrix unary operations: transpose, adjoint, conjugate")
|
||||
{
|
||||
std::vector<MatrixDescriptor<rotgen::matrix<double>>> test_matrices = {
|
||||
|
|
@ -61,3 +102,20 @@ TTS_CASE("Matrix unary operations: transpose, adjoint, conjugate")
|
|||
test_matrix_unary_ops<rotgen::matrix<double>>(desc.rows, desc.cols, desc.init_fn);
|
||||
};
|
||||
|
||||
TTS_CASE("Basic arithmetic reduction operations")
|
||||
{
|
||||
std::vector<MatrixDescriptor<rotgen::matrix<double>>> test_matrices = {
|
||||
{3, 3, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = r + c; }},
|
||||
{3, 3, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = 0.0; }},
|
||||
{2, 4, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = -r -c*c - 1234; }},
|
||||
{4, 4, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = 7.0; }},
|
||||
{1, 1, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = 42.0; }},
|
||||
{4, 2, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = std::sin(r + c); }},
|
||||
{1, 5, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = -1.5 * r + 2.56 * c; }},
|
||||
{5, 7, [](auto& m, std::size_t r, std::size_t c) { m(r, c) = (r == c ? 1.0 : 0.0); }},
|
||||
};
|
||||
|
||||
for (const auto& [rows, cols, init_fn] : test_matrices)
|
||||
test_matrix_scalar_reductions<rotgen::matrix<double>>(rows, cols, init_fn);
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue