From 2724ca173dd2f4f3db0e65bedcc1b7363742e3d6 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Thu, 14 Mar 2024 21:54:13 +0100 Subject: [PATCH] improve random_kernel test --- include/Matrix.h | 6 ++++++ src/Matrix.cpp | 16 ++++++++++------ test/test_random_kernel.cpp | 37 ++++++++++++++++++++++++++++++++----- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/include/Matrix.h b/include/Matrix.h index 8f142df..4bcd00a 100644 --- a/include/Matrix.h +++ b/include/Matrix.h @@ -72,6 +72,12 @@ class Matrix { */ void Augment(const Matrix& a_Right); + /** + * \brief Affecte tous les coefficients de la matrice à un élément + * \param a_Element L'élément à affecter + */ + void Fill(Element a_Element); + /** * \brief Retourne la sous-matrice spécifiée * \param a_RawOrigin L'indice de la première ligne de la matrice à récupérer diff --git a/src/Matrix.cpp b/src/Matrix.cpp index bc08484..becc942 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -18,10 +18,7 @@ Matrix::Matrix(std::size_t a_Raws, std::size_t a_Columns, std::initializer_list< } Matrix Matrix::operator*(const Matrix& a_Other) const { - if (m_Columns != a_Other.m_Raws) { - std::cerr << "Mutiplication impossible car la dimensions des matrices est incompatible" << std::endl; - return {}; - } + assert(m_Columns == a_Other.m_Raws); Matrix result(m_Raws, a_Other.m_Columns); @@ -73,6 +70,14 @@ Matrix Matrix::RawVector(std::initializer_list&& a_Elements) { return result; } +void Matrix::Fill(Element a_Element) { + for (std::size_t i = 0; i < m_Raws; i++) { + for (std::size_t j = 0; j < m_Columns; j++) { + at(i, j) = a_Element; + } + } +} + void Matrix::Augment(const Matrix& a_Right) { assert(a_Right.m_Raws == m_Raws); Matrix temp {m_Raws, m_Columns + a_Right.m_Columns}; @@ -121,8 +126,7 @@ Matrix Matrix::operator-(const Matrix& a_Other) const { } bool Matrix::operator==(const Matrix& a_Other) const { - if (m_Raws != a_Other.m_Raws || m_Columns != a_Other.m_Columns) - return false; + assert(m_Raws == a_Other.m_Raws && m_Columns == a_Other.m_Columns); for (std::size_t i = 0; i < m_Raws; i++) { for (std::size_t j = 0; j < m_Columns; j++) { diff --git a/test/test_random_kernel.cpp b/test/test_random_kernel.cpp index 2511b39..964ab23 100644 --- a/test/test_random_kernel.cpp +++ b/test/test_random_kernel.cpp @@ -6,6 +6,7 @@ #include static constexpr int EXECUTION_COUNT = 100; +static constexpr int KERNEL_CHECKS = 100; static constexpr int MATRIX_MAX_SIZE = 100; static const Solver solver; @@ -14,8 +15,8 @@ static unsigned int GetRandomInt() { return rand() % MATRIX_MAX_SIZE + 1; } -static void Test() { - Matrix matrix {GetRandomInt(), GetRandomInt()}; +static Matrix GetRandomMatrix(std::size_t a_Raw, std::size_t a_Column) { + Matrix matrix {a_Raw, a_Column}; for (std::size_t i = 0; i < matrix.GetRawCount(); i++) { for (std::size_t j = 0; j < matrix.GetColumnCount(); j++) { @@ -23,10 +24,36 @@ static void Test() { } } - Vect vect1 = solver.Kernel(matrix); - Vect vect2 = solver.Kernel(vect1.GetLinearSystem()); + return matrix; +} - assert(vect1 == vect2); +static void Test() { + Matrix matrix = GetRandomMatrix(GetRandomInt(), GetRandomInt()); + + for (std::size_t i = 0; i < matrix.GetRawCount(); i++) { + for (std::size_t j = 0; j < matrix.GetColumnCount(); j++) { + matrix.at(i, j) = GetRandomInt(); + } + } + + Vect kernel = solver.Kernel(matrix); + + Matrix nullVector {matrix.GetRawCount(), 1}; + nullVector.Fill(0.0); + + for (std::size_t i = 0; i < kernel.GetCardinal(); i++) { + assert(matrix * kernel.GetVector(i) == nullVector); + } + + for (std::size_t i = 0; i < KERNEL_CHECKS; i++) { + Matrix vector = GetRandomMatrix(kernel.GetDimension(), 1); + + assert(kernel.IsElementOf(vector) == (matrix * vector == nullVector)); + } + + Vect kernel2 = solver.Kernel(kernel.GetLinearSystem()); + + assert(kernel == kernel2); } int main() {