improve random_kernel test
All checks were successful
Linux arm64 / Build (push) Successful in 1m25s

This commit is contained in:
2024-03-14 21:54:13 +01:00
parent 13c9bc40db
commit 2724ca173d
3 changed files with 48 additions and 11 deletions

View File

@@ -72,6 +72,12 @@ class Matrix {
*/ */
void Augment(const Matrix& a_Right); 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 * \brief Retourne la sous-matrice spécifiée
* \param a_RawOrigin L'indice de la première ligne de la matrice à récupérer * \param a_RawOrigin L'indice de la première ligne de la matrice à récupérer

View File

@@ -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 { Matrix Matrix::operator*(const Matrix& a_Other) const {
if (m_Columns != a_Other.m_Raws) { assert(m_Columns == a_Other.m_Raws);
std::cerr << "Mutiplication impossible car la dimensions des matrices est incompatible" << std::endl;
return {};
}
Matrix result(m_Raws, a_Other.m_Columns); Matrix result(m_Raws, a_Other.m_Columns);
@@ -73,6 +70,14 @@ Matrix Matrix::RawVector(std::initializer_list<Element>&& a_Elements) {
return result; 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) { void Matrix::Augment(const Matrix& a_Right) {
assert(a_Right.m_Raws == m_Raws); assert(a_Right.m_Raws == m_Raws);
Matrix temp {m_Raws, m_Columns + a_Right.m_Columns}; 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 { bool Matrix::operator==(const Matrix& a_Other) const {
if (m_Raws != a_Other.m_Raws || m_Columns != a_Other.m_Columns) assert(m_Raws == a_Other.m_Raws && m_Columns == a_Other.m_Columns);
return false;
for (std::size_t i = 0; i < m_Raws; i++) { for (std::size_t i = 0; i < m_Raws; i++) {
for (std::size_t j = 0; j < m_Columns; j++) { for (std::size_t j = 0; j < m_Columns; j++) {

View File

@@ -6,6 +6,7 @@
#include <vector> #include <vector>
static constexpr int EXECUTION_COUNT = 100; static constexpr int EXECUTION_COUNT = 100;
static constexpr int KERNEL_CHECKS = 100;
static constexpr int MATRIX_MAX_SIZE = 100; static constexpr int MATRIX_MAX_SIZE = 100;
static const Solver solver; static const Solver solver;
@@ -14,8 +15,8 @@ static unsigned int GetRandomInt() {
return rand() % MATRIX_MAX_SIZE + 1; return rand() % MATRIX_MAX_SIZE + 1;
} }
static void Test() { static Matrix GetRandomMatrix(std::size_t a_Raw, std::size_t a_Column) {
Matrix matrix {GetRandomInt(), GetRandomInt()}; Matrix matrix {a_Raw, a_Column};
for (std::size_t i = 0; i < matrix.GetRawCount(); i++) { for (std::size_t i = 0; i < matrix.GetRawCount(); i++) {
for (std::size_t j = 0; j < matrix.GetColumnCount(); j++) { for (std::size_t j = 0; j < matrix.GetColumnCount(); j++) {
@@ -23,10 +24,36 @@ static void Test() {
} }
} }
Vect vect1 = solver.Kernel(matrix); return matrix;
Vect vect2 = solver.Kernel(vect1.GetLinearSystem()); }
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() { int main() {