diff --git a/include/BigInt.h b/include/BigInt.h new file mode 100644 index 0000000..41d7ecd --- /dev/null +++ b/include/BigInt.h @@ -0,0 +1,49 @@ +#pragma once + +#include +#include + +/** + * \class BigInt + * \brief Représente un entier d'une taille quelconque + * \warning Prend énormément de place en mémoire. À utiliser avec précaution ! +*/ +class BigInt { + private: + mpz_t m_Data; + + public: + BigInt(std::string&& a_Number); + + BigInt(long a_Number = 0); + + BigInt(const BigInt& a_Copy); + + BigInt(BigInt&& a_Move); + + ~BigInt(); + + BigInt operator+(const BigInt& a_Other); + + BigInt operator-(const BigInt& a_Other); + + BigInt operator*(const BigInt& a_Other); + + BigInt operator/(const BigInt& a_Other); + + BigInt operator+=(const BigInt& a_Other); + + BigInt operator-=(const BigInt& a_Other); + + BigInt operator*=(const BigInt& a_Other); + + BigInt operator/=(const BigInt& a_Other); + + bool operator==(const BigInt& a_Other); + + void operator=(const BigInt& a_Other); + + const std::string ToString() const; + + bool IsEqualZero() const; +}; \ No newline at end of file diff --git a/include/IO.h b/include/IO.h index 01d6d45..8f62402 100644 --- a/include/IO.h +++ b/include/IO.h @@ -10,10 +10,14 @@ class Matrix; class Vect; class VectAffine; +class BigInt; std::ostream& operator<<(std::ostream& stream, const Matrix& mat); std::istream& operator>>(std::istream& stream, Matrix& mat); +std::ostream& operator<<(std::ostream& stream, const BigInt& nbre); +std::istream& operator>>(std::istream& stream, BigInt& nbre); + /** * \brief Charge une matrice à partir d'un fichier * \param fileName Le chemin du fichier à charger diff --git a/include/Matrix.h b/include/Matrix.h index 494e003..25c23a4 100644 --- a/include/Matrix.h +++ b/include/Matrix.h @@ -10,13 +10,15 @@ #include #include +#include "BigInt.h" + /** * \class Matrix * \brief Représente une matrice d'éléments */ class Matrix { public: - typedef long double Element; + typedef BigInt Element; typedef std::vector::iterator iterator; private: @@ -154,6 +156,21 @@ class Matrix { }; template -bool IsEqualZero(T var) { +bool IsEqualZero(const T& var) { return std::abs(var) < std::pow(10, -5); +} + +template <> +inline bool IsEqualZero(const int& var) { + return var == 0; +} + +template <> +inline bool IsEqualZero(const long& var) { + return var == 0; +} + +template <> +inline bool IsEqualZero(const BigInt& var) { + return var.IsEqualZero(); } \ No newline at end of file diff --git a/src/BigInt.cpp b/src/BigInt.cpp new file mode 100644 index 0000000..abd4b05 --- /dev/null +++ b/src/BigInt.cpp @@ -0,0 +1,86 @@ +#include "BigInt.h" + +#include + +BigInt::~BigInt() { + mpz_clear(m_Data); +} + +BigInt::BigInt(BigInt&& a_Move) { + std::swap(m_Data, a_Move.m_Data); +} + +BigInt::BigInt(std::string&& a_Number) { + mpz_init_set_str(m_Data, a_Number.c_str(), 10); +} + +BigInt::BigInt(long a_Number) { + mpz_init_set_si(m_Data, a_Number); +} + +BigInt::BigInt(const BigInt& a_Copy) { + mpz_init_set(m_Data, a_Copy.m_Data); +} + +void BigInt::operator=(const BigInt& a_Other) { + mpz_init_set(m_Data, a_Other.m_Data); +} + +BigInt BigInt::operator+(const BigInt& a_Other) { + BigInt result = *this; + result += a_Other; + return result; +} + +BigInt BigInt::operator-(const BigInt& a_Other) { + BigInt result = *this; + result -= a_Other; + return result; +} + +BigInt BigInt::operator*(const BigInt& a_Other) { + BigInt result = *this; + result *= a_Other; + return result; +} + +BigInt BigInt::operator/(const BigInt& a_Other) { + BigInt result = *this; + result /= a_Other; + return result; +} + +BigInt BigInt::operator+=(const BigInt& a_Other) { + mpz_add(m_Data, m_Data, a_Other.m_Data); + return *this; +} + +BigInt BigInt::operator-=(const BigInt& a_Other) { + mpz_sub(m_Data, m_Data, a_Other.m_Data); + return *this; +} + +BigInt BigInt::operator*=(const BigInt& a_Other) { + mpz_mul(m_Data, m_Data, a_Other.m_Data); + return *this; +} + +BigInt BigInt::operator/=(const BigInt& a_Other) { + mpz_divexact(m_Data, m_Data, a_Other.m_Data); + return *this; +} + +bool BigInt::operator==(const BigInt& a_Other) { + return mpz_cmp(m_Data, a_Other.m_Data) == 0; +} + +const std::string BigInt::ToString() const { + std::string result; + result.reserve(mpz_sizeinbase(m_Data, 10) + 2); + mpz_get_str(result.data(), 10, m_Data); + return result.c_str(); +} + +bool BigInt::IsEqualZero() const { + return mpz_cmp_si(m_Data, 0) == 0; +} \ No newline at end of file diff --git a/src/IO.cpp b/src/IO.cpp index 800a3cd..495056a 100644 --- a/src/IO.cpp +++ b/src/IO.cpp @@ -31,6 +31,18 @@ std::istream& operator>>(std::istream& stream, Matrix& mat) { return stream; } +std::ostream& operator<<(std::ostream& stream, const BigInt& nbre) { + stream << nbre.ToString(); + return stream; +} + +std::istream& operator>>(std::istream& stream, BigInt& nbre) { + long value; + stream >> value; + nbre = BigInt(value); + return stream; +} + Matrix LoadMatrix(const std::string& fileName) { std::ifstream in {fileName}; if (!in) { diff --git a/src/Solver.cpp b/src/Solver.cpp index 50d0a02..a1a19d2 100644 --- a/src/Solver.cpp +++ b/src/Solver.cpp @@ -16,7 +16,7 @@ Vect Solver::Kernel(Matrix&& a_Matrix) const { a_Matrix.Transpose(); a_Matrix.Augment(Matrix::Identity(a_Matrix.GetRawCount())); - Gauss::GaussJordan(a_Matrix, false, true); + Gauss::GaussJordan(a_Matrix, false, false); a_Matrix.Transpose(); // nombre de colonnes non nulles diff --git a/src/gui/PivotGui.cpp b/src/gui/PivotGui.cpp index a5e41a7..2d1d51b 100644 --- a/src/gui/PivotGui.cpp +++ b/src/gui/PivotGui.cpp @@ -17,7 +17,7 @@ static Matrix LoadMatrixFromStdVect(const std::vector>& data) { } static std::string PrintVect(const Vect& vect) { - if (vect.GetCardinal() == 0) + if (vect.GetCardinal() == 0) return "{0}"; std::string result = "Vect( "; @@ -25,7 +25,7 @@ static std::string PrintVect(const Vect& vect) { Matrix vector = vect.GetVector(i); result += " ("; for (std::size_t j = 0; j < vect.GetDimension(); j++) { - result += std::to_string(static_cast((vector.at(j, 0)))) + ", "; + result += vector.at(j, 0).ToString() + ", "; } result += " ), "; } @@ -60,14 +60,18 @@ void PivotGui::Render() { ImGui::Text("Matrice initiale:"); ImGui::InputInt("##RowsMatriceInitiale", &matrixSizeY); + matrixSizeY = std::max(1, matrixSizeY); ImGui::SameLine(); ImGui::Text("Lignes"); ImGui::InputInt("##ColumnsMatriceInitiale", &matrixSizeX); + matrixSizeX = std::max(1, matrixSizeX); ImGui::SameLine(); ImGui::Text("Colonnes"); + ImGui::NewLine(); + ImGui::BeginChild("MatriceInitiale", ImVec2(topLeftWindowSize.x, io.DisplaySize.y * 0.7f), false); // Resize matrixValues and initialize new elements to 0 @@ -77,13 +81,16 @@ void PivotGui::Render() { row.resize(matrixSizeX, 0); } + bool refresh = false; + for (int y = 0; y < matrixSizeY; y++) { for (int x = 0; x < matrixSizeX; x++) { if (x > 0) ImGui::SameLine(); ImGui::PushID(y * matrixSizeX + x); ImGui::PushItemWidth(30); // Adjust this value to change the cell size - ImGui::InputInt("", &matrixValues[y][x], 0, 0, ImGuiInputTextFlags_CharsDecimal); + if (ImGui::InputInt("", &matrixValues[y][x], 0, 0, ImGuiInputTextFlags_CharsDecimal)) + refresh = true; ImGui::PopItemWidth(); ImGui::PopID(); } @@ -106,7 +113,7 @@ void PivotGui::Render() { // rajouter le code pour la partie top right - static std::string result = "RIEN"; + static std::string result = ""; ImGui::TextWrapped(result.c_str()); @@ -118,36 +125,25 @@ void PivotGui::Render() { ImGui::Begin("Bottom Part", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar); - if (ImGui::Button("Calcul")) { + if (refresh) { // Calculate the kernel and image Vect image = solver.Image(LoadMatrixFromStdVect(matrixValues)); Matrix linearSystem = image.GetLinearSystem(); - // Create a column matrix with as many elements as the number of columns in the linear system - std::vector columnMatrix(linearSystem.GetColumnCount()); - for (size_t i = 0; i < linearSystem.GetColumnCount(); ++i) { - columnMatrix[i] = std::string(1, 'a' + static_cast(i)); - } - - // Multiply the linear system matrix by the column matrix - std::vector resultMatrix(linearSystem.GetRawCount()); - for (size_t i = 0; i < linearSystem.GetRawCount(); ++i) { - for (size_t j = 0; j < linearSystem.GetColumnCount(); ++j) { - resultMatrix[i] += std::to_string(static_cast(linearSystem.at(i, j))) + "*" + columnMatrix[j] + " + "; - } - resultMatrix[i] = resultMatrix[i].substr(0, resultMatrix[i].length() - 3) + " = 0"; - } - // Store the equationsResult strings in the global variable equationsResultImage = "Equations cartesiennes de l'espace vectoriel (Image):\n"; - for (const auto& equation : resultMatrix) { - equationsResultImage += equation + "\n"; + for (size_t i = 0; i < linearSystem.GetRawCount(); ++i) { + for (size_t j = 0; j < linearSystem.GetColumnCount(); ++j) { + equationsResultImage += + linearSystem.at(i, j).ToString() + "*" + std::string {static_cast('a' + j)} + " + "; + } + equationsResultImage = equationsResultImage.substr(0, equationsResultImage.size() - 3) + " = 0\n"; } result = std::string("Noyau: ") + "\n" + PrintVect(solver.Kernel(LoadMatrixFromStdVect(matrixValues))) + "\n" + "\n" + - "Rang: " + "\n" + std::to_string(solver.Rank(LoadMatrixFromStdVect(matrixValues))) + "\n" + "\n" + - "Image: " + "\n" + PrintVect(image); + "Rang: " + "\n" + std::to_string(solver.Rank(LoadMatrixFromStdVect(matrixValues))) + "\n" + "\n" + "Image: " + "\n" + + PrintVect(image); } ImGui::End(); // End fenetre bas diff --git a/test/test_random_kernel.cpp b/test/test_random_kernel.cpp index 27e36b3..31e326e 100644 --- a/test/test_random_kernel.cpp +++ b/test/test_random_kernel.cpp @@ -6,9 +6,9 @@ #include #include -static constexpr int EXECUTION_COUNT = 100; +static constexpr int EXECUTION_COUNT = 10; static constexpr int KERNEL_CHECKS = 100; -static constexpr int MATRIX_MAX_SIZE = 100; +static constexpr int MATRIX_MAX_SIZE = 10; static const Solver solver; diff --git a/test/test_solver.cpp b/test/test_solver.cpp index 83f1a07..bebb537 100644 --- a/test/test_solver.cpp +++ b/test/test_solver.cpp @@ -14,7 +14,7 @@ void TestRectangular() { 1, -1, -1, 2 }}; - VectAffine aff {Matrix::ColumnVector({0, -1, 1}), Matrix::ColumnVector({3.0 / 2.0, 0, -1.0 / 2.0})}; + VectAffine aff {Matrix::ColumnVector({0, -1, 1}), Matrix::ColumnVector({3, 0, -1})}; Solver solver; @@ -41,8 +41,11 @@ void TestKernelImage() { Matrix copy = mat; - test_assert(solver.Image(std::move(copy)) == image); - test_assert(solver.Kernel(std::move(mat)) == noyau); + Vect imageCalc = solver.Image(std::move(copy)); + Vect kernelCalc = solver.Kernel(std::move(mat)); + + test_assert(imageCalc == image); + test_assert(kernelCalc == noyau); } } diff --git a/xmake.lua b/xmake.lua index b1f574c..f1a58cf 100644 --- a/xmake.lua +++ b/xmake.lua @@ -2,10 +2,12 @@ add_rules("mode.debug", "mode.release") add_requires("libsdl 2.28.3", {configs = {sdlmain = false}}) add_requires("imgui", {configs = {sdl2_no_renderer = true, opengl3 = true}}) +add_requires("gmp"); set_languages("c++20") set_warnings("all") add_includedirs("include") +add_packages("gmp") -- Solver Library target("Pivot")