From 23de24e9a17af9ee9c109826de371fa771595dc1 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 13 Feb 2024 11:38:52 +0100 Subject: [PATCH] add stuff --- src/Matrix.cpp | 32 ++++++++++++++++++++++++++------ src/Matrix.h | 14 ++++++++++++-- src/Vect.cpp | 20 ++++++++++++++++++++ src/Vect.h | 22 ++++++++++++++++++++++ test/mainTest.cpp | 1 + 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 src/Vect.cpp create mode 100644 src/Vect.h diff --git a/src/Matrix.cpp b/src/Matrix.cpp index 22a62a9..bae35f9 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -39,10 +40,6 @@ Matrix Matrix::operator*(const Matrix& other) const { return result; } -static bool IsEqualZero(long double var) { - return std::abs(var) < std::pow(10, -5); -} - void Matrix::Print() const { for (size_t i = 0; i < m_Lignes; ++i) { std::cout << "[ "; @@ -101,12 +98,14 @@ void Matrix::Load(const std::string& filename) { } } -void Matrix::Transpose() { +Matrix Matrix::Transpose() const { + Matrix result{m_Colonnes, m_Lignes}; for (std::size_t i = 0; i < m_Lignes; i++) { for (std::size_t j = i; j < m_Colonnes; j++) { - std::swap(at(i, j), at(j, i)); + result.at(j, i) = at(i, j); } } + return result; } void Matrix::Identity() { @@ -224,4 +223,25 @@ long double& Matrix::at(std::size_t ligne, std::size_t colonne) { long double Matrix::at(std::size_t ligne, std::size_t colonne) const { return m_Data[ligne * m_Lignes + colonne]; +} + +std::size_t Matrix::GetRawCount() const { + return m_Lignes; +} + +std::size_t Matrix::GetColumnCount() const { + return m_Colonnes; +} + +Matrix Matrix::SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne, std::size_t ligne, std::size_t colonne) const { + assert(m_Lignes >= ligne && m_Colonnes >= colonne); + Matrix result{ligne, colonne}; + + for (std::size_t i = 0; i < ligne; i++) { + for (std::size_t j = 0; j < colonne; j++) { + result.at(i, j) = at(i + origine_ligne, j + origine_colonne); + } + } + + return result; } \ No newline at end of file diff --git a/src/Matrix.h b/src/Matrix.h index 8ff0ec1..dcd8198 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include @@ -15,6 +17,9 @@ class Matrix { Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list&& initList); ~Matrix(); + std::size_t GetRawCount() const; + std::size_t GetColumnCount() const; + Matrix operator*(const Matrix& other) const; void GaussNonJordan(bool reduite); @@ -31,12 +36,14 @@ class Matrix { void Load(const std::string& filename); - void Transpose(); + Matrix Transpose() const; void Identity(); bool IsInversed() const; + Matrix SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne, std::size_t ligne, std::size_t colonne) const; + bool operator==(const Matrix& other) const; long double& operator[](std::size_t indice); @@ -46,4 +53,7 @@ class Matrix { long double at(std::size_t ligne, std::size_t colonne) const; }; -static bool IsEqualZero(long double var); \ No newline at end of file +template +bool IsEqualZero(T var) { + return std::abs(var) < std::pow(10, -5); +} \ No newline at end of file diff --git a/src/Vect.cpp b/src/Vect.cpp new file mode 100644 index 0000000..f4be1ea --- /dev/null +++ b/src/Vect.cpp @@ -0,0 +1,20 @@ +#include "Vect.h" + +Vect::Vect(const Matrix& mat) { + for (std::size_t i = 0; i < mat.GetColumnCount(); i++) { + std::size_t j; + for (j = 0; j < mat.GetRawCount(); j++) { + if(!IsEqualZero(mat.at(i, j))) + break; + } + if (j == mat.GetRawCount()) { + m_Data = mat.SubMatrix(0, 0, mat.GetRawCount(), j); + } + } + m_Data = mat; +} + +// TODO +void Vect::Print() const { + +} \ No newline at end of file diff --git a/src/Vect.h b/src/Vect.h new file mode 100644 index 0000000..2cad493 --- /dev/null +++ b/src/Vect.h @@ -0,0 +1,22 @@ +#pragma once + +#include "Matrix.h" + +// espace vectoriel +class Vect { + private: + Matrix m_Data{0, 0}; + + public: + /** + * \brief Construit une base d'un espace vectoriel à partir des colonnes d'une matrice. + * Ne prend pas en compte les colonnes de 0 + * \param mat Une matrice échelonnée. + */ + Vect(const Matrix& mat); + + /** + * \brief Affiche la base de l'espace vectoriel dans la console + */ + void Print() const; +}; \ No newline at end of file diff --git a/test/mainTest.cpp b/test/mainTest.cpp index 1748358..43efd2a 100644 --- a/test/mainTest.cpp +++ b/test/mainTest.cpp @@ -4,6 +4,7 @@ #ifdef NDEBUG #error "Il faut être en debug mode ! xmake f -m debug" #endif + struct Test{ Matrix mat; Matrix res;