This commit is contained in:
@@ -1,86 +0,0 @@
|
||||
#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,18 +31,6 @@ 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) {
|
||||
|
||||
@@ -154,10 +154,12 @@ bool Matrix::operator==(const Matrix& a_Other) const {
|
||||
}
|
||||
|
||||
Matrix::Element& Matrix::at(std::size_t a_Raw, std::size_t a_Column) {
|
||||
assert(a_Raw < m_Raws && a_Column < m_Columns);
|
||||
return m_Data[a_Raw * m_Columns + a_Column];
|
||||
}
|
||||
|
||||
Matrix::Element Matrix::at(std::size_t a_Raw, std::size_t a_Column) const {
|
||||
assert(a_Raw < m_Raws && a_Column < m_Columns);
|
||||
return m_Data[a_Raw * m_Columns + a_Column];
|
||||
}
|
||||
|
||||
|
||||
43
src/NR.cpp
43
src/NR.cpp
@@ -3,7 +3,7 @@
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
int PGCD(int x, int y) {
|
||||
NR::Int PGCD(NR::Int x, NR::Int y) {
|
||||
if (x == 0 || y == 0)
|
||||
return 1;
|
||||
else if (x % y == 0)
|
||||
@@ -14,18 +14,18 @@ int PGCD(int x, int y) {
|
||||
|
||||
NR::NR() : m_Numerator(0), m_Denominator(1) {}
|
||||
|
||||
NR::NR(int entier) : m_Numerator(entier), m_Denominator(1) {}
|
||||
NR::NR(NR::Int entier) : m_Numerator(entier), m_Denominator(1) {}
|
||||
|
||||
NR::NR(int numerator, int denominator) :
|
||||
NR::NR(NR::Int numerator, NR::Int denominator) :
|
||||
m_Numerator((denominator > 0) ? numerator : -numerator), m_Denominator(std::abs(denominator)) {
|
||||
assert(denominator != 0);
|
||||
Reduce();
|
||||
}
|
||||
|
||||
void NR::Reduce() {
|
||||
int divisor = PGCD(m_Denominator, m_Numerator);
|
||||
NR::Int divisor = PGCD(m_Denominator, m_Numerator);
|
||||
m_Denominator /= divisor;
|
||||
m_Numerator /= divisor;
|
||||
assert(m_Denominator != 0);
|
||||
}
|
||||
|
||||
NR NR::Inverse() const {
|
||||
@@ -33,11 +33,11 @@ NR NR::Inverse() const {
|
||||
return {m_Denominator, m_Numerator};
|
||||
}
|
||||
|
||||
int NR::GetNumerator() const {
|
||||
NR::Int NR::GetNumerator() const {
|
||||
return m_Numerator;
|
||||
}
|
||||
|
||||
int NR::GetDenominator() const {
|
||||
NR::Int NR::GetDenominator() const {
|
||||
return m_Denominator;
|
||||
}
|
||||
|
||||
@@ -66,48 +66,57 @@ bool NR::operator>=(const NR& opNR) const {
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const NR& opNR) {
|
||||
os << opNR.GetNumerator() << "/" << opNR.GetDenominator();
|
||||
os << opNR.GetNumerator();
|
||||
if (opNR.GetDenominator() != 1)
|
||||
os << "/" << opNR.GetDenominator();
|
||||
return os;
|
||||
}
|
||||
|
||||
std::istream& operator>>(std::istream& is, NR& opNR) {
|
||||
char slash;
|
||||
is >> opNR.m_Numerator >> slash >> opNR.m_Denominator;
|
||||
is >> opNR.m_Numerator >> slash;
|
||||
if (slash != '/') {
|
||||
// on revient un charactère en arrière
|
||||
is.seekg(is.tellg() - static_cast<std::streampos>(1));
|
||||
opNR.m_Denominator = 1;
|
||||
} else {
|
||||
is >> opNR.m_Denominator;
|
||||
}
|
||||
opNR.Reduce();
|
||||
return is;
|
||||
}
|
||||
|
||||
NR NR::operator+(const NR& opNR) const {
|
||||
int num, den;
|
||||
Int num, den;
|
||||
num = m_Numerator * opNR.GetDenominator();
|
||||
den = m_Denominator * opNR.GetDenominator();
|
||||
num += (opNR.GetNumerator() * m_Denominator);
|
||||
NR result(num, den);
|
||||
NR result(num, num == 0 ? 1 : den);
|
||||
return result;
|
||||
}
|
||||
|
||||
NR NR::operator-(const NR& opNR) const {
|
||||
int num, den;
|
||||
Int num, den;
|
||||
num = m_Numerator * opNR.GetDenominator();
|
||||
den = m_Denominator * opNR.GetDenominator();
|
||||
num -= (opNR.GetNumerator() * m_Denominator);
|
||||
NR result(num, den);
|
||||
NR result(num, num == 0 ? 1 : den);
|
||||
return result;
|
||||
}
|
||||
|
||||
NR NR::operator*(const NR& opNR) const {
|
||||
int num, den;
|
||||
Int num, den;
|
||||
num = m_Numerator * opNR.GetNumerator();
|
||||
den = m_Denominator * opNR.GetDenominator();
|
||||
NR result(num, den);
|
||||
NR result(num, num == 0 ? 1 : den);
|
||||
return result;
|
||||
}
|
||||
|
||||
NR NR::operator/(const NR& opNR) const {
|
||||
int num, den;
|
||||
Int num, den;
|
||||
num = m_Numerator * opNR.GetDenominator();
|
||||
den = m_Denominator * opNR.GetNumerator();
|
||||
NR result(num, den);
|
||||
NR result(num, num == 0 ? 1 : den);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ Vect Solver::Kernel(Matrix&& a_Matrix) const {
|
||||
|
||||
a_Matrix.Transpose();
|
||||
a_Matrix.Augment(Matrix::Identity(a_Matrix.GetRawCount()));
|
||||
Gauss::GaussJordan(a_Matrix, false, false);
|
||||
Gauss::GaussJordan(a_Matrix, false, true);
|
||||
a_Matrix.Transpose();
|
||||
|
||||
// nombre de colonnes non nulles
|
||||
@@ -45,9 +45,8 @@ VectAffine Solver::RectangularSystem(Matrix&& a_MatrixA, const Matrix& a_VectorB
|
||||
Vect noyau = solver.Kernel(std::move(a_MatrixA));
|
||||
Matrix origin = mat.SubMatrix(0, mat.GetColumnCount() - 1, mat.GetRawCount(), 1);
|
||||
|
||||
// on rajoute des 0 si il faut
|
||||
|
||||
Matrix fullOrigin {mat.GetColumnCount(), 1};
|
||||
// on calcule le vecteur qui dirige l'espace affine
|
||||
Matrix fullOrigin {mat.GetColumnCount() - 1, 1};
|
||||
for (std::size_t i = 0; i < mat.GetRawCount(); i++) {
|
||||
int pivot_index = FirstNotNullElementIndexOnLine(mat, i);
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ static bool IsColumnNull(Matrix& mat, std::size_t column) {
|
||||
|
||||
Vect::Vect(Matrix&& a_Matrix) : m_Data(std::move(a_Matrix)) {
|
||||
m_Data.Transpose();
|
||||
Gauss::GaussJordan(m_Data, false, false);
|
||||
Gauss::GaussJordan(m_Data, false, true);
|
||||
m_Data.Transpose();
|
||||
Simplify();
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "Matrix.h"
|
||||
#include "Solver.h"
|
||||
#include <imgui.h>
|
||||
#include <sstream>
|
||||
|
||||
static std::string equationsResultImage;
|
||||
|
||||
@@ -16,6 +17,12 @@ static Matrix LoadMatrixFromStdVect(const std::vector<std::vector<int>>& data) {
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::string ElementToString(Matrix::Element e) {
|
||||
std::stringstream ss;
|
||||
ss << e;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
static std::string PrintVect(const Vect& vect) {
|
||||
if (vect.GetCardinal() == 0)
|
||||
return "{0}";
|
||||
@@ -25,11 +32,13 @@ 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 += ElementToString(vector.at(j, 0)) + ", ";
|
||||
}
|
||||
result = result.substr(0, result.size() - 2);
|
||||
result += " ), ";
|
||||
}
|
||||
result += " )";
|
||||
result = result.substr(0, result.size() - 2);
|
||||
result += " )";
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -43,6 +52,8 @@ void PivotGui::Render() {
|
||||
static int matrixSizeY = 4;
|
||||
static Solver solver;
|
||||
|
||||
static bool refresh = true;
|
||||
|
||||
// divisions des fenetres
|
||||
ImVec2 topLeftWindowSize(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.8f);
|
||||
ImVec2 topRightWindowSize(io.DisplaySize.x * 0.5f, io.DisplaySize.y * 0.8f);
|
||||
@@ -59,13 +70,15 @@ void PivotGui::Render() {
|
||||
|
||||
ImGui::Text("Matrice initiale:");
|
||||
|
||||
ImGui::InputInt("##RowsMatriceInitiale", &matrixSizeY);
|
||||
if (ImGui::InputInt("##RowsMatriceInitiale", &matrixSizeY))
|
||||
refresh = true;
|
||||
matrixSizeY = std::max(1, matrixSizeY);
|
||||
ImGui::SameLine();
|
||||
ImGui::Text("Lignes");
|
||||
|
||||
|
||||
ImGui::InputInt("##ColumnsMatriceInitiale", &matrixSizeX);
|
||||
if (ImGui::InputInt("##ColumnsMatriceInitiale", &matrixSizeX))
|
||||
refresh = true;
|
||||
matrixSizeX = std::max(1, matrixSizeX);
|
||||
ImGui::SameLine();
|
||||
ImGui::Text("Colonnes");
|
||||
@@ -76,13 +89,13 @@ void PivotGui::Render() {
|
||||
|
||||
// Resize matrixValues and initialize new elements to 0
|
||||
|
||||
matrixValues.resize(matrixSizeY);
|
||||
for (auto& row : matrixValues) {
|
||||
row.resize(matrixSizeX, 0);
|
||||
if (refresh) {
|
||||
matrixValues.resize(matrixSizeY);
|
||||
for (auto& row : matrixValues) {
|
||||
row.resize(matrixSizeX, 0);
|
||||
}
|
||||
}
|
||||
|
||||
bool refresh = false;
|
||||
|
||||
for (int y = 0; y < matrixSizeY; y++) {
|
||||
for (int x = 0; x < matrixSizeX; x++) {
|
||||
if (x > 0)
|
||||
@@ -136,7 +149,7 @@ void PivotGui::Render() {
|
||||
for (size_t i = 0; i < linearSystem.GetRawCount(); ++i) {
|
||||
for (size_t j = 0; j < linearSystem.GetColumnCount(); ++j) {
|
||||
equationsResultImage +=
|
||||
std::to_string(static_cast<int>(linearSystem.at(i, j))) + "*" + std::string {static_cast<char>('a' + j)} + " + ";
|
||||
ElementToString(linearSystem.at(i, j)) + "*" + std::string {static_cast<char>('a' + j)} + " + ";
|
||||
}
|
||||
equationsResultImage = equationsResultImage.substr(0, equationsResultImage.size() - 3) + " = 0\n";
|
||||
}
|
||||
@@ -146,6 +159,7 @@ void PivotGui::Render() {
|
||||
PrintVect(image);
|
||||
}
|
||||
|
||||
refresh = false;
|
||||
ImGui::End(); // End fenetre bas
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user