big int
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -17,7 +17,7 @@ static Matrix LoadMatrixFromStdVect(const std::vector<std::vector<int>>& 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<int>((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<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
|
||||
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<char>('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
|
||||
|
||||
Reference in New Issue
Block a user