From 8c96ca43406df9a7a957268c8ccaf48655e14b98 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 10 Feb 2024 12:18:34 +0100 Subject: [PATCH 01/29] remove useless dimension --- src/Matrix.cpp | 9 +++++---- src/Matrix.h | 1 - 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Matrix.cpp b/src/Matrix.cpp index 27341e2..535b7f9 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -9,14 +9,15 @@ Matrix::Matrix(const std::string& fileNameInput) { Load(fileNameInput); } -Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Lignes(lignes), m_Colonnes(colonnes), m_Dimension(lignes * colonnes) { - m_Data.resize(m_Dimension); +Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Lignes(lignes), m_Colonnes(colonnes) { + m_Data.resize(m_Lignes * m_Colonnes); } Matrix::Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list&& initList) : - m_Lignes(lignes), m_Colonnes(colonnes), m_Dimension(lignes * colonnes) { + m_Lignes(lignes), m_Colonnes(colonnes) { m_Data = initList; - m_Data.resize(m_Dimension); + m_Data.resize(m_Lignes * m_Colonnes); } + Matrix::~Matrix() {} Matrix Matrix::operator*(const Matrix& other) const { diff --git a/src/Matrix.h b/src/Matrix.h index ad80018..fe397e8 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -7,7 +7,6 @@ class Matrix { private: std::size_t m_Lignes; std::size_t m_Colonnes; - std::size_t m_Dimension; std::vector m_Data; public: From 21a7268cbcab3894b42b1e0101c12dfc5865b60c Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 10 Feb 2024 14:31:54 +0100 Subject: [PATCH 02/29] add actions --- .gitea/workflows/ubuntu.yaml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .gitea/workflows/ubuntu.yaml diff --git a/.gitea/workflows/ubuntu.yaml b/.gitea/workflows/ubuntu.yaml new file mode 100644 index 0000000..308f361 --- /dev/null +++ b/.gitea/workflows/ubuntu.yaml @@ -0,0 +1,27 @@ +name: Linux arm64 +run-name: Build And Test + +on: [push] + +jobs: + Build: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v3 + + - name: Prepare XMake + uses: xmake-io/github-action-setup-xmake@v1 + with: + xmake-version: branch@master + actions-cache-folder: '.xmake-cache' + actions-cache-key: 'ubuntu' + + - name: XMake config + run: xmake f -p linux -y --root + + - name: Build + run: xmake --root + + - name: Test + run: xmake test --root From 150e67e30bd9a2f36c88a72f9b8e9837bb4e3907 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 10 Feb 2024 14:54:32 +0100 Subject: [PATCH 03/29] update readme --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1d1e02d..aa2b67b 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,16 @@ # Cahier des charges -![imagecdc](PeiP2_MAM-INFO_projet_02.jpg) \ No newline at end of file +![imagecdc](PeiP2_MAM-INFO_projet_02.jpg) + +# Build + +``` +xmake +``` + +# Run + +``` +xmake run +``` \ No newline at end of file From 4c217a4b20ab57c43e0cb0e57dc939ff861962f3 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 10 Feb 2024 16:58:44 +0100 Subject: [PATCH 04/29] add basic test --- src/Matrix.cpp | 14 ++++++++++++++ src/Matrix.h | 2 ++ test/mainTest.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ xmake.lua | 12 ++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 test/mainTest.cpp diff --git a/src/Matrix.cpp b/src/Matrix.cpp index 535b7f9..22a62a9 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -134,6 +134,20 @@ bool Matrix::IsInversed() const { return true; } +bool Matrix::operator==(const Matrix& other) const { + if (m_Lignes != other.m_Lignes || m_Colonnes != other.m_Colonnes) + return false; + + for (std::size_t i = 0; i < m_Lignes; i++) { + for (std::size_t j = 0; j < m_Colonnes; j++) { + if (!IsEqualZero(at(i, j) - other.at(i, j))) + return false; + } + } + + return true; +} + void Matrix::GaussNonJordan(bool reduite) { int r = -1; for (std::size_t j = 0; j < m_Colonnes; j++) { diff --git a/src/Matrix.h b/src/Matrix.h index fe397e8..8ff0ec1 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -37,6 +37,8 @@ class Matrix { bool IsInversed() const; + bool operator==(const Matrix& other) const; + long double& operator[](std::size_t indice); long double& at(std::size_t ligne, std::size_t colonne); diff --git a/test/mainTest.cpp b/test/mainTest.cpp new file mode 100644 index 0000000..1748358 --- /dev/null +++ b/test/mainTest.cpp @@ -0,0 +1,45 @@ +#include "Matrix.h" +#include + +#ifdef NDEBUG +#error "Il faut être en debug mode ! xmake f -m debug" +#endif +struct Test{ + Matrix mat; + Matrix res; +}; + +static const std::vector TEST_MATRICES = { + // test 1 + {{3, 3, { + 1, 2, 3, + 4, 5, 6, + 7, 8, 9, + }}, {3, 3, { + 1, 0, -1, + 0, 1, 2, + 0, 0, 0, + }}}, + // test 2 + {{3, 3, { + 4, 5, 6, + 1, 2, 3, + 7, 8, 9, + }}, {3, 3, { + 1, 0, -1, + 0, 1, 2, + 0, 0, 0, + }}} +}; + +void test() { + for (Test test : TEST_MATRICES) { + test.mat.GaussJordan(true); + assert(test.mat == test.res); + } +} + +int main(int argc, char** argv) { + test(); + return 0; +} diff --git a/xmake.lua b/xmake.lua index 769129f..0cd4b3f 100644 --- a/xmake.lua +++ b/xmake.lua @@ -6,6 +6,18 @@ target("Pivot") set_rundir("$(projectdir)/matricies") set_languages("c++17") + + + + +target("PivotTest") + set_kind("binary") + add_files("test/*.cpp", "src/Matrix.cpp") + add_includedirs("src") + set_default(false) + add_tests("compile_and_run") + set_rundir("$(projectdir)/matricies") + -- -- If you want to known more usage about xmake, please see https://xmake.io -- From a7a2986336b8759b6f17c388f0b3d46bf05bc5c4 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 10 Feb 2024 16:59:33 +0100 Subject: [PATCH 05/29] ignore .vscode folder --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1521057..f928f14 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ build/ # MacOS Cache .DS_Store - +# VsCode +.vscode From 820308b6c76d1832c4165247ceaa75ebecec7a3b Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 10 Feb 2024 17:37:00 +0100 Subject: [PATCH 06/29] fix action --- .gitea/workflows/ubuntu.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitea/workflows/ubuntu.yaml b/.gitea/workflows/ubuntu.yaml index 308f361..e4d31e4 100644 --- a/.gitea/workflows/ubuntu.yaml +++ b/.gitea/workflows/ubuntu.yaml @@ -24,4 +24,6 @@ jobs: run: xmake --root - name: Test - run: xmake test --root + run: | + xmake f -m debug --root + xmake test --root From 23de24e9a17af9ee9c109826de371fa771595dc1 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Tue, 13 Feb 2024 11:38:52 +0100 Subject: [PATCH 07/29] 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; From 715bc843b2b9fe57a83dd591f2cf516f721a242e Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 14 Feb 2024 20:22:14 +0100 Subject: [PATCH 08/29] O_o --- src/Matrix.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Matrix.cpp b/src/Matrix.cpp index bae35f9..5c11454 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -218,11 +218,11 @@ long double& Matrix::operator[](std::size_t indice) { } long double& Matrix::at(std::size_t ligne, std::size_t colonne) { - return m_Data[ligne * m_Lignes + colonne]; + return m_Data[ligne * m_Colonnes + colonne]; } long double Matrix::at(std::size_t ligne, std::size_t colonne) const { - return m_Data[ligne * m_Lignes + colonne]; + return m_Data[ligne * m_Colonnes + colonne]; } std::size_t Matrix::GetRawCount() const { From b90195cac1b9b8c7c5a1ce9b7d4316311b6c062a Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 14 Feb 2024 20:29:02 +0100 Subject: [PATCH 09/29] change transpose signature --- src/Matrix.cpp | 6 +++--- src/Matrix.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Matrix.cpp b/src/Matrix.cpp index 5c11454..0a7908c 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -98,14 +98,14 @@ void Matrix::Load(const std::string& filename) { } } -Matrix Matrix::Transpose() const { +void Matrix::Transpose() { 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++) { + for (std::size_t j = 0; j < m_Colonnes; j++) { result.at(j, i) = at(i, j); } } - return result; + *this = result; } void Matrix::Identity() { diff --git a/src/Matrix.h b/src/Matrix.h index dcd8198..3bb3f39 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -36,7 +36,7 @@ class Matrix { void Load(const std::string& filename); - Matrix Transpose() const; + void Transpose(); void Identity(); From d91d35a3af060f5eba28fa2d60f5c7894b115c55 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 14 Feb 2024 20:29:25 +0100 Subject: [PATCH 10/29] implement Vect --- src/Vect.cpp | 28 ++++++++++++++++++++-------- src/Vect.h | 9 ++++++--- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/Vect.cpp b/src/Vect.cpp index f4be1ea..d49695c 100644 --- a/src/Vect.cpp +++ b/src/Vect.cpp @@ -1,20 +1,32 @@ #include "Vect.h" +#include -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))) +Vect::Vect(const Matrix& mat) : m_Data(mat) { + Simplify(); +} + +void Vect::Simplify() { + Matrix mat = m_Data; + for (std::size_t j = 0; j < mat.GetColumnCount(); j++) { + std::size_t i; + for (i = 0; i < mat.GetRawCount(); i++) { + if (!IsEqualZero(mat.at(i, j))) break; } - if (j == mat.GetRawCount()) { + if (i == mat.GetRawCount()) { m_Data = mat.SubMatrix(0, 0, mat.GetRawCount(), j); + return; } } m_Data = mat; } -// TODO void Vect::Print() const { - + std::cout << "Espace vectoriel de dimension " << m_Data.GetColumnCount() << " de base :\n\n"; + for (std::size_t i = 0; i < m_Data.GetRawCount(); i++) { + for (std::size_t j = 0; j < m_Data.GetColumnCount(); j++) { + printf("[ %.3f ]\t", static_cast(m_Data.at(i, j))); + } + std::cout << "\n"; + } } \ No newline at end of file diff --git a/src/Vect.h b/src/Vect.h index 2cad493..0a5782d 100644 --- a/src/Vect.h +++ b/src/Vect.h @@ -5,7 +5,7 @@ // espace vectoriel class Vect { private: - Matrix m_Data{0, 0}; + Matrix m_Data; public: /** @@ -16,7 +16,10 @@ class Vect { Vect(const Matrix& mat); /** - * \brief Affiche la base de l'espace vectoriel dans la console - */ + * \brief Affiche la base de l'espace vectoriel dans la console + */ void Print() const; + + private: + void Simplify(); }; \ No newline at end of file From a016163b86960df1611c8273bd95692fe8e71258 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 14 Feb 2024 20:30:16 +0100 Subject: [PATCH 11/29] more example --- src/main.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 1e71236..8d66b28 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include "Matrix.h" +#include "Vect.h" #include void test() { @@ -15,6 +15,22 @@ void test() { std::cout << "<<\nTransposée:\n"; mat.Print(); // mat.Save("matrice4x4echelonne.mat"); + + Matrix mat2 {"matrice4x4.mat"}; + mat2.Print(); + + mat2.Transpose(); + std::cout << "Transposée : \n"; + mat2.Print(); + + mat2.GaussJordan(true); + mat2.Transpose(); + + std::cout << "Echelonnée en colonne :\n"; + mat2.Print(); + + Vect sol {mat2}; + sol.Print(); } void prompt() { From cf64e4d33ce1e94d56c68cde76056fbd83b4de57 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 14 Feb 2024 21:30:41 +0100 Subject: [PATCH 12/29] improve identity --- src/Matrix.cpp | 13 ++++++++----- src/Matrix.h | 2 ++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Matrix.cpp b/src/Matrix.cpp index 0a7908c..8b3a7c9 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -109,17 +109,20 @@ void Matrix::Transpose() { } void Matrix::Identity() { + assert(m_Lignes == m_Colonnes); for (std::size_t i = 0; i < m_Lignes; i++) { for (std::size_t j = i; j < m_Colonnes; j++) { - if (i != j) { - at(i, j) = 0; - } else { - at(i, j) = 1; - } + at(i, j) = i == j; } } } +Matrix Matrix::Identity(std::size_t taille) { + Matrix id{taille, taille}; + id.Identity(); + return id; +} + bool Matrix::IsInversed() const { for (std::size_t i = 0; i < m_Lignes; ++i) { std::size_t j; diff --git a/src/Matrix.h b/src/Matrix.h index 3bb3f39..5122715 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -40,6 +40,8 @@ class Matrix { void Identity(); + static Matrix Identity(std::size_t taille); + bool IsInversed() const; Matrix SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne, std::size_t ligne, std::size_t colonne) const; From 8ef738db5520a3228c6c32c8a91f12a5a22b0990 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 14 Feb 2024 21:31:05 +0100 Subject: [PATCH 13/29] matrice augmenter --- src/Matrix.cpp | 19 +++++++++++++++++++ src/Matrix.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/Matrix.cpp b/src/Matrix.cpp index 8b3a7c9..a30e09b 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -136,6 +136,25 @@ bool Matrix::IsInversed() const { return true; } +void Matrix::Augmenter(const Matrix& droite) { + assert(droite.m_Lignes == m_Lignes); + Matrix temp{m_Lignes, m_Colonnes + droite.m_Colonnes}; + + for (std::size_t i = 0; i < m_Lignes; i++) { + for (std::size_t j = 0; j < m_Colonnes; j++) { + temp.at(i, j) = at(i, j); + } + } + + for (std::size_t i = 0; i < m_Lignes; i++) { + for (std::size_t j = 0; j < droite.m_Colonnes; j++) { + temp.at(i, j + m_Colonnes) = droite.at(i, j); + } + } + + *this = temp; +} + bool Matrix::operator==(const Matrix& other) const { if (m_Lignes != other.m_Lignes || m_Colonnes != other.m_Colonnes) return false; diff --git a/src/Matrix.h b/src/Matrix.h index 5122715..a858f89 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -44,6 +44,8 @@ class Matrix { bool IsInversed() const; + void Augmenter(const Matrix& droite); + 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; From e1f501237da9b4b21282266b24573a0b1a2fe4d0 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 14 Feb 2024 21:31:23 +0100 Subject: [PATCH 14/29] vect dimension --- src/Vect.cpp | 6 +++++- src/Vect.h | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Vect.cpp b/src/Vect.cpp index d49695c..1dad13f 100644 --- a/src/Vect.cpp +++ b/src/Vect.cpp @@ -22,11 +22,15 @@ void Vect::Simplify() { } void Vect::Print() const { - std::cout << "Espace vectoriel de dimension " << m_Data.GetColumnCount() << " de base :\n\n"; + std::cout << "Espace vectoriel de dimension " << GetDimension() << " de base :\n\n"; for (std::size_t i = 0; i < m_Data.GetRawCount(); i++) { for (std::size_t j = 0; j < m_Data.GetColumnCount(); j++) { printf("[ %.3f ]\t", static_cast(m_Data.at(i, j))); } std::cout << "\n"; } +} + +std::size_t Vect::GetDimension() const { + return m_Data.GetColumnCount(); } \ No newline at end of file diff --git a/src/Vect.h b/src/Vect.h index 0a5782d..a99c3ba 100644 --- a/src/Vect.h +++ b/src/Vect.h @@ -20,6 +20,8 @@ class Vect { */ void Print() const; + std::size_t GetDimension() const; + private: void Simplify(); }; \ No newline at end of file From 4ffeea890028784a1d7685577afe9d855d801703 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 14 Feb 2024 21:35:26 +0100 Subject: [PATCH 15/29] image et noyau --- src/Solver.cpp | 31 +++++++++++++++++++++++++++++++ src/Solver.h | 16 ++++++++++++++++ src/main.cpp | 26 ++++++++++---------------- 3 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 src/Solver.cpp create mode 100644 src/Solver.h diff --git a/src/Solver.cpp b/src/Solver.cpp new file mode 100644 index 0000000..5e1634d --- /dev/null +++ b/src/Solver.cpp @@ -0,0 +1,31 @@ +#include "Solver.h" + +Solver::Solver(const Matrix& mat) : m_Matrix(mat) {} + +Vect Solver::Image() const { + Matrix result = m_Matrix; + result.Transpose(); + result.GaussJordan(true); + result.Transpose(); + return {result}; +} + +// https://en.wikipedia.org/wiki/Kernel_(linear_algebra)#Computation_by_Gaussian_elimination +Vect Solver::Noyau() const { + Matrix result = m_Matrix; + result.Transpose(); + result.Augmenter(Matrix::Identity(result.GetRawCount())); + result.GaussJordan(true); + result.Transpose(); + + // nombre de colonnes non nulles + std::size_t origine_colonne = Vect(result.SubMatrix(0, 0, m_Matrix.GetRawCount(), m_Matrix.GetColumnCount())).GetDimension(); + + return {result.SubMatrix(m_Matrix.GetRawCount(), origine_colonne, result.GetRawCount() - m_Matrix.GetRawCount(), + result.GetColumnCount() - origine_colonne)}; +} + +std::size_t Solver::Rang() const { + Vect image = Image(); + return image.GetDimension(); +} diff --git a/src/Solver.h b/src/Solver.h new file mode 100644 index 0000000..aded7b5 --- /dev/null +++ b/src/Solver.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Vect.h" + +class Solver { + private: + Matrix m_Matrix; + public: + Solver(const Matrix& mat); + ~Solver() {} + + Vect Image() const; + Vect Noyau() const; + + std::size_t Rang() const; +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8d66b28..30091d6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,8 @@ -#include "Vect.h" +#include "Solver.h" #include void test() { - Matrix mat{"matrice4x4.mat"}; + /* Matrix mat{"matrice4x4.mat"}; mat.Print(); // mat.Save("matrice3x3.mat"); std::cout << "sdfdjiofoseifheoiefhoig\n"; @@ -14,23 +14,17 @@ void test() { mat.Transpose(); std::cout << "<<\nTransposée:\n"; mat.Print(); - // mat.Save("matrice4x4echelonne.mat"); + // mat.Save("matrice4x4echelonne.mat"); */ - Matrix mat2 {"matrice4x4.mat"}; - mat2.Print(); - - mat2.Transpose(); - std::cout << "Transposée : \n"; - mat2.Print(); - - mat2.GaussJordan(true); - mat2.Transpose(); - - std::cout << "Echelonnée en colonne :\n"; + Matrix mat2{"matrice4x4.mat"}; mat2.Print(); - Vect sol {mat2}; - sol.Print(); + Solver solver{mat2}; + + std::cout << "\tImage :\n"; + solver.Image().Print(); + std::cout << "\tNoyau :\n"; + solver.Noyau().Print(); } void prompt() { From a1d74ec126a4441f00720af3faa8c379ff2400a9 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 14 Feb 2024 22:22:59 +0100 Subject: [PATCH 16/29] vectorial space to linear equation --- src/Matrix.cpp | 4 ++-- src/Vect.cpp | 12 ++++++++++++ src/Vect.h | 2 ++ src/main.cpp | 11 +++++++++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Matrix.cpp b/src/Matrix.cpp index a30e09b..465bc55 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -1,11 +1,11 @@ +#include "Matrix.h" + #include #include #include #include #include -#include "Matrix.h" - Matrix::Matrix(const std::string& fileNameInput) { Load(fileNameInput); } diff --git a/src/Vect.cpp b/src/Vect.cpp index 1dad13f..5930737 100644 --- a/src/Vect.cpp +++ b/src/Vect.cpp @@ -1,4 +1,6 @@ #include "Vect.h" + +#include "Solver.h" #include Vect::Vect(const Matrix& mat) : m_Data(mat) { @@ -21,6 +23,16 @@ void Vect::Simplify() { m_Data = mat; } +Matrix Vect::GetLinearSystem() const { + Matrix vect = m_Data; + vect.Transpose(); + + Solver solver {vect}; + vect = solver.Noyau().m_Data; + vect.Transpose(); + return vect; +} + void Vect::Print() const { std::cout << "Espace vectoriel de dimension " << GetDimension() << " de base :\n\n"; for (std::size_t i = 0; i < m_Data.GetRawCount(); i++) { diff --git a/src/Vect.h b/src/Vect.h index a99c3ba..5f89f02 100644 --- a/src/Vect.h +++ b/src/Vect.h @@ -22,6 +22,8 @@ class Vect { std::size_t GetDimension() const; + Matrix GetLinearSystem() const; + private: void Simplify(); }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 30091d6..39d5061 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,10 +21,17 @@ void test() { Solver solver{mat2}; + Vect image = solver.Image(); + Vect noyau = solver.Noyau(); + std::cout << "\tImage :\n"; - solver.Image().Print(); + image.Print(); + std::cout << "Système :\n"; + image.GetLinearSystem().Print(); std::cout << "\tNoyau :\n"; - solver.Noyau().Print(); + noyau.Print(); + std::cout << "Système :\n"; + noyau.GetLinearSystem().Print(); } void prompt() { From c2fd2805fac5c777b1d2e53b19f22519fafc6313 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Fri, 16 Feb 2024 10:02:27 +0100 Subject: [PATCH 17/29] matrix stream operators --- src/Matrix.cpp | 40 +++++++++++++++++++++++++--------------- src/Matrix.h | 3 +++ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/Matrix.cpp b/src/Matrix.cpp index 465bc55..a64fb97 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -74,13 +74,7 @@ void Matrix::Save(const std::string& fileName) { std::cerr << "Impossible de sauvegarder la matrice !\n"; return; } - out << m_Lignes << " " << m_Colonnes << "\n"; - for (std::size_t i = 0; i < m_Lignes; i++) { - for (std::size_t j = 0; j < m_Colonnes; j++) { - out << at(i, j) << " "; - } - out << "\n"; - } + out << *this; } void Matrix::Load(const std::string& filename) { @@ -89,13 +83,7 @@ void Matrix::Load(const std::string& filename) { std::cerr << "Impossible de charger la matrice !\n"; return; } - in >> m_Lignes >> m_Colonnes; - m_Data.resize(m_Lignes * m_Colonnes); - for (std::size_t i = 0; i < m_Lignes; i++) { - for (std::size_t j = 0; j < m_Colonnes; j++) { - in >> at(i, j); - } - } + in >> *this; } void Matrix::Transpose() { @@ -266,4 +254,26 @@ Matrix Matrix::SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne, } return result; -} \ No newline at end of file +} + +std::ostream& operator<<(std::ostream& stream, const Matrix& mat) { + stream << mat.m_Lignes << " " << mat.m_Colonnes << "\n"; + for (std::size_t i = 0; i < mat.m_Lignes; i++) { + for (std::size_t j = 0; j < mat.m_Colonnes; j++) { + stream << mat.at(i, j) << " "; + } + stream << "\n"; + } + return stream; +} + +std::istream& operator>>(std::istream& stream, Matrix& mat) { + stream >> mat.m_Lignes >> mat.m_Colonnes; + mat.m_Data.resize(mat.m_Lignes * mat.m_Colonnes); + for (std::size_t i = 0; i < mat.m_Lignes; i++) { + for (std::size_t j = 0; j < mat.m_Colonnes; j++) { + stream >> mat.at(i, j); + } + } + return stream; +} diff --git a/src/Matrix.h b/src/Matrix.h index a858f89..4e61977 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -55,6 +55,9 @@ class Matrix { long double& at(std::size_t ligne, std::size_t colonne); long double at(std::size_t ligne, std::size_t colonne) const; + + friend std::ostream& operator<<(std::ostream& stream, const Matrix& mat); + friend std::istream& operator>>(std::istream& stream, Matrix& mat); }; template From 46dcad1457f97a94c7908da3e82c0a5faaa0d4f5 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Fri, 16 Feb 2024 10:39:42 +0100 Subject: [PATCH 18/29] equality of vectorial spaces --- src/Solver.cpp | 4 ++-- src/Vect.cpp | 36 +++++++++++++++++++++++++++++++++--- src/Vect.h | 10 ++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/Solver.cpp b/src/Solver.cpp index 5e1634d..336711d 100644 --- a/src/Solver.cpp +++ b/src/Solver.cpp @@ -19,7 +19,7 @@ Vect Solver::Noyau() const { result.Transpose(); // nombre de colonnes non nulles - std::size_t origine_colonne = Vect(result.SubMatrix(0, 0, m_Matrix.GetRawCount(), m_Matrix.GetColumnCount())).GetDimension(); + std::size_t origine_colonne = Vect(result.SubMatrix(0, 0, m_Matrix.GetRawCount(), m_Matrix.GetColumnCount())).GetCardinal(); return {result.SubMatrix(m_Matrix.GetRawCount(), origine_colonne, result.GetRawCount() - m_Matrix.GetRawCount(), result.GetColumnCount() - origine_colonne)}; @@ -27,5 +27,5 @@ Vect Solver::Noyau() const { std::size_t Solver::Rang() const { Vect image = Image(); - return image.GetDimension(); + return image.GetCardinal(); } diff --git a/src/Vect.cpp b/src/Vect.cpp index 5930737..930a92e 100644 --- a/src/Vect.cpp +++ b/src/Vect.cpp @@ -23,18 +23,48 @@ void Vect::Simplify() { m_Data = mat; } +std::size_t Vect::GetCardinal() const { + return m_Data.GetColumnCount(); +} + +bool Vect::operator==(const Vect& other) const { + if (GetDimension() != other.GetDimension() || GetCardinal() != other.GetCardinal()) + return false; + + // on vérifie si chaque vecteur de la deuxième base appartient à la première base + for (std::size_t i = 0; i < GetCardinal(); i++) { + Vect base = *this; + base.AddVector(other.m_Data.SubMatrix(0, i, GetDimension(), 1)); + if (base.GetCardinal() != GetCardinal()) + return false; + } + return true; +} + +void Vect::AddVector(const Matrix& mat) { + m_Data.Augmenter(mat); + m_Data.Transpose(); + m_Data.GaussNonJordan(false); + m_Data.Transpose(); + Simplify(); +} + +bool Vect::operator!=(const Vect& other) const { + return !(*this == other); +} + Matrix Vect::GetLinearSystem() const { Matrix vect = m_Data; vect.Transpose(); - Solver solver {vect}; + Solver solver{vect}; vect = solver.Noyau().m_Data; vect.Transpose(); return vect; } void Vect::Print() const { - std::cout << "Espace vectoriel de dimension " << GetDimension() << " de base :\n\n"; + std::cout << "Espace vectoriel de dimension " << GetCardinal() << " de base :\n\n"; for (std::size_t i = 0; i < m_Data.GetRawCount(); i++) { for (std::size_t j = 0; j < m_Data.GetColumnCount(); j++) { printf("[ %.3f ]\t", static_cast(m_Data.at(i, j))); @@ -44,5 +74,5 @@ void Vect::Print() const { } std::size_t Vect::GetDimension() const { - return m_Data.GetColumnCount(); + return m_Data.GetRawCount(); } \ No newline at end of file diff --git a/src/Vect.h b/src/Vect.h index 5f89f02..6d5c759 100644 --- a/src/Vect.h +++ b/src/Vect.h @@ -21,9 +21,19 @@ class Vect { void Print() const; std::size_t GetDimension() const; + std::size_t GetCardinal() const; Matrix GetLinearSystem() const; + /** + * \brief Concatène la base actuelle avec un nouveau vecteur + * \param mat Une matrice colonne de taille GetDimension() + */ + void AddVector(const Matrix& mat); + + bool operator==(const Vect& other) const; + bool operator!=(const Vect& other) const; + private: void Simplify(); }; \ No newline at end of file From d156602da7c5327a975ad854f9f95d49d569e544 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Fri, 16 Feb 2024 10:40:26 +0100 Subject: [PATCH 19/29] improve tests --- test/{mainTest.cpp => test_jordan.cpp} | 0 test/test_vect.cpp | 31 ++++++++++++++++++++ xmake.lua | 39 ++++++++++++++++++++------ 3 files changed, 61 insertions(+), 9 deletions(-) rename test/{mainTest.cpp => test_jordan.cpp} (100%) create mode 100644 test/test_vect.cpp diff --git a/test/mainTest.cpp b/test/test_jordan.cpp similarity index 100% rename from test/mainTest.cpp rename to test/test_jordan.cpp diff --git a/test/test_vect.cpp b/test/test_vect.cpp new file mode 100644 index 0000000..66a261b --- /dev/null +++ b/test/test_vect.cpp @@ -0,0 +1,31 @@ +#include "Vect.h" +#include + +int main() { + Vect vect1 {{3, 2, { + 1, 2, + 3, 4, + 5, 6, + }}}; + Vect vect2 {{3, 2, { + 1, 0, + 0, 0, + 0, 1, + }}}; + Vect vect3 {{3, 2, { + 1, 3, + 3, 7, + 5, 11, + }}}; + Vect vect4 {{3, 2, { + 1, 0, + 0, 0, + 1, 11, + }}}; + assert(vect1 == vect3); + assert(vect2 == vect4); + assert(vect1 != vect2); + assert(vect2 != vect3); + assert(vect3 != vect4); + return 0; +} \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 0cd4b3f..1744eb4 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,22 +1,43 @@ add_rules("mode.debug", "mode.release") +set_languages("c++17") + +-- Solver Library target("Pivot") - set_kind("binary") + set_kind("static") add_files("src/*.cpp") + remove_files("src/main.cpp") + + + + + +-- Solver Main +target("PivotMain") set_rundir("$(projectdir)/matricies") - set_languages("c++17") + add_files("src/main.cpp") + add_deps("Pivot") + set_default(true) -target("PivotTest") - set_kind("binary") - add_files("test/*.cpp", "src/Matrix.cpp") - add_includedirs("src") - set_default(false) - add_tests("compile_and_run") - set_rundir("$(projectdir)/matricies") +-- Solver tests +for _, file in ipairs(os.files("test/test_*.cpp")) do + local name = path.basename(file) + target(name) + set_kind("binary") + add_files("test/" .. name .. ".cpp") + set_rundir("$(projectdir)/matricies") + add_includedirs("src") + + set_default(false) + + add_deps("Pivot") + + add_tests("compile_and_run") +end -- -- If you want to known more usage about xmake, please see https://xmake.io From 88cc5290095c2a0108f162d03496bca19aeb8e9e Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Fri, 16 Feb 2024 11:09:27 +0100 Subject: [PATCH 20/29] add solver test --- matricies/test/matrice1.test | 12 ++++++++++++ test/test_solver.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 matricies/test/matrice1.test create mode 100644 test/test_solver.cpp diff --git a/matricies/test/matrice1.test b/matricies/test/matrice1.test new file mode 100644 index 0000000..9d63899 --- /dev/null +++ b/matricies/test/matrice1.test @@ -0,0 +1,12 @@ +2 3 +1 2 3 +4 5 6 + +2 2 +0 1 +1 0 + +3 1 +1 +-2 +1 \ No newline at end of file diff --git a/test/test_solver.cpp b/test/test_solver.cpp new file mode 100644 index 0000000..b621d43 --- /dev/null +++ b/test/test_solver.cpp @@ -0,0 +1,26 @@ +#include "Solver.h" +#include +#include +#include +#include + +namespace fs = std::filesystem; + +int main() { + std::string path = "test"; + for (const auto& entry : fs::directory_iterator(path)) { + std::string fileName = entry.path().string(); + std::cout << "Opening " << fileName << " ...\n"; + std::ifstream in{fileName}; + Matrix mat{1, 1}, imageMat{1, 1}, noyauMat{1, 1}; + in >> mat >> imageMat >> noyauMat; + + Vect image{imageMat}; + Vect noyau{noyauMat}; + + Solver solver{mat}; + assert(solver.Image() == image); + assert(solver.Noyau() == noyau); + } + return 0; +} \ No newline at end of file From 99467048e2302c0467818cebb0c99478eccf745d Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 21 Feb 2024 21:59:46 +0100 Subject: [PATCH 21/29] change Vect display --- src/Vect.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Vect.cpp b/src/Vect.cpp index 930a92e..17c06d9 100644 --- a/src/Vect.cpp +++ b/src/Vect.cpp @@ -67,7 +67,7 @@ void Vect::Print() const { std::cout << "Espace vectoriel de dimension " << GetCardinal() << " de base :\n\n"; for (std::size_t i = 0; i < m_Data.GetRawCount(); i++) { for (std::size_t j = 0; j < m_Data.GetColumnCount(); j++) { - printf("[ %.3f ]\t", static_cast(m_Data.at(i, j))); + printf("[ %u ]\t", static_cast(m_Data.at(i, j))); } std::cout << "\n"; } From 54798d0ff2889fdecafaa0c72b4b0023fe772e17 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 21 Feb 2024 22:01:31 +0100 Subject: [PATCH 22/29] resolve rectangular systems --- src/Solver.cpp | 12 ++++++++++++ src/Solver.h | 3 +++ src/Vect.cpp | 13 ++++++++++++- src/Vect.h | 20 ++++++++++++++++++++ src/main.cpp | 3 +++ 5 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Solver.cpp b/src/Solver.cpp index 336711d..ca71ef7 100644 --- a/src/Solver.cpp +++ b/src/Solver.cpp @@ -25,6 +25,18 @@ Vect Solver::Noyau() const { result.GetColumnCount() - origine_colonne)}; } +VectAffine Solver::SystemeTriangulaire() const { + Matrix mat = m_Matrix; + mat.GaussJordan(true); + + Solver solver{mat.SubMatrix(0, 0, mat.GetRawCount(), mat.GetColumnCount() - 1)}; + + Vect noyau = solver.Noyau(); + Matrix origin = mat.SubMatrix(0, mat.GetColumnCount() - 1, mat.GetRawCount(), 1); + + return {noyau, origin}; +} + std::size_t Solver::Rang() const { Vect image = Image(); return image.GetCardinal(); diff --git a/src/Solver.h b/src/Solver.h index aded7b5..f299f2e 100644 --- a/src/Solver.h +++ b/src/Solver.h @@ -5,6 +5,7 @@ class Solver { private: Matrix m_Matrix; + public: Solver(const Matrix& mat); ~Solver() {} @@ -12,5 +13,7 @@ class Solver { Vect Image() const; Vect Noyau() const; + VectAffine SystemeTriangulaire() const; + std::size_t Rang() const; }; \ No newline at end of file diff --git a/src/Vect.cpp b/src/Vect.cpp index 17c06d9..5ec21af 100644 --- a/src/Vect.cpp +++ b/src/Vect.cpp @@ -1,6 +1,7 @@ #include "Vect.h" #include "Solver.h" +#include #include Vect::Vect(const Matrix& mat) : m_Data(mat) { @@ -75,4 +76,14 @@ void Vect::Print() const { std::size_t Vect::GetDimension() const { return m_Data.GetRawCount(); -} \ No newline at end of file +} + +VectAffine::VectAffine(const Vect& base, const Matrix& origine) : + m_Base(base), m_Origin(origine.SubMatrix(0, 0, m_Base.GetDimension(), 1)) {} + +void VectAffine::Print() const { + std::cout << "\tEspace Affine :\n\n"; + m_Base.Print(); + std::cout << "\nOrigine :\n\n"; + m_Origin.Print(); +} diff --git a/src/Vect.h b/src/Vect.h index 6d5c759..9dbee22 100644 --- a/src/Vect.h +++ b/src/Vect.h @@ -36,4 +36,24 @@ class Vect { private: void Simplify(); +}; + + +class VectAffine { + private: + Vect m_Base; + Matrix m_Origin; + + public: + VectAffine(const Vect& base, const Matrix& origin); + + void Print() const; + + const Vect& GetBase() const { + return m_Base; + } + + const Matrix& GetOrigin() const { + return m_Origin; + } }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 39d5061..d4a1693 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,6 +32,9 @@ void test() { noyau.Print(); std::cout << "Système :\n"; noyau.GetLinearSystem().Print(); + + std::cout << "\n\n"; + solver.SystemeTriangulaire().Print(); } void prompt() { From efd0a88868a4f5ec6f5e0a3b2e01411b416ba5e6 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Wed, 21 Feb 2024 22:06:08 +0100 Subject: [PATCH 23/29] format test_solver --- test/test_solver.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/test_solver.cpp b/test/test_solver.cpp index b621d43..27cc16b 100644 --- a/test/test_solver.cpp +++ b/test/test_solver.cpp @@ -1,17 +1,21 @@ -#include "Solver.h" #include #include #include #include +#include "Solver.h" + namespace fs = std::filesystem; int main() { std::string path = "test"; for (const auto& entry : fs::directory_iterator(path)) { std::string fileName = entry.path().string(); + std::cout << "Opening " << fileName << " ...\n"; + std::ifstream in{fileName}; + Matrix mat{1, 1}, imageMat{1, 1}, noyauMat{1, 1}; in >> mat >> imageMat >> noyauMat; @@ -19,6 +23,7 @@ int main() { Vect noyau{noyauMat}; Solver solver{mat}; + assert(solver.Image() == image); assert(solver.Noyau() == noyau); } From 82ad2e06961b78c433628305b9aa838fca5564e4 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Fri, 23 Feb 2024 10:14:23 +0100 Subject: [PATCH 24/29] format project --- .clang-format | 2 ++ src/Matrix.cpp | 13 +++++++------ src/NR.h | 2 ++ src/Solver.cpp | 2 +- src/Solver.h | 1 + src/Vect.cpp | 2 +- src/Vect.h | 1 - src/main.cpp | 4 ++-- test/test_jordan.cpp | 12 ++++++------ test/test_solver.cpp | 10 +++++----- 10 files changed, 27 insertions(+), 22 deletions(-) diff --git a/.clang-format b/.clang-format index e993027..38e37ec 100644 --- a/.clang-format +++ b/.clang-format @@ -7,6 +7,8 @@ ConstructorInitializerAllOnOneLineOrOnePerLine: true PointerAlignment: Left SortIncludes: true SpacesBeforeTrailingComments: 2 +SeparateDefinitionBlocks: Always +SpaceBeforeCpp11BracedList: true UseTab: Always MaxEmptyLinesToKeep: 5 diff --git a/src/Matrix.cpp b/src/Matrix.cpp index a64fb97..422a22a 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -13,6 +13,7 @@ Matrix::Matrix(const std::string& fileNameInput) { Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Lignes(lignes), m_Colonnes(colonnes) { m_Data.resize(m_Lignes * m_Colonnes); } + Matrix::Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list&& initList) : m_Lignes(lignes), m_Colonnes(colonnes) { m_Data = initList; @@ -69,7 +70,7 @@ void Matrix::Insert() { } void Matrix::Save(const std::string& fileName) { - std::ofstream out{fileName}; + std::ofstream out {fileName}; if (!out) { std::cerr << "Impossible de sauvegarder la matrice !\n"; return; @@ -78,7 +79,7 @@ void Matrix::Save(const std::string& fileName) { } void Matrix::Load(const std::string& filename) { - std::ifstream in{filename}; + std::ifstream in {filename}; if (!in) { std::cerr << "Impossible de charger la matrice !\n"; return; @@ -87,7 +88,7 @@ void Matrix::Load(const std::string& filename) { } void Matrix::Transpose() { - Matrix result{m_Colonnes, m_Lignes}; + Matrix result {m_Colonnes, m_Lignes}; for (std::size_t i = 0; i < m_Lignes; i++) { for (std::size_t j = 0; j < m_Colonnes; j++) { result.at(j, i) = at(i, j); @@ -106,7 +107,7 @@ void Matrix::Identity() { } Matrix Matrix::Identity(std::size_t taille) { - Matrix id{taille, taille}; + Matrix id {taille, taille}; id.Identity(); return id; } @@ -126,7 +127,7 @@ bool Matrix::IsInversed() const { void Matrix::Augmenter(const Matrix& droite) { assert(droite.m_Lignes == m_Lignes); - Matrix temp{m_Lignes, m_Colonnes + droite.m_Colonnes}; + Matrix temp {m_Lignes, m_Colonnes + droite.m_Colonnes}; for (std::size_t i = 0; i < m_Lignes; i++) { for (std::size_t j = 0; j < m_Colonnes; j++) { @@ -245,7 +246,7 @@ std::size_t Matrix::GetColumnCount() const { 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}; + Matrix result {ligne, colonne}; for (std::size_t i = 0; i < ligne; i++) { for (std::size_t j = 0; j < colonne; j++) { diff --git a/src/NR.h b/src/NR.h index 8d6c6ce..b6c32d8 100644 --- a/src/NR.h +++ b/src/NR.h @@ -7,6 +7,8 @@ class NR { public: NR() : m_Numerator(0), m_Denominator(1) {} + NR(int entier) : m_Numerator(entier), m_Denominator(1) {} + NR(int numerator, int denominator) : m_Numerator(numerator), m_Denominator(denominator) {} }; \ No newline at end of file diff --git a/src/Solver.cpp b/src/Solver.cpp index ca71ef7..db4adf3 100644 --- a/src/Solver.cpp +++ b/src/Solver.cpp @@ -29,7 +29,7 @@ VectAffine Solver::SystemeTriangulaire() const { Matrix mat = m_Matrix; mat.GaussJordan(true); - Solver solver{mat.SubMatrix(0, 0, mat.GetRawCount(), mat.GetColumnCount() - 1)}; + Solver solver {mat.SubMatrix(0, 0, mat.GetRawCount(), mat.GetColumnCount() - 1)}; Vect noyau = solver.Noyau(); Matrix origin = mat.SubMatrix(0, mat.GetColumnCount() - 1, mat.GetRawCount(), 1); diff --git a/src/Solver.h b/src/Solver.h index f299f2e..c1d2eb7 100644 --- a/src/Solver.h +++ b/src/Solver.h @@ -8,6 +8,7 @@ class Solver { public: Solver(const Matrix& mat); + ~Solver() {} Vect Image() const; diff --git a/src/Vect.cpp b/src/Vect.cpp index 5ec21af..936a6fd 100644 --- a/src/Vect.cpp +++ b/src/Vect.cpp @@ -58,7 +58,7 @@ Matrix Vect::GetLinearSystem() const { Matrix vect = m_Data; vect.Transpose(); - Solver solver{vect}; + Solver solver {vect}; vect = solver.Noyau().m_Data; vect.Transpose(); return vect; diff --git a/src/Vect.h b/src/Vect.h index 9dbee22..64b587e 100644 --- a/src/Vect.h +++ b/src/Vect.h @@ -38,7 +38,6 @@ class Vect { void Simplify(); }; - class VectAffine { private: Vect m_Base; diff --git a/src/main.cpp b/src/main.cpp index d4a1693..cf4696a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,10 +16,10 @@ void test() { mat.Print(); // mat.Save("matrice4x4echelonne.mat"); */ - Matrix mat2{"matrice4x4.mat"}; + Matrix mat2 {"matrice4x4.mat"}; mat2.Print(); - Solver solver{mat2}; + Solver solver {mat2}; Vect image = solver.Image(); Vect noyau = solver.Noyau(); diff --git a/test/test_jordan.cpp b/test/test_jordan.cpp index 43efd2a..6272486 100644 --- a/test/test_jordan.cpp +++ b/test/test_jordan.cpp @@ -5,9 +5,9 @@ #error "Il faut être en debug mode ! xmake f -m debug" #endif -struct Test{ - Matrix mat; - Matrix res; +struct Test { + Matrix mat; + Matrix res; }; static const std::vector TEST_MATRICES = { @@ -35,9 +35,9 @@ static const std::vector TEST_MATRICES = { void test() { for (Test test : TEST_MATRICES) { - test.mat.GaussJordan(true); - assert(test.mat == test.res); - } + test.mat.GaussJordan(true); + assert(test.mat == test.res); + } } int main(int argc, char** argv) { diff --git a/test/test_solver.cpp b/test/test_solver.cpp index 27cc16b..876d7bc 100644 --- a/test/test_solver.cpp +++ b/test/test_solver.cpp @@ -14,15 +14,15 @@ int main() { std::cout << "Opening " << fileName << " ...\n"; - std::ifstream in{fileName}; + std::ifstream in {fileName}; - Matrix mat{1, 1}, imageMat{1, 1}, noyauMat{1, 1}; + Matrix mat {1, 1}, imageMat {1, 1}, noyauMat {1, 1}; in >> mat >> imageMat >> noyauMat; - Vect image{imageMat}; - Vect noyau{noyauMat}; + Vect image {imageMat}; + Vect noyau {noyauMat}; - Solver solver{mat}; + Solver solver {mat}; assert(solver.Image() == image); assert(solver.Noyau() == noyau); From 3b07ae783f34a170135027861f928b709c98b313 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Fri, 23 Feb 2024 10:48:43 +0100 Subject: [PATCH 25/29] refactor project --- src/Gauss.cpp | 75 ++++++++++++++++++++++++++++++++ src/Gauss.h | 9 ++++ src/Matrix.cpp | 100 +++---------------------------------------- src/Matrix.h | 19 +------- src/Solver.cpp | 8 ++-- src/Vect.cpp | 5 ++- src/main.cpp | 3 +- test/test_jordan.cpp | 3 +- 8 files changed, 103 insertions(+), 119 deletions(-) create mode 100644 src/Gauss.cpp create mode 100644 src/Gauss.h diff --git a/src/Gauss.cpp b/src/Gauss.cpp new file mode 100644 index 0000000..60513ee --- /dev/null +++ b/src/Gauss.cpp @@ -0,0 +1,75 @@ +#include "Gauss.h" + +#include "Matrix.h" + +namespace Gauss { + +static void GaussNonJordan(Matrix& mat, bool reduite) { + int r = -1; + for (std::size_t j = 0; j < mat.GetColumnCount(); j++) { + std::size_t indice_ligne_maximum = r + 1; + + // Recherche maximum + for (std::size_t i = r + 1; i < mat.GetRawCount(); i++) { + if (std::abs(mat.at(i, j)) > std::abs(mat.at(indice_ligne_maximum, j))) + indice_ligne_maximum = i; + } + + // Si A[k,j]≠0 alors (A[k,j] désigne la valeur de la ligne k et de la colonne j) + if (!IsEqualZero(mat.at(indice_ligne_maximum, j))) { + r++; + + // Si k≠r alors + if (indice_ligne_maximum != r) { + // Échanger les lignes k et r (On place la ligne du pivot en position r) + for (std::size_t k = 0; k < mat.GetColumnCount(); k++) { + std::swap(mat.at(indice_ligne_maximum, k), mat.at(r, k)); + } + } + + // Pour i de 1 jusqu'à n (On simplifie les autres lignes) + for (std::size_t i = (reduite ? 0 : j); i < mat.GetRawCount(); i++) { + // Si i≠r alors + if (i != r) { + // Soustraire à la ligne i la ligne r multipliée par A[i,j] (de façon à + // annuler A[i,j]) + for (int k = mat.GetColumnCount() - 1; k >= 0; k--) { + long double pivot = mat.at(r, j); + long double anul = mat.at(i, j); + mat.at(i, k) = mat.at(i, k) * pivot - mat.at(r, k) * anul; + } + } + } + } + } +} + +static void GaussJordan(Matrix& mat, bool reduite) { + GaussNonJordan(mat, reduite); + for (std::size_t i = 0; i < mat.GetRawCount(); i++) { + int k = -1; + for (std::size_t j = 0; j < mat.GetColumnCount(); j++) { + if (!IsEqualZero(mat.at(i, j))) { + k = j; + break; + } + } + // ligne de 0 + if (k == -1) + break; + // on divise la ligne par (i, k) + long double annul = mat.at(i, k); + for (int j = 0; j < mat.GetColumnCount(); j++) { + mat.at(i, j) /= annul; + } + } +} + +void GaussJordan(Matrix& mat, bool reduite, bool normalise) { + if (normalise) + GaussJordan(mat, reduite); + else + GaussNonJordan(mat, reduite); +} + +} // namespace Gauss \ No newline at end of file diff --git a/src/Gauss.h b/src/Gauss.h new file mode 100644 index 0000000..168919a --- /dev/null +++ b/src/Gauss.h @@ -0,0 +1,9 @@ +#pragma once + +class Matrix; + +namespace Gauss { + +void GaussJordan(Matrix& mat, bool reduite, bool normalise); + +} // namespace Gauss \ No newline at end of file diff --git a/src/Matrix.cpp b/src/Matrix.cpp index 422a22a..b289cd3 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -25,6 +25,7 @@ Matrix::~Matrix() {} Matrix Matrix::operator*(const Matrix& other) const { if (m_Colonnes != other.m_Lignes) { std::cerr << "Mutiplication impossible car la dimensions des matrices est incompatible" << std::endl; + return {1, 1, {0}}; } Matrix result(m_Lignes, other.m_Colonnes); @@ -53,13 +54,6 @@ void Matrix::Print() const { } } -void Matrix::PrintDebug() { -#ifndef NDEBUG - Print(); - std::cout << "\n"; -#endif -} - void Matrix::Insert() { for (size_t i = 0; i < m_Lignes; ++i) { for (size_t j = 0; j < m_Colonnes; ++j) { @@ -97,32 +91,14 @@ void Matrix::Transpose() { *this = result; } -void Matrix::Identity() { - assert(m_Lignes == m_Colonnes); - for (std::size_t i = 0; i < m_Lignes; i++) { - for (std::size_t j = i; j < m_Colonnes; j++) { - at(i, j) = i == j; - } - } -} - Matrix Matrix::Identity(std::size_t taille) { Matrix id {taille, taille}; - id.Identity(); - return id; -} - -bool Matrix::IsInversed() const { - for (std::size_t i = 0; i < m_Lignes; ++i) { - std::size_t j; - for (j = 0; j < m_Colonnes; ++j) { - if (!IsEqualZero(at(i, j))) { - break; - } - return false; + for (std::size_t i = 0; i < taille; i++) { + for (std::size_t j = i; j < taille; j++) { + id.at(i, j) = (i == j); } } - return true; + return id; } void Matrix::Augmenter(const Matrix& droite) { @@ -158,72 +134,6 @@ bool Matrix::operator==(const Matrix& other) const { return true; } -void Matrix::GaussNonJordan(bool reduite) { - int r = -1; - for (std::size_t j = 0; j < m_Colonnes; j++) { - std::size_t indice_ligne_maximum = r + 1; - - // Recherche maximum - for (std::size_t i = r + 1; i < m_Lignes; i++) { - if (std::abs(at(i, j)) > std::abs(at(indice_ligne_maximum, j))) - indice_ligne_maximum = i; - } - - // std::cout << "l'indice du maximum est : " << indice_ligne_maximum << "\n\n"; - - // Si A[k,j]≠0 alors (A[k,j] désigne la valeur de la ligne k et de la colonne j) - if (!IsEqualZero(at(indice_ligne_maximum, j))) { - r++; - - // PrintDebug(); - - // Si k≠r alors - if (indice_ligne_maximum != r) { - // Échanger les lignes k et r (On place la ligne du pivot en position r) - // std::cout << "On échange les lignes " << indice_ligne_maximum << " et " << r << '\n'; - for (std::size_t k = 0; k < m_Colonnes; k++) { - std::swap(at(indice_ligne_maximum, k), at(r, k)); - } - } - - // Pour i de 1 jusqu'à n (On simplifie les autres lignes) - for (std::size_t i = (reduite ? 0 : j); i < m_Lignes; i++) { - // Si i≠r alors - if (i != r) { - // Soustraire à la ligne i la ligne r multipliée par A[i,j] (de façon à - // annuler A[i,j]) - for (int k = m_Colonnes - 1; k >= 0; k--) { - long double pivot = at(r, j); - long double anul = at(i, j); - at(i, k) = at(i, k) * pivot - at(r, k) * anul; - } - } - } - } - } -} - -void Matrix::GaussJordan(bool reduite) { - GaussNonJordan(reduite); - for (std::size_t i = 0; i < m_Lignes; i++) { - int k = -1; - for (std::size_t j = 0; j < m_Colonnes; j++) { - if (!IsEqualZero(at(i, j))) { - k = j; - break; - } - } - // ligne de 0 - if (k == -1) - break; - // on divise la ligne par (i, k) - long double annul = at(i, k); - for (int j = 0; j < m_Colonnes; j++) { - at(i, j) /= annul; - } - } -} - long double& Matrix::operator[](std::size_t indice) { return m_Data[indice]; } diff --git a/src/Matrix.h b/src/Matrix.h index 4e61977..c6fbb56 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -20,40 +20,25 @@ class Matrix { std::size_t GetRawCount() const; std::size_t GetColumnCount() const; - Matrix operator*(const Matrix& other) const; - - void GaussNonJordan(bool reduite); - - void GaussJordan(bool reduite); - + void Insert(); void Print() const; - void PrintDebug(); - - void Insert(); - void Save(const std::string& fileName); - void Load(const std::string& filename); void Transpose(); - void Identity(); - static Matrix Identity(std::size_t taille); - bool IsInversed() const; - void Augmenter(const Matrix& droite); 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; - + Matrix operator*(const Matrix& other) const; long double& operator[](std::size_t indice); long double& at(std::size_t ligne, std::size_t colonne); - long double at(std::size_t ligne, std::size_t colonne) const; friend std::ostream& operator<<(std::ostream& stream, const Matrix& mat); diff --git a/src/Solver.cpp b/src/Solver.cpp index db4adf3..69fed58 100644 --- a/src/Solver.cpp +++ b/src/Solver.cpp @@ -1,11 +1,13 @@ #include "Solver.h" +#include "Gauss.h" + Solver::Solver(const Matrix& mat) : m_Matrix(mat) {} Vect Solver::Image() const { Matrix result = m_Matrix; result.Transpose(); - result.GaussJordan(true); + Gauss::GaussJordan(result, true, true); result.Transpose(); return {result}; } @@ -15,7 +17,7 @@ Vect Solver::Noyau() const { Matrix result = m_Matrix; result.Transpose(); result.Augmenter(Matrix::Identity(result.GetRawCount())); - result.GaussJordan(true); + Gauss::GaussJordan(result, true, true); result.Transpose(); // nombre de colonnes non nulles @@ -27,7 +29,7 @@ Vect Solver::Noyau() const { VectAffine Solver::SystemeTriangulaire() const { Matrix mat = m_Matrix; - mat.GaussJordan(true); + Gauss::GaussJordan(mat, true, true); Solver solver {mat.SubMatrix(0, 0, mat.GetRawCount(), mat.GetColumnCount() - 1)}; diff --git a/src/Vect.cpp b/src/Vect.cpp index 936a6fd..f56bb08 100644 --- a/src/Vect.cpp +++ b/src/Vect.cpp @@ -1,5 +1,6 @@ #include "Vect.h" +#include "Gauss.h" #include "Solver.h" #include #include @@ -45,7 +46,7 @@ bool Vect::operator==(const Vect& other) const { void Vect::AddVector(const Matrix& mat) { m_Data.Augmenter(mat); m_Data.Transpose(); - m_Data.GaussNonJordan(false); + Gauss::GaussJordan(m_Data, false, false); m_Data.Transpose(); Simplify(); } @@ -68,7 +69,7 @@ void Vect::Print() const { std::cout << "Espace vectoriel de dimension " << GetCardinal() << " de base :\n\n"; for (std::size_t i = 0; i < m_Data.GetRawCount(); i++) { for (std::size_t j = 0; j < m_Data.GetColumnCount(); j++) { - printf("[ %u ]\t", static_cast(m_Data.at(i, j))); + printf("[ %d ]\t", static_cast(m_Data.at(i, j))); } std::cout << "\n"; } diff --git a/src/main.cpp b/src/main.cpp index cf4696a..a662fbc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include "Gauss.h" #include "Solver.h" #include @@ -52,7 +53,7 @@ void prompt() { mat.Print(); - mat.GaussJordan(true); + Gauss::GaussJordan(mat, true, true); mat.Print(); } diff --git a/test/test_jordan.cpp b/test/test_jordan.cpp index 6272486..d61e51a 100644 --- a/test/test_jordan.cpp +++ b/test/test_jordan.cpp @@ -1,3 +1,4 @@ +#include "Gauss.h" #include "Matrix.h" #include @@ -35,7 +36,7 @@ static const std::vector TEST_MATRICES = { void test() { for (Test test : TEST_MATRICES) { - test.mat.GaussJordan(true); + Gauss::GaussJordan(test.mat, true, true); assert(test.mat == test.res); } } From 99eca82b3a2980b3e0579e530dceb280c82c3ab3 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Fri, 23 Feb 2024 11:02:49 +0100 Subject: [PATCH 26/29] fix Vect display --- src/Vect.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Vect.cpp b/src/Vect.cpp index f56bb08..1947141 100644 --- a/src/Vect.cpp +++ b/src/Vect.cpp @@ -69,7 +69,7 @@ void Vect::Print() const { std::cout << "Espace vectoriel de dimension " << GetCardinal() << " de base :\n\n"; for (std::size_t i = 0; i < m_Data.GetRawCount(); i++) { for (std::size_t j = 0; j < m_Data.GetColumnCount(); j++) { - printf("[ %d ]\t", static_cast(m_Data.at(i, j))); + std::cout << "[ " << m_Data.at(i, j) << " ]\t"; } std::cout << "\n"; } From 5a5c247019bda016e3fe11dbc3e81172120fe083 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Fri, 23 Feb 2024 11:03:38 +0100 Subject: [PATCH 27/29] english name for symbols --- src/Matrix.cpp | 80 ++++++++++++++++++++++---------------------- src/Matrix.h | 20 +++++------ src/Solver.cpp | 10 +++--- src/Solver.h | 6 ++-- src/Vect.cpp | 4 +-- src/main.cpp | 4 +-- test/test_solver.cpp | 2 +- 7 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/Matrix.cpp b/src/Matrix.cpp index b289cd3..596a55f 100644 --- a/src/Matrix.cpp +++ b/src/Matrix.cpp @@ -10,30 +10,30 @@ Matrix::Matrix(const std::string& fileNameInput) { Load(fileNameInput); } -Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Lignes(lignes), m_Colonnes(colonnes) { - m_Data.resize(m_Lignes * m_Colonnes); +Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Raws(lignes), m_Columns(colonnes) { + m_Data.resize(m_Raws * m_Columns); } Matrix::Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list&& initList) : - m_Lignes(lignes), m_Colonnes(colonnes) { + m_Raws(lignes), m_Columns(colonnes) { m_Data = initList; - m_Data.resize(m_Lignes * m_Colonnes); + m_Data.resize(m_Raws * m_Columns); } Matrix::~Matrix() {} Matrix Matrix::operator*(const Matrix& other) const { - if (m_Colonnes != other.m_Lignes) { + if (m_Columns != other.m_Raws) { std::cerr << "Mutiplication impossible car la dimensions des matrices est incompatible" << std::endl; return {1, 1, {0}}; } - Matrix result(m_Lignes, other.m_Colonnes); + Matrix result(m_Raws, other.m_Columns); - for (std::size_t i = 0; i < m_Lignes; ++i) { - for (std::size_t j = 0; j < other.m_Colonnes; ++j) { + for (std::size_t i = 0; i < m_Raws; ++i) { + for (std::size_t j = 0; j < other.m_Columns; ++j) { long double sum = 0; - for (std::size_t k = 0; k < m_Colonnes; k++) { + for (std::size_t k = 0; k < m_Columns; k++) { sum += at(i, k) * other.at(k, j); } result.at(i, j) = sum; @@ -43,10 +43,10 @@ Matrix Matrix::operator*(const Matrix& other) const { } void Matrix::Print() const { - for (size_t i = 0; i < m_Lignes; ++i) { + for (size_t i = 0; i < m_Raws; ++i) { std::cout << "[ "; - for (size_t j = 0; j < m_Colonnes; ++j) { - std::size_t indice = i * m_Lignes + j; + for (size_t j = 0; j < m_Columns; ++j) { + std::size_t indice = i * m_Raws + j; std::cout << at(i, j) << " "; } std::cout << "]"; @@ -55,8 +55,8 @@ void Matrix::Print() const { } void Matrix::Insert() { - for (size_t i = 0; i < m_Lignes; ++i) { - for (size_t j = 0; j < m_Colonnes; ++j) { + for (size_t i = 0; i < m_Raws; ++i) { + for (size_t j = 0; j < m_Columns; ++j) { std::cin >> at(i, j); } std::cout << std::endl; @@ -82,9 +82,9 @@ void Matrix::Load(const std::string& filename) { } void Matrix::Transpose() { - Matrix result {m_Colonnes, m_Lignes}; - for (std::size_t i = 0; i < m_Lignes; i++) { - for (std::size_t j = 0; j < m_Colonnes; j++) { + Matrix result {m_Columns, m_Raws}; + for (std::size_t i = 0; i < m_Raws; i++) { + for (std::size_t j = 0; j < m_Columns; j++) { result.at(j, i) = at(i, j); } } @@ -101,19 +101,19 @@ Matrix Matrix::Identity(std::size_t taille) { return id; } -void Matrix::Augmenter(const Matrix& droite) { - assert(droite.m_Lignes == m_Lignes); - Matrix temp {m_Lignes, m_Colonnes + droite.m_Colonnes}; +void Matrix::Augment(const Matrix& droite) { + assert(droite.m_Raws == m_Raws); + Matrix temp {m_Raws, m_Columns + droite.m_Columns}; - for (std::size_t i = 0; i < m_Lignes; i++) { - for (std::size_t j = 0; j < m_Colonnes; j++) { + for (std::size_t i = 0; i < m_Raws; i++) { + for (std::size_t j = 0; j < m_Columns; j++) { temp.at(i, j) = at(i, j); } } - for (std::size_t i = 0; i < m_Lignes; i++) { - for (std::size_t j = 0; j < droite.m_Colonnes; j++) { - temp.at(i, j + m_Colonnes) = droite.at(i, j); + for (std::size_t i = 0; i < m_Raws; i++) { + for (std::size_t j = 0; j < droite.m_Columns; j++) { + temp.at(i, j + m_Columns) = droite.at(i, j); } } @@ -121,11 +121,11 @@ void Matrix::Augmenter(const Matrix& droite) { } bool Matrix::operator==(const Matrix& other) const { - if (m_Lignes != other.m_Lignes || m_Colonnes != other.m_Colonnes) + if (m_Raws != other.m_Raws || m_Columns != other.m_Columns) return false; - for (std::size_t i = 0; i < m_Lignes; i++) { - for (std::size_t j = 0; j < m_Colonnes; j++) { + for (std::size_t i = 0; i < m_Raws; i++) { + for (std::size_t j = 0; j < m_Columns; j++) { if (!IsEqualZero(at(i, j) - other.at(i, j))) return false; } @@ -139,23 +139,23 @@ long double& Matrix::operator[](std::size_t indice) { } long double& Matrix::at(std::size_t ligne, std::size_t colonne) { - return m_Data[ligne * m_Colonnes + colonne]; + return m_Data[ligne * m_Columns + colonne]; } long double Matrix::at(std::size_t ligne, std::size_t colonne) const { - return m_Data[ligne * m_Colonnes + colonne]; + return m_Data[ligne * m_Columns + colonne]; } std::size_t Matrix::GetRawCount() const { - return m_Lignes; + return m_Raws; } std::size_t Matrix::GetColumnCount() const { - return m_Colonnes; + return m_Columns; } 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); + assert(m_Raws >= ligne && m_Columns >= colonne); Matrix result {ligne, colonne}; for (std::size_t i = 0; i < ligne; i++) { @@ -168,9 +168,9 @@ Matrix Matrix::SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne, } std::ostream& operator<<(std::ostream& stream, const Matrix& mat) { - stream << mat.m_Lignes << " " << mat.m_Colonnes << "\n"; - for (std::size_t i = 0; i < mat.m_Lignes; i++) { - for (std::size_t j = 0; j < mat.m_Colonnes; j++) { + stream << mat.m_Raws << " " << mat.m_Columns << "\n"; + for (std::size_t i = 0; i < mat.m_Raws; i++) { + for (std::size_t j = 0; j < mat.m_Columns; j++) { stream << mat.at(i, j) << " "; } stream << "\n"; @@ -179,10 +179,10 @@ std::ostream& operator<<(std::ostream& stream, const Matrix& mat) { } std::istream& operator>>(std::istream& stream, Matrix& mat) { - stream >> mat.m_Lignes >> mat.m_Colonnes; - mat.m_Data.resize(mat.m_Lignes * mat.m_Colonnes); - for (std::size_t i = 0; i < mat.m_Lignes; i++) { - for (std::size_t j = 0; j < mat.m_Colonnes; j++) { + stream >> mat.m_Raws >> mat.m_Columns; + mat.m_Data.resize(mat.m_Raws * mat.m_Columns); + for (std::size_t i = 0; i < mat.m_Raws; i++) { + for (std::size_t j = 0; j < mat.m_Columns; j++) { stream >> mat.at(i, j); } } diff --git a/src/Matrix.h b/src/Matrix.h index c6fbb56..2d34ded 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -7,14 +7,14 @@ class Matrix { private: - std::size_t m_Lignes; - std::size_t m_Colonnes; + std::size_t m_Raws; + std::size_t m_Columns; std::vector m_Data; public: Matrix(const std::string& fileNameInput); - Matrix(std::size_t lignes, std::size_t colonnes); - Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list&& initList); + Matrix(std::size_t raws, std::size_t columns); + Matrix(std::size_t raws, std::size_t columns, std::initializer_list&& initList); ~Matrix(); std::size_t GetRawCount() const; @@ -28,18 +28,18 @@ class Matrix { void Transpose(); - static Matrix Identity(std::size_t taille); + static Matrix Identity(std::size_t size); - void Augmenter(const Matrix& droite); + void Augment(const Matrix& right); - Matrix SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne, std::size_t ligne, std::size_t colonne) const; + Matrix SubMatrix(std::size_t raw_origin, std::size_t column_origin, std::size_t raw, std::size_t column) const; bool operator==(const Matrix& other) const; Matrix operator*(const Matrix& other) const; - long double& operator[](std::size_t indice); + long double& operator[](std::size_t index); - long double& at(std::size_t ligne, std::size_t colonne); - long double at(std::size_t ligne, std::size_t colonne) const; + long double& at(std::size_t raw, std::size_t column); + long double at(std::size_t raw, std::size_t column) const; friend std::ostream& operator<<(std::ostream& stream, const Matrix& mat); friend std::istream& operator>>(std::istream& stream, Matrix& mat); diff --git a/src/Solver.cpp b/src/Solver.cpp index 69fed58..5f05d14 100644 --- a/src/Solver.cpp +++ b/src/Solver.cpp @@ -13,10 +13,10 @@ Vect Solver::Image() const { } // https://en.wikipedia.org/wiki/Kernel_(linear_algebra)#Computation_by_Gaussian_elimination -Vect Solver::Noyau() const { +Vect Solver::Kernel() const { Matrix result = m_Matrix; result.Transpose(); - result.Augmenter(Matrix::Identity(result.GetRawCount())); + result.Augment(Matrix::Identity(result.GetRawCount())); Gauss::GaussJordan(result, true, true); result.Transpose(); @@ -27,19 +27,19 @@ Vect Solver::Noyau() const { result.GetColumnCount() - origine_colonne)}; } -VectAffine Solver::SystemeTriangulaire() const { +VectAffine Solver::TriangularSystem() const { Matrix mat = m_Matrix; Gauss::GaussJordan(mat, true, true); Solver solver {mat.SubMatrix(0, 0, mat.GetRawCount(), mat.GetColumnCount() - 1)}; - Vect noyau = solver.Noyau(); + Vect noyau = solver.Kernel(); Matrix origin = mat.SubMatrix(0, mat.GetColumnCount() - 1, mat.GetRawCount(), 1); return {noyau, origin}; } -std::size_t Solver::Rang() const { +std::size_t Solver::Rank() const { Vect image = Image(); return image.GetCardinal(); } diff --git a/src/Solver.h b/src/Solver.h index c1d2eb7..2d6bbf5 100644 --- a/src/Solver.h +++ b/src/Solver.h @@ -12,9 +12,9 @@ class Solver { ~Solver() {} Vect Image() const; - Vect Noyau() const; + Vect Kernel() const; - VectAffine SystemeTriangulaire() const; + VectAffine TriangularSystem() const; - std::size_t Rang() const; + std::size_t Rank() const; }; \ No newline at end of file diff --git a/src/Vect.cpp b/src/Vect.cpp index 1947141..e3a2368 100644 --- a/src/Vect.cpp +++ b/src/Vect.cpp @@ -44,7 +44,7 @@ bool Vect::operator==(const Vect& other) const { } void Vect::AddVector(const Matrix& mat) { - m_Data.Augmenter(mat); + m_Data.Augment(mat); m_Data.Transpose(); Gauss::GaussJordan(m_Data, false, false); m_Data.Transpose(); @@ -60,7 +60,7 @@ Matrix Vect::GetLinearSystem() const { vect.Transpose(); Solver solver {vect}; - vect = solver.Noyau().m_Data; + vect = solver.Kernel().m_Data; vect.Transpose(); return vect; } diff --git a/src/main.cpp b/src/main.cpp index a662fbc..c45d706 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,7 @@ void test() { Solver solver {mat2}; Vect image = solver.Image(); - Vect noyau = solver.Noyau(); + Vect noyau = solver.Kernel(); std::cout << "\tImage :\n"; image.Print(); @@ -35,7 +35,7 @@ void test() { noyau.GetLinearSystem().Print(); std::cout << "\n\n"; - solver.SystemeTriangulaire().Print(); + solver.TriangularSystem().Print(); } void prompt() { diff --git a/test/test_solver.cpp b/test/test_solver.cpp index 876d7bc..cfe6e7e 100644 --- a/test/test_solver.cpp +++ b/test/test_solver.cpp @@ -25,7 +25,7 @@ int main() { Solver solver {mat}; assert(solver.Image() == image); - assert(solver.Noyau() == noyau); + assert(solver.Kernel() == noyau); } return 0; } \ No newline at end of file From a35f45015b1e7fdd5306e5e005a798c6e107431e Mon Sep 17 00:00:00 2001 From: Simon Pribylski Date: Fri, 23 Feb 2024 11:16:58 +0100 Subject: [PATCH 28/29] Use fixed xmake version for action --- .gitea/workflows/ubuntu.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/ubuntu.yaml b/.gitea/workflows/ubuntu.yaml index e4d31e4..2b37b66 100644 --- a/.gitea/workflows/ubuntu.yaml +++ b/.gitea/workflows/ubuntu.yaml @@ -13,7 +13,7 @@ jobs: - name: Prepare XMake uses: xmake-io/github-action-setup-xmake@v1 with: - xmake-version: branch@master + xmake-version: '2.8.6' actions-cache-folder: '.xmake-cache' actions-cache-key: 'ubuntu' From 6b03fab302019fb14f75fda8cd052960db00661e Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Sat, 24 Feb 2024 13:05:29 +0100 Subject: [PATCH 29/29] action: use xmake latest --- .gitea/workflows/ubuntu.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/ubuntu.yaml b/.gitea/workflows/ubuntu.yaml index 2b37b66..4f37a72 100644 --- a/.gitea/workflows/ubuntu.yaml +++ b/.gitea/workflows/ubuntu.yaml @@ -13,7 +13,7 @@ jobs: - name: Prepare XMake uses: xmake-io/github-action-setup-xmake@v1 with: - xmake-version: '2.8.6' + xmake-version: latest actions-cache-folder: '.xmake-cache' actions-cache-key: 'ubuntu'