rotgen/test/unit/basic/norms.cpp
Karen Kaspar 114bc27901 Feat/non member functions
Co-authored-by: Karen <kkaspar@codereckons.com>
Co-authored-by: Joel FALCOU <jfalcou@codereckons.com>

See merge request oss/rotgen!7
2025-06-11 14:41:49 +02:00

47 lines
No EOL
1.9 KiB
C++

//==================================================================================================
/*
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>());
}
};