rotgen/test/unit/matrix/inverse.cpp
Joel Falcou c400650f1a Implements rotgen::quaternion
Closes #19

Co-authored-by: Jules Pénuchot <jules@penuchot.com>
2025-11-09 19:07:20 +01:00

66 lines
1.9 KiB
C++

//==================================================================================================
/*
ROTGEN - Runtime Overlay for Eigen
Copyright : CODE RECKONS
SPDX-License-Identifier: BSL-1.0
*/
//==================================================================================================
#include <rotgen/rotgen.hpp>
#include "unit/common/references.hpp"
#include "unit/tests.hpp"
TTS_CASE_TPL("Test dynamic matrix inverse",
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 eps = std::numeric_limits<T>::epsilon();
for (int n = 2; n < 15; n *= 1.5)
{
auto input = 10 * rotgen::setRandom<mat_t>(n, n);
auto inv = rotgen::inverse(input);
auto rec = input * inv;
auto id = mat_t::Identity(rotgen::rows(rec), rotgen::cols(rec));
auto error = rec - id;
TTS_LESS_EQUAL(rotgen::maxCoeff(rotgen::abs(error)) / eps, 256.)
<< "Result:\n"
<< rec << "\n"
<< "Residuals:\n"
<< error << "\n";
}
};
template<typename T, typename O, int N> void check_static_inverse()
{
using mat_t = rotgen::matrix<T, N, N, O::value>;
auto eps = std::numeric_limits<T>::epsilon();
auto input = rotgen::setRandom<mat_t>();
auto inv = rotgen::inverse(input);
auto rec = input * inv;
auto id = mat_t::Identity(rotgen::rows(rec), rotgen::cols(rec));
auto error = rec - id;
TTS_LESS_EQUAL(rotgen::maxCoeff(rotgen::abs(error)) / eps, 256.)
<< "Result:\n"
<< rec << "\n"
<< "Residuals:\n"
<< error << "\n";
}
TTS_CASE_TPL("Test static matrix inverse",
rotgen::tests::types)<typename T, typename O>(
tts::type<tts::types<T, O>>)
{
check_static_inverse<T, O, 2>();
check_static_inverse<T, O, 3>();
check_static_inverse<T, O, 4>();
check_static_inverse<T, O, 6>();
check_static_inverse<T, O, 9>();
check_static_inverse<T, O, 13>();
};