big int
This commit is contained in:
49
include/BigInt.h
Normal file
49
include/BigInt.h
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <gmp.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \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;
|
||||||
|
};
|
||||||
@@ -10,10 +10,14 @@
|
|||||||
class Matrix;
|
class Matrix;
|
||||||
class Vect;
|
class Vect;
|
||||||
class VectAffine;
|
class VectAffine;
|
||||||
|
class BigInt;
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& stream, const Matrix& mat);
|
std::ostream& operator<<(std::ostream& stream, const Matrix& mat);
|
||||||
std::istream& operator>>(std::istream& stream, 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
|
* \brief Charge une matrice à partir d'un fichier
|
||||||
* \param fileName Le chemin du fichier à charger
|
* \param fileName Le chemin du fichier à charger
|
||||||
|
|||||||
@@ -10,13 +10,15 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "BigInt.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \class Matrix
|
* \class Matrix
|
||||||
* \brief Représente une matrice d'éléments
|
* \brief Représente une matrice d'éléments
|
||||||
*/
|
*/
|
||||||
class Matrix {
|
class Matrix {
|
||||||
public:
|
public:
|
||||||
typedef long double Element;
|
typedef BigInt Element;
|
||||||
typedef std::vector<Element>::iterator iterator;
|
typedef std::vector<Element>::iterator iterator;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -154,6 +156,21 @@ class Matrix {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool IsEqualZero(T var) {
|
bool IsEqualZero(const T& var) {
|
||||||
return std::abs(var) < std::pow(10, -5);
|
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();
|
||||||
|
}
|
||||||
86
src/BigInt.cpp
Normal file
86
src/BigInt.cpp
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
#include "BigInt.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
12
src/IO.cpp
12
src/IO.cpp
@@ -31,6 +31,18 @@ std::istream& operator>>(std::istream& stream, Matrix& mat) {
|
|||||||
return stream;
|
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) {
|
Matrix LoadMatrix(const std::string& fileName) {
|
||||||
std::ifstream in {fileName};
|
std::ifstream in {fileName};
|
||||||
if (!in) {
|
if (!in) {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ Vect Solver::Kernel(Matrix&& a_Matrix) const {
|
|||||||
|
|
||||||
a_Matrix.Transpose();
|
a_Matrix.Transpose();
|
||||||
a_Matrix.Augment(Matrix::Identity(a_Matrix.GetRawCount()));
|
a_Matrix.Augment(Matrix::Identity(a_Matrix.GetRawCount()));
|
||||||
Gauss::GaussJordan(a_Matrix, false, true);
|
Gauss::GaussJordan(a_Matrix, false, false);
|
||||||
a_Matrix.Transpose();
|
a_Matrix.Transpose();
|
||||||
|
|
||||||
// nombre de colonnes non nulles
|
// nombre de colonnes non nulles
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ static std::string PrintVect(const Vect& vect) {
|
|||||||
Matrix vector = vect.GetVector(i);
|
Matrix vector = vect.GetVector(i);
|
||||||
result += " (";
|
result += " (";
|
||||||
for (std::size_t j = 0; j < vect.GetDimension(); j++) {
|
for (std::size_t j = 0; j < vect.GetDimension(); j++) {
|
||||||
result += std::to_string(static_cast<int>((vector.at(j, 0)))) + ", ";
|
result += vector.at(j, 0).ToString() + ", ";
|
||||||
}
|
}
|
||||||
result += " ), ";
|
result += " ), ";
|
||||||
}
|
}
|
||||||
@@ -60,14 +60,18 @@ void PivotGui::Render() {
|
|||||||
ImGui::Text("Matrice initiale:");
|
ImGui::Text("Matrice initiale:");
|
||||||
|
|
||||||
ImGui::InputInt("##RowsMatriceInitiale", &matrixSizeY);
|
ImGui::InputInt("##RowsMatriceInitiale", &matrixSizeY);
|
||||||
|
matrixSizeY = std::max(1, matrixSizeY);
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
ImGui::Text("Lignes");
|
ImGui::Text("Lignes");
|
||||||
|
|
||||||
|
|
||||||
ImGui::InputInt("##ColumnsMatriceInitiale", &matrixSizeX);
|
ImGui::InputInt("##ColumnsMatriceInitiale", &matrixSizeX);
|
||||||
|
matrixSizeX = std::max(1, matrixSizeX);
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
ImGui::Text("Colonnes");
|
ImGui::Text("Colonnes");
|
||||||
|
|
||||||
|
ImGui::NewLine();
|
||||||
|
|
||||||
ImGui::BeginChild("MatriceInitiale", ImVec2(topLeftWindowSize.x, io.DisplaySize.y * 0.7f), false);
|
ImGui::BeginChild("MatriceInitiale", ImVec2(topLeftWindowSize.x, io.DisplaySize.y * 0.7f), false);
|
||||||
|
|
||||||
// Resize matrixValues and initialize new elements to 0
|
// Resize matrixValues and initialize new elements to 0
|
||||||
@@ -77,13 +81,16 @@ void PivotGui::Render() {
|
|||||||
row.resize(matrixSizeX, 0);
|
row.resize(matrixSizeX, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool refresh = false;
|
||||||
|
|
||||||
for (int y = 0; y < matrixSizeY; y++) {
|
for (int y = 0; y < matrixSizeY; y++) {
|
||||||
for (int x = 0; x < matrixSizeX; x++) {
|
for (int x = 0; x < matrixSizeX; x++) {
|
||||||
if (x > 0)
|
if (x > 0)
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
ImGui::PushID(y * matrixSizeX + x);
|
ImGui::PushID(y * matrixSizeX + x);
|
||||||
ImGui::PushItemWidth(30); // Adjust this value to change the cell size
|
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::PopItemWidth();
|
||||||
ImGui::PopID();
|
ImGui::PopID();
|
||||||
}
|
}
|
||||||
@@ -106,7 +113,7 @@ void PivotGui::Render() {
|
|||||||
|
|
||||||
// rajouter le code pour la partie top right
|
// rajouter le code pour la partie top right
|
||||||
|
|
||||||
static std::string result = "RIEN";
|
static std::string result = "";
|
||||||
|
|
||||||
ImGui::TextWrapped(result.c_str());
|
ImGui::TextWrapped(result.c_str());
|
||||||
|
|
||||||
@@ -118,36 +125,25 @@ void PivotGui::Render() {
|
|||||||
ImGui::Begin("Bottom Part", nullptr,
|
ImGui::Begin("Bottom Part", nullptr,
|
||||||
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
|
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
|
||||||
|
|
||||||
if (ImGui::Button("Calcul")) {
|
if (refresh) {
|
||||||
|
|
||||||
// Calculate the kernel and image
|
// Calculate the kernel and image
|
||||||
Vect image = solver.Image(LoadMatrixFromStdVect(matrixValues));
|
Vect image = solver.Image(LoadMatrixFromStdVect(matrixValues));
|
||||||
Matrix linearSystem = image.GetLinearSystem();
|
Matrix linearSystem = image.GetLinearSystem();
|
||||||
|
|
||||||
// Create a column matrix with as many elements as the number of columns in the linear system
|
|
||||||
std::vector<std::string> columnMatrix(linearSystem.GetColumnCount());
|
|
||||||
for (size_t i = 0; i < linearSystem.GetColumnCount(); ++i) {
|
|
||||||
columnMatrix[i] = std::string(1, 'a' + static_cast<char>(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Multiply the linear system matrix by the column matrix
|
|
||||||
std::vector<std::string> 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<int>(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
|
// Store the equationsResult strings in the global variable
|
||||||
equationsResultImage = "Equations cartesiennes de l'espace vectoriel (Image):\n";
|
equationsResultImage = "Equations cartesiennes de l'espace vectoriel (Image):\n";
|
||||||
for (const auto& equation : resultMatrix) {
|
for (size_t i = 0; i < linearSystem.GetRawCount(); ++i) {
|
||||||
equationsResultImage += equation + "\n";
|
for (size_t j = 0; j < linearSystem.GetColumnCount(); ++j) {
|
||||||
|
equationsResultImage +=
|
||||||
|
linearSystem.at(i, j).ToString() + "*" + std::string {static_cast<char>('a' + j)} + " + ";
|
||||||
|
}
|
||||||
|
equationsResultImage = equationsResultImage.substr(0, equationsResultImage.size() - 3) + " = 0\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
result = std::string("Noyau: ") + "\n" + PrintVect(solver.Kernel(LoadMatrixFromStdVect(matrixValues))) + "\n" + "\n" +
|
result = std::string("Noyau: ") + "\n" + PrintVect(solver.Kernel(LoadMatrixFromStdVect(matrixValues))) + "\n" + "\n" +
|
||||||
"Rang: " + "\n" + std::to_string(solver.Rank(LoadMatrixFromStdVect(matrixValues))) + "\n" + "\n" +
|
"Rang: " + "\n" + std::to_string(solver.Rank(LoadMatrixFromStdVect(matrixValues))) + "\n" + "\n" + "Image: " + "\n" +
|
||||||
"Image: " + "\n" + PrintVect(image);
|
PrintVect(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui::End(); // End fenetre bas
|
ImGui::End(); // End fenetre bas
|
||||||
|
|||||||
@@ -6,9 +6,9 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
static constexpr int EXECUTION_COUNT = 100;
|
static constexpr int EXECUTION_COUNT = 10;
|
||||||
static constexpr int KERNEL_CHECKS = 100;
|
static constexpr int KERNEL_CHECKS = 100;
|
||||||
static constexpr int MATRIX_MAX_SIZE = 100;
|
static constexpr int MATRIX_MAX_SIZE = 10;
|
||||||
|
|
||||||
static const Solver solver;
|
static const Solver solver;
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ void TestRectangular() {
|
|||||||
1, -1, -1, 2
|
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;
|
Solver solver;
|
||||||
|
|
||||||
@@ -41,8 +41,11 @@ void TestKernelImage() {
|
|||||||
|
|
||||||
Matrix copy = mat;
|
Matrix copy = mat;
|
||||||
|
|
||||||
test_assert(solver.Image(std::move(copy)) == image);
|
Vect imageCalc = solver.Image(std::move(copy));
|
||||||
test_assert(solver.Kernel(std::move(mat)) == noyau);
|
Vect kernelCalc = solver.Kernel(std::move(mat));
|
||||||
|
|
||||||
|
test_assert(imageCalc == image);
|
||||||
|
test_assert(kernelCalc == noyau);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ add_rules("mode.debug", "mode.release")
|
|||||||
|
|
||||||
add_requires("libsdl 2.28.3", {configs = {sdlmain = false}})
|
add_requires("libsdl 2.28.3", {configs = {sdlmain = false}})
|
||||||
add_requires("imgui", {configs = {sdl2_no_renderer = true, opengl3 = true}})
|
add_requires("imgui", {configs = {sdl2_no_renderer = true, opengl3 = true}})
|
||||||
|
add_requires("gmp");
|
||||||
|
|
||||||
set_languages("c++20")
|
set_languages("c++20")
|
||||||
set_warnings("all")
|
set_warnings("all")
|
||||||
add_includedirs("include")
|
add_includedirs("include")
|
||||||
|
add_packages("gmp")
|
||||||
|
|
||||||
-- Solver Library
|
-- Solver Library
|
||||||
target("Pivot")
|
target("Pivot")
|
||||||
|
|||||||
Reference in New Issue
Block a user