47 Commits

Author SHA1 Message Date
413ff4fce4 add project license
Some checks failed
Linux arm64 / Build (push) Failing after 40m2s
2024-05-23 17:32:01 +02:00
49e4d4b1e2 working linear system gui
Some checks failed
Linux arm64 / Build (push) Has been cancelled
2024-05-23 17:18:22 +02:00
9b84b9bf59 maybe fix test
Some checks failed
Linux arm64 / Build (push) Failing after 33m25s
2024-05-23 15:17:21 +02:00
bb650e4f2d gui refactor 2024-05-23 15:16:29 +02:00
e784a7b471 reduce solver random matrix size
Some checks failed
Linux arm64 / Build (push) Failing after 38m45s
2024-05-23 14:34:22 +02:00
9a5b99a79d init random
Some checks failed
Linux arm64 / Build (push) Failing after 2m43s
2024-05-14 23:09:51 +02:00
ffa0ebf4cb add missing include
Some checks failed
Linux arm64 / Build (push) Has been cancelled
2024-05-14 22:51:53 +02:00
4b3e878bc5 removed weird function
Some checks failed
Linux arm64 / Build (push) Failing after 2m26s
2024-05-14 22:41:01 +02:00
47f250170e remove useless xmake require
Some checks failed
Linux arm64 / Build (push) Has been cancelled
2024-05-14 22:39:57 +02:00
a4036ae36d add linear system test
Some checks failed
Linux arm64 / Build (push) Has been cancelled
2024-05-14 22:39:15 +02:00
e6d0785009 show jordaned matrix
Some checks failed
Linux arm64 / Build (push) Has been cancelled
2024-05-14 22:36:57 +02:00
a135df2e96 trop de trucs
All checks were successful
Linux arm64 / Build (push) Successful in 2m33s
2024-05-14 13:00:59 +02:00
d9e49d1319 forgot this 2024-05-13 21:30:52 +02:00
e127ff8c29 easier to debug assert 2024-05-13 21:11:40 +02:00
9b423f9c83 fix react system 2024-05-13 21:11:20 +02:00
fa7e70a437 better fill 2024-05-13 21:10:23 +02:00
76fa9fb329 fix gui build
Some checks failed
Linux arm64 / Build (push) Failing after 2m54s
2024-05-12 10:49:21 +02:00
7f1ef38286 fix solver
Some checks failed
Linux arm64 / Build (push) Failing after 2m0s
2024-05-12 09:23:37 +02:00
2af915057a fix intellisense
All checks were successful
Linux arm64 / Build (push) Successful in 2m29s
2024-05-11 19:44:43 +02:00
2975006972 big int 2024-05-11 19:44:32 +02:00
0d071b1cf9 remove unused include 2024-05-11 19:02:53 +02:00
a02f71e29e equations cartesiennes.
All checks were successful
Linux arm64 / Build (push) Successful in 3m10s
2024-05-11 18:44:20 +02:00
6833811b97 display kernel
All checks were successful
Linux arm64 / Build (push) Successful in 3m1s
2024-05-11 15:56:18 +02:00
b41bcae337 do computations
All checks were successful
Linux arm64 / Build (push) Successful in 2m56s
2024-05-10 18:53:29 +02:00
427eeb0b51 Merge branch 'master' into imgui
All checks were successful
Linux arm64 / Build (push) Successful in 3m16s
2024-05-10 18:39:37 +02:00
5fac74cefc fixing auto-insert of garbage values when adding columns
All checks were successful
Linux arm64 / Build (push) Successful in 37m37s
2024-05-10 15:35:41 +02:00
b239c0aaff adding dynamic number input and reading in matrix
Some checks failed
Linux arm64 / Build (push) Has been cancelled
2024-05-10 15:19:22 +02:00
6b763b8481 change c++ version
All checks were successful
Linux arm64 / Build (push) Successful in 1m45s
2024-05-09 15:57:52 +02:00
5391b7b76a more optimizations
Some checks failed
Linux arm64 / Build (push) Failing after 25s
2024-05-09 15:56:59 +02:00
dc31f1f091 less use of transpose
All checks were successful
Linux arm64 / Build (push) Successful in 1m26s
2024-05-04 15:21:49 +02:00
11cc0fadad gauss column 2024-05-04 15:10:13 +02:00
e71bc588e5 solver move matricies 2024-05-04 14:56:50 +02:00
36ef301cb9 doc test_assert 2024-05-04 13:08:11 +02:00
f5a282c455 allow tests in release mode
All checks were successful
Linux arm64 / Build (push) Successful in 6m40s
2024-05-04 12:42:26 +02:00
99c0e2010b PivotGUI visuals + .lua update
All checks were successful
Linux arm64 / Build (push) Successful in 41m17s
2024-04-11 00:20:51 +02:00
851080c7a4 fix minor typos
All checks were successful
Linux arm64 / Build (push) Successful in 1m16s
2024-04-05 15:04:23 +02:00
2724ca173d improve random_kernel test
All checks were successful
Linux arm64 / Build (push) Successful in 1m25s
2024-03-14 21:54:13 +01:00
13c9bc40db added random kernel tests
All checks were successful
Linux arm64 / Build (push) Successful in 1m1s
2024-03-06 22:53:07 +01:00
1b994daf3c reduce Vect on construct 2024-03-06 22:31:18 +01:00
99624d1b00 solver rework + refactor 2024-03-06 21:24:15 +01:00
Morph01
f595582948 added window for Pivot interface with a grid
All checks were successful
Linux arm64 / Build (push) Successful in 45m6s
2024-03-06 18:35:07 +01:00
259750a794 fix warnings
All checks were successful
Linux arm64 / Build (push) Successful in 36s
2024-03-05 23:00:32 +01:00
b9a3fb4ca4 action: fix tests
All checks were successful
Linux arm64 / Build (push) Successful in 33m30s
2024-02-25 08:50:08 +01:00
ceda1b00df action: update for gui
Some checks failed
Linux arm64 / Build (push) Failing after 41m36s
2024-02-24 19:53:41 +01:00
23484289a4 ignore imgui.ini
Some checks failed
Linux arm64 / Build (push) Has been cancelled
2024-02-24 19:24:56 +01:00
766aa6ada4 add imgui support 2024-02-24 19:24:49 +01:00
208c79800e add vscode intellisense 2024-02-24 19:22:57 +01:00
25 changed files with 1193 additions and 297 deletions

View File

@@ -7,15 +7,26 @@ jobs:
Build:
runs-on: ubuntu-latest
steps:
- name: Install opengl
run: |
apt update
apt install libgl-dev -y
- name: Check out repository code
uses: actions/checkout@v3
- name: Prepare XMake
- name: Prepare Xmake
uses: xmake-io/github-action-setup-xmake@v1
with:
xmake-version: latest
actions-cache-folder: '.xmake-cache'
actions-cache-key: 'ubuntu'
actions-cache-key: 'ubuntu-xmake'
- name: Cache
uses: actions/cache@v4
with:
path: ~/.xmake
key: 'ubuntu'
- name: XMake config
run: xmake f -p linux -y --root

3
.gitignore vendored
View File

@@ -7,3 +7,6 @@ build/
# VsCode
.vscode
#ImGui
imgui.ini

View File

@@ -2,8 +2,9 @@
"configurations": [
{
"name": "Pivot",
"cppStandard": "c++17",
"includePath": ["include"]
"cppStandard": "c++20",
"includePath": ["include"],
"compileCommands": "${workspaceFolder}/.vscode/compile_commands.json"
}
],
"version": 4

21
LICENSE.txt Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 Simon Pribylski, Thibaut Alessi, Houssem Zammali, Julien Chataigner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -10,11 +10,11 @@ class Matrix;
namespace Gauss {
/**
* \brief Echelonne une matrice en utilisant l'algorithme de Gauss-Jordan
* \param mat La matrice à échelonner
* \param reduite Mets des 0 au dessus des pivots
* \param normalise Mets les pivots à 1
* \brief Echelonne une matrice en ligne en utilisant l'algorithme de Gauss-Jordan
* \param a_Matrix La matrice à échelonner
* \param a_Reduite Mets des 0 au dessus des pivots
* \param a_Normalise Mets les pivots à 1
*/
void GaussJordan(Matrix& mat, bool reduite, bool normalise);
void GaussJordan(Matrix& a_Matrix, bool a_Reduite, bool a_Normalise);
} // namespace Gauss

View File

@@ -10,13 +10,16 @@
#include <string>
#include <vector>
#include "NR.h"
/**
* \class Matrix
* \brief Représente une matrice d'éléments
*/
class Matrix {
public:
typedef long double Element;
typedef NR Element;
typedef std::vector<Element>::iterator iterator;
private:
std::size_t m_Raws;
@@ -30,11 +33,11 @@ class Matrix {
Matrix() : m_Raws(0), m_Columns(0) {}
/**
* \brief Construit une matrice de taille donnée
* \param raws Le nombre de lignes
* \param columns Le nombre de colonne
* \brief Construit une matrice de taille donnée remplie de données aléatoires (et peut-être invalides !)
* \param a_Raws Le nombre de lignes
* \param a_Columns Le nombre de colonne
*/
Matrix(std::size_t raws, std::size_t columns);
Matrix(std::size_t a_Raws, std::size_t a_Columns);
/**
* \brief Construit une matrice de taille donnée avec des éléments donnés.\n
@@ -42,21 +45,21 @@ class Matrix {
* \code Matrix(2, 2, {1, 2, 3, 4}) \endcode construit la matrice \n
* [1, 2]\n
* [3, 4]
* \param raws Le nombre de lignes
* \param columns Le nombre de colonne
* \param initList Les élements à mettre
* \param a_Raws Le nombre de lignes
* \param a_Columns Le nombre de colonne
* \param a_Elements Les élements à mettre
*/
Matrix(std::size_t raws, std::size_t columns, std::initializer_list<Element>&& initList);
Matrix(std::size_t a_Raws, std::size_t a_Columns, std::initializer_list<Element>&& a_Elements);
~Matrix() {}
/**
* \brief Retourne le nombre de ligne de la matrice
* \brief Retourne le nombre de lignes de la matrice
*/
std::size_t GetRawCount() const;
/**
* \brief Retourne le nombre de colonne de la matrice
* \brief Retourne le nombre de colonnes de la matrice
*/
std::size_t GetColumnCount() const;
@@ -66,52 +69,65 @@ class Matrix {
void Transpose();
/**
* \brief Augmente la matrice actuelle avec une autre
* \param right Une matrice avec le bon nombre de lignes
* \pre GetRawCount() = right.GetRawCount()
* \brief Augmente la matrice actuelle à droite avec une autre
* \param a_Right Une matrice avec le bon nombre de lignes
* \pre Les deux matrices doivent avoir le même nombre de lignes
*/
void Augment(const Matrix& right);
void Augment(const Matrix& a_Right);
/**
* \brief Augmente la matrice actuelle en dessous avec une autre
* \param a_Bottom Une matrice avec le bon nombre de colonnes
* \pre Les deux matrices doivent avoir le même nombre de colonnes
*/
void AugmentBottom(const Matrix& a_Bottom);
/**
* \brief Affecte tous les coefficients de la matrice à un élément
* \param a_Element L'élément à affecter
*/
void Fill(Element a_Element);
/**
* \brief Retourne la sous-matrice spécifiée
* \param raw_origin L'indice de la première ligne de la matrice à récupérer
* \param column_origin L'indice de la première colonne de la matrice à récupérer
* \param raw Le nombre de lignes de la sous-matrice
* \param column Le nombre de colonnes de la sous-matrice
* \pre raw_origin + raw <= GetRawCount()
* \pre column_origin + column <= GetColumnCount()
* \param a_RawOrigin L'indice de la première ligne de la matrice à récupérer
* \param a_ColumnOrigin L'indice de la première colonne de la matrice à récupérer
* \param a_RawCount Le nombre de lignes de la sous-matrice
* \param a_ColumnCount Le nombre de colonnes de la sous-matrice
* \pre a_RawOrigin + a_RawCount <= GetRawCount()
* \pre a_ColumnOrigin + a_ColumnCount <= GetColumnCount()
*/
Matrix SubMatrix(std::size_t raw_origin, std::size_t column_origin, std::size_t raw, std::size_t column) const;
Matrix SubMatrix(std::size_t a_RawOrigin, std::size_t a_ColumnOrigin, std::size_t a_RawCount, std::size_t a_ColumnCount) const;
Matrix operator+(const Matrix& other) const;
Matrix operator-(const Matrix& other) const;
Matrix operator+(const Matrix& a_Other) const;
Matrix operator-(const Matrix& a_Other) const;
bool operator==(const Matrix& other) const;
bool operator==(const Matrix& a_Other) const;
/**
* \brief Effectue un produit matriciel
*/
Matrix operator*(const Matrix& other) const;
Matrix operator*(const Matrix& a_Other) const;
/**
* \brief Retourne l'élément à l'indice recherché
* \param raw L'indice de la ligne
* \param column L'indice de la colonne
* \param a_Raw L'indice de la ligne
* \param a_Column L'indice de la colonne
*/
Element& at(std::size_t raw, std::size_t column);
Element& at(std::size_t a_Raw, std::size_t a_Column);
/**
* \brief Retourne l'élément à l'indice recherché (version constante)
* \param raw L'indice de la ligne
* \param column L'indice de la colonne
* \param a_Raw L'indice de la ligne
* \param a_Column L'indice de la colonne
*/
Element at(std::size_t raw, std::size_t column) const;
Element at(std::size_t a_Raw, std::size_t a_Column) const;
/**
* \brief Construit une matrice identité de taille donnée
* \param size La taille de la matrice carrée
* \param a_Size La taille de la matrice carrée
*/
static Matrix Identity(std::size_t size);
static Matrix Identity(std::size_t a_Size);
/**
* \brief Construit une matrice colonne à partir de données existantes.\n
@@ -121,7 +137,7 @@ class Matrix {
* \endcode
* construit une matrice de 4 lignes et 1 colonne de coordonnées (1, 2, 3, 4)
*/
static Matrix ColumnVector(std::initializer_list<Element>&&);
static Matrix ColumnVector(std::initializer_list<Element>&& a_Elements);
/**
* \brief Construit une matrice ligne à partir de données existantes.\n
@@ -131,10 +147,30 @@ class Matrix {
* \endcode
* construit une matrice de 1 ligne et 4 colonnes de coordonnées (1, 2, 3, 4)
*/
static Matrix RawVector(std::initializer_list<Element>&&);
static Matrix RawVector(std::initializer_list<Element>&& a_Elements);
iterator begin();
iterator end();
iterator GetLineIterator(std::size_t a_Raw);
};
template <typename T>
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 NR& var) {
return var == 0;
}

View File

@@ -3,17 +3,20 @@
#include <iostream>
class NR {
public:
using Int = long long;
private:
int m_Numerator;
int m_Denominator; // has to be > 0, sign is carried by the numerator
Int m_Numerator;
Int m_Denominator; // has to be > 0, sign is carried by the numerator
public:
NR();
NR(int entier);
NR(int numerator, int denominator); // check if denominator != 0
NR(Int entier);
NR(Int numerator, Int denominator); // check if denominator != 0
int GetNumerator() const;
int GetDenominator() const;
Int GetNumerator() const;
Int GetDenominator() const;
bool operator==(const NR& opNR) const;
bool operator<(const NR& opNR) const;
@@ -43,5 +46,3 @@ class NR {
private:
void Reduce();
};
int PGCD(int x, int y);

View File

@@ -12,40 +12,33 @@
* \brief Permet d'obtenir différentes propriétés d'une matrice comme l'image ou le noyau
*/
class Solver {
private:
Matrix m_Matrix;
public:
/**
* \brief Initialise le resolveur
* \param mat La matrice d'entrée
*/
Solver(const Matrix& mat);
~Solver() {}
/**
* \brief Calcule l'image de la matrice d'entrée
* \brief Calcule l'image d'une matrice
* \param a_Matrix La matrice à traiter
* \return L'espace vectoriel correspondant
*/
Vect Image() const;
Vect Image(Matrix&& a_Matrix) const;
/**
* \brief Calcule le noyau de la matrice d'entrée
* \brief Calcule le noyau d'une matrice
* \param a_Matrix La matrice à traiter
* \return L'espace vectoriel correspondant
*/
Vect Kernel() const;
Vect Kernel(Matrix&& a_Matrix) const;
/**
* \brief Résout le système triangulaire de la forme AX=B, avec X et B, des vecteurs colonne.
* La matrice d'entrée est considéré comme étant la matrice augmenté [A|B]
* \brief Résout le système rectangulaire de la forme AX=B, avec X et B, des vecteurs colonne.
* \param a_MatrixA La matrice jouant le rôle de A
* \param a_VectorB La matrice colonne jouant le rôle de B
* \return L'espace affine associé
*/
VectAffine TriangularSystem() const;
VectAffine RectangularSystem(Matrix&& a_MatrixA, const Matrix& a_VectorB) const;
/**
* \brief Calcule le rang de la matrice
* \note Ceci équivaut à \code Image().GetCardinal() \endcode
* \brief Calcule le rang d'une matrice
* \param a_Matrix La matrice à traiter
* \note Ceci équivaut à \code Image(a_Matrix).GetCardinal() \endcode
*/
std::size_t Rank() const;
std::size_t Rank(Matrix&& a_Matrix) const;
};

View File

@@ -19,16 +19,16 @@ class Vect {
/**
* \brief Construit une base d'un espace vectoriel à partir des colonnes d'une matrice.
* Les colonnes de 0 sont ignorées
* \param mat Une matrice échelonnée.
* \param a_Matrix Une matrice échelonnée.
*/
Vect(const Matrix& mat);
Vect(Matrix&& a_Matrix);
/**
* \brief Permet d'obtenir le ieme vecteur de la base
* \param index l'index du vecteur souhaité
* \param a_Index l'index du vecteur souhaité
* \return Une matrice colonne
*/
Matrix GetVector(std::size_t index) const;
Matrix GetVector(std::size_t a_Index) const;
/**
* \brief Retourne le nombre de coordonnées des vecteurs de la base (leur nombre de colonne)
@@ -48,21 +48,23 @@ class Vect {
/**
* \brief Concatène la base actuelle avec un nouveau vecteur
* \param vec Une matrice colonne de taille GetDimension()
* \param a_Vector Une matrice colonne de taille GetDimension()
*/
void AddVector(const Matrix& vec);
void AddVector(const Matrix& a_Vector);
/**
* \brief Vérifie si le vecteur spécifié appartient au sous-espace vectoriel
* \param vec Une matrice colonne représentant le vecteur à tester
* \param a_Vector Une matrice colonne représentant le vecteur à tester
*/
bool IsElementOf(const Matrix& vec) const;
bool IsElementOf(const Matrix& a_Vector) const;
bool operator==(const Vect& other) const;
bool operator!=(const Vect& other) const;
bool operator==(const Vect& a_Other) const;
bool operator!=(const Vect& a_Other) const;
private:
void Simplify();
friend class VectAffine;
};
/**
@@ -77,10 +79,10 @@ class VectAffine {
public:
/**
* \brief Construit un espace affine à partir d'un espace vectoriel et d'une origine
* \param base La base de l'espace vectoriel
* \param origin Le vecteur d'origine (matrice colonne)
* \param a_Base La base de l'espace vectoriel
* \param a_Origin Le vecteur d'origine (matrice colonne)
*/
VectAffine(const Vect& base, const Matrix& origin);
VectAffine(const Vect& a_Base, const Matrix& a_Origin);
/**
* \brief Retourne l'espace vectoriel correspondant
@@ -99,11 +101,11 @@ class VectAffine {
/**
* \brief Vérifie si le vecteur spécifié appartient à l'espace affine
* \param vec Une matrice colonne représentant le vecteur à tester
* \param a_Vector Une matrice colonne représentant le vecteur à tester
*/
bool IsElementOf(const Matrix& vec) const;
bool IsElementOf(const Matrix& a_Vector) const;
bool operator==(const VectAffine& vect) const {
return m_Origin == vect.GetOrigin() && m_Base == vect.GetBase();
bool operator==(const VectAffine& a_VectAffine) const {
return m_Origin == a_VectAffine.GetOrigin() && m_Base == a_VectAffine.GetBase();
};
};

View File

@@ -2,18 +2,20 @@
#include "Matrix.h"
#include <algorithm>
#include <execution>
#include <ranges>
namespace Gauss {
static void SwapLines(Matrix& mat, std::size_t line1, std::size_t line2) {
for (std::size_t k = 0; k < mat.GetColumnCount(); k++) {
std::swap(mat.at(line1, k), mat.at(line2, k));
}
std::swap_ranges(
std::execution::par_unseq, mat.GetLineIterator(line1), mat.GetLineIterator(line1 + 1), mat.GetLineIterator(line2));
}
static void DivideLine(Matrix& mat, std::size_t line, Matrix::Element number) {
for (std::size_t j = 0; j < mat.GetColumnCount(); j++) {
mat.at(line, j) /= number;
}
std::transform(std::execution::par_unseq, mat.GetLineIterator(line), mat.GetLineIterator(line + 1), mat.GetLineIterator(line),
[number](Matrix::Element e) { return e /= number; });
}
static int FirstNotNullElementIndexOnColumn(Matrix& mat, std::size_t column, std::size_t startLine = 0) {
@@ -29,17 +31,19 @@ static void SimplifyLine(Matrix& mat, std::size_t line, std::size_t pivot_line,
const Matrix::Element pivot = mat.at(pivot_line, pivot_column);
const Matrix::Element anul = mat.at(line, pivot_column);
for (std::size_t j = 0; j < mat.GetColumnCount(); j++) {
auto range = std::views::iota(static_cast<std::size_t>(0), mat.GetColumnCount());
std::for_each(std::execution::par_unseq, range.begin(), range.end(), [&mat, pivot, anul, line, pivot_line](std::size_t j) {
mat.at(line, j) = mat.at(line, j) * pivot - mat.at(pivot_line, j) * anul;
}
});
}
void GaussJordan(Matrix& mat, bool reduite, bool normalise) {
static void GaussJordanReduced(Matrix& a_Matrix, bool a_Normalise) {
int indice_ligne_pivot = -1;
for (std::size_t j = 0; j < mat.GetColumnCount(); j++) {
for (std::size_t j = 0; j < a_Matrix.GetColumnCount(); j++) {
int indice_ligne_pivot_trouve = FirstNotNullElementIndexOnColumn(mat, j, indice_ligne_pivot + 1);
int indice_ligne_pivot_trouve = FirstNotNullElementIndexOnColumn(a_Matrix, j, indice_ligne_pivot + 1);
if (indice_ligne_pivot_trouve < 0) // colonne de 0
continue; // on regarde la prochaine colonne
@@ -47,23 +51,63 @@ void GaussJordan(Matrix& mat, bool reduite, bool normalise) {
indice_ligne_pivot++;
if (indice_ligne_pivot_trouve != indice_ligne_pivot) {
SwapLines(mat, indice_ligne_pivot_trouve, indice_ligne_pivot);
SwapLines(a_Matrix, indice_ligne_pivot_trouve, indice_ligne_pivot);
}
Matrix::Element pivot = mat.at(indice_ligne_pivot, j);
Matrix::Element pivot = a_Matrix.at(indice_ligne_pivot, j);
if (normalise) {
DivideLine(mat, indice_ligne_pivot, pivot);
if (a_Normalise) {
DivideLine(a_Matrix, indice_ligne_pivot, pivot);
}
auto range = std::views::iota(static_cast<std::size_t>(0), a_Matrix.GetRawCount());
// On simplifie les autres lignes
for (std::size_t i = (reduite ? 0 : j); i < mat.GetRawCount(); i++) {
// Pour les lignes autre que la ligne pivot
std::for_each(std::execution::par_unseq, range.begin(), range.end(), [&a_Matrix, j, indice_ligne_pivot](std::size_t i) {
if (i != static_cast<std::size_t>(indice_ligne_pivot)) {
SimplifyLine(mat, i, indice_ligne_pivot, j);
}
SimplifyLine(a_Matrix, i, indice_ligne_pivot, j);
}
});
}
}
static void GaussJordanTriangular(Matrix& a_Matrix, bool a_Normalise) {
int indice_ligne_pivot = -1;
for (std::size_t j = 0; j < a_Matrix.GetColumnCount(); j++) {
int indice_ligne_pivot_trouve = FirstNotNullElementIndexOnColumn(a_Matrix, j, indice_ligne_pivot + 1);
if (indice_ligne_pivot_trouve < 0) // colonne de 0
continue; // on regarde la prochaine colonne
indice_ligne_pivot++;
if (indice_ligne_pivot_trouve != indice_ligne_pivot) {
SwapLines(a_Matrix, indice_ligne_pivot_trouve, indice_ligne_pivot);
}
Matrix::Element pivot = a_Matrix.at(indice_ligne_pivot, j);
if (a_Normalise) {
DivideLine(a_Matrix, indice_ligne_pivot, pivot);
}
auto range = std::views::iota(static_cast<std::size_t>(indice_ligne_pivot + 1), a_Matrix.GetRawCount());
// On simplifie les autres lignes après la ligne du pivot
std::for_each(std::execution::par_unseq, range.begin(), range.end(),
[&a_Matrix, indice_ligne_pivot, j](std::size_t i) {
SimplifyLine(a_Matrix, i, indice_ligne_pivot, j);
});
}
}
void GaussJordan(Matrix& a_Matrix, bool a_Reduite, bool a_Normalise) {
if (a_Reduite)
GaussJordanReduced(a_Matrix, a_Normalise);
else
GaussJordanTriangular(a_Matrix, a_Normalise);
}
} // namespace Gauss

View File

@@ -7,29 +7,26 @@
#include <fstream>
#include <iostream>
Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Raws(lignes), m_Columns(colonnes) {
Matrix::Matrix(std::size_t a_Raws, std::size_t a_Columns) : m_Raws(a_Raws), m_Columns(a_Columns) {
m_Data.resize(m_Raws * m_Columns);
}
Matrix::Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list<Element>&& initList) :
m_Raws(lignes), m_Columns(colonnes) {
m_Data = initList;
Matrix::Matrix(std::size_t a_Raws, std::size_t a_Columns, std::initializer_list<Element>&& a_Elements) :
m_Raws(a_Raws), m_Columns(a_Columns) {
m_Data = a_Elements;
m_Data.resize(m_Raws * m_Columns);
}
Matrix Matrix::operator*(const Matrix& other) const {
if (m_Columns != other.m_Raws) {
std::cerr << "Mutiplication impossible car la dimensions des matrices est incompatible" << std::endl;
return {};
}
Matrix Matrix::operator*(const Matrix& a_Other) const {
assert(m_Columns == a_Other.m_Raws);
Matrix result(m_Raws, other.m_Columns);
Matrix result(m_Raws, a_Other.m_Columns);
for (std::size_t i = 0; i < m_Raws; ++i) {
for (std::size_t j = 0; j < other.m_Columns; ++j) {
for (std::size_t j = 0; j < a_Other.m_Columns; ++j) {
Element sum = 0;
for (std::size_t k = 0; k < m_Columns; k++) {
sum += at(i, k) * other.at(k, j);
sum += at(i, k) * a_Other.at(k, j);
}
result.at(i, j) = sum;
}
@@ -47,35 +44,39 @@ void Matrix::Transpose() {
*this = result;
}
Matrix Matrix::Identity(std::size_t taille) {
Matrix id {taille, taille};
for (std::size_t i = 0; i < taille; i++) {
for (std::size_t j = i; j < taille; j++) {
Matrix Matrix::Identity(std::size_t a_Size) {
Matrix id {a_Size, a_Size};
for (std::size_t i = 0; i < a_Size; i++) {
for (std::size_t j = i; j < a_Size; j++) {
id.at(i, j) = (i == j);
}
}
return id;
}
Matrix Matrix::ColumnVector(std::initializer_list<Element>&& initList) {
Matrix result {initList.size(), 1};
Matrix Matrix::ColumnVector(std::initializer_list<Element>&& a_Elements) {
Matrix result {a_Elements.size(), 1};
result.m_Data = initList;
result.m_Data = a_Elements;
return result;
}
Matrix Matrix::RawVector(std::initializer_list<Element>&& initList) {
Matrix result {1, initList.size()};
Matrix Matrix::RawVector(std::initializer_list<Element>&& a_Elements) {
Matrix result {1, a_Elements.size()};
result.m_Data = initList;
result.m_Data = a_Elements;
return result;
}
void Matrix::Augment(const Matrix& droite) {
assert(droite.m_Raws == m_Raws);
Matrix temp {m_Raws, m_Columns + droite.m_Columns};
void Matrix::Fill(Element a_Element) {
std::fill(GetLineIterator(0), GetLineIterator(m_Raws), a_Element);
}
void Matrix::Augment(const Matrix& a_Right) {
assert(a_Right.m_Raws == m_Raws);
Matrix temp {m_Raws, m_Columns + a_Right.m_Columns};
for (std::size_t i = 0; i < m_Raws; i++) {
for (std::size_t j = 0; j < m_Columns; j++) {
@@ -84,49 +85,67 @@ void Matrix::Augment(const Matrix& droite) {
}
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);
for (std::size_t j = 0; j < a_Right.m_Columns; j++) {
temp.at(i, j + m_Columns) = a_Right.at(i, j);
}
}
*this = temp;
}
Matrix Matrix::operator+(const Matrix& other) const {
assert(GetColumnCount() == other.GetColumnCount() && GetRawCount() == other.GetRawCount());
Matrix result = *this;
for (std::size_t i = 0; i < GetRawCount(); i++) {
for (std::size_t j = 0; j < GetColumnCount(); j++) {
result.at(i, j) += other.at(i, j);
}
}
return result;
}
Matrix Matrix::operator-(const Matrix& other) const {
assert(GetColumnCount() == other.GetColumnCount() && GetRawCount() == other.GetRawCount());
Matrix result = *this;
for (std::size_t i = 0; i < GetRawCount(); i++) {
for (std::size_t j = 0; j < GetColumnCount(); j++) {
result.at(i, j) -= other.at(i, j);
}
}
return result;
}
bool Matrix::operator==(const Matrix& other) const {
if (m_Raws != other.m_Raws || m_Columns != other.m_Columns)
return false;
void Matrix::AugmentBottom(const Matrix& a_Bottom) {
assert(a_Bottom.m_Columns == m_Columns);
Matrix temp {m_Raws + a_Bottom.GetRawCount(), m_Columns};
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)))
temp.at(i, j) = at(i, j);
}
}
for (std::size_t i = 0; i < a_Bottom.GetRawCount(); i++) {
for (std::size_t j = 0; j < GetColumnCount(); j++) {
temp.at(i + GetRawCount(), j) = a_Bottom.at(i, j);
}
}
*this = temp;
}
Matrix Matrix::operator+(const Matrix& a_Other) const {
assert(GetColumnCount() == a_Other.GetColumnCount() && GetRawCount() == a_Other.GetRawCount());
Matrix result = *this;
for (std::size_t i = 0; i < GetRawCount(); i++) {
for (std::size_t j = 0; j < GetColumnCount(); j++) {
result.at(i, j) += a_Other.at(i, j);
}
}
return result;
}
Matrix Matrix::operator-(const Matrix& a_Other) const {
assert(GetColumnCount() == a_Other.GetColumnCount() && GetRawCount() == a_Other.GetRawCount());
Matrix result = *this;
for (std::size_t i = 0; i < GetRawCount(); i++) {
for (std::size_t j = 0; j < GetColumnCount(); j++) {
result.at(i, j) -= a_Other.at(i, j);
}
}
return result;
}
bool Matrix::operator==(const Matrix& a_Other) const {
assert(m_Raws == a_Other.m_Raws && m_Columns == a_Other.m_Columns);
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) - a_Other.at(i, j)))
return false;
}
}
@@ -134,12 +153,14 @@ bool Matrix::operator==(const Matrix& other) const {
return true;
}
Matrix::Element& Matrix::at(std::size_t ligne, std::size_t colonne) {
return m_Data[ligne * m_Columns + colonne];
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 ligne, std::size_t colonne) const {
return m_Data[ligne * m_Columns + colonne];
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];
}
std::size_t Matrix::GetRawCount() const {
@@ -150,16 +171,29 @@ std::size_t Matrix::GetColumnCount() const {
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_Raws >= origine_ligne + ligne && m_Columns >= origine_colonne + colonne);
Matrix Matrix::SubMatrix(
std::size_t a_RawOrigin, std::size_t a_ColumnOrigin, std::size_t a_RawCount, std::size_t a_ColumnCount) const {
assert(m_Raws >= a_RawOrigin + a_RawCount && m_Columns >= a_ColumnOrigin + a_ColumnCount);
Matrix result {ligne, colonne};
Matrix result {a_RawCount, a_ColumnCount};
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);
for (std::size_t i = 0; i < result.GetRawCount(); i++) {
for (std::size_t j = 0; j < result.GetColumnCount(); j++) {
result.at(i, j) = at(i + a_RawOrigin, j + a_ColumnOrigin);
}
}
return result;
}
Matrix::iterator Matrix::begin() {
return m_Data.begin();
}
Matrix::iterator Matrix::end() {
return m_Data.end();
}
Matrix::iterator Matrix::GetLineIterator(std::size_t a_Raw) {
return m_Data.begin() + a_Raw * GetColumnCount();
}

View File

@@ -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;
}

View File

@@ -2,54 +2,69 @@
#include "Gauss.h"
Solver::Solver(const Matrix& mat) : m_Matrix(mat) {}
static int FirstNotNullElementIndexOnLine(const Matrix& mat, std::size_t line) {
for (std::size_t i = 0; i < mat.GetColumnCount(); i++) {
if (!IsEqualZero(mat.at(line, i))) {
return i;
}
}
return -1;
}
Vect Solver::Image() const {
Matrix result = m_Matrix;
result.Transpose();
Gauss::GaussJordan(result, true, true);
result.Transpose();
return {result};
Vect Solver::Image(Matrix&& a_Matrix) const {
a_Matrix.Transpose();
Gauss::GaussJordan(a_Matrix, false, false);
a_Matrix.Transpose();
return {std::move(a_Matrix)};
}
// https://en.wikipedia.org/wiki/Kernel_(linear_algebra)#Computation_by_Gaussian_elimination
Vect Solver::Kernel() const {
Matrix result = m_Matrix;
result.Transpose();
result.Augment(Matrix::Identity(result.GetRawCount()));
Gauss::GaussJordan(result, true, true);
result.Transpose();
Vect Solver::Kernel(Matrix&& a_Matrix) const {
std::size_t matrixRawCount = a_Matrix.GetRawCount();
std::size_t matrixColumnCount = a_Matrix.GetColumnCount();
a_Matrix.Transpose();
a_Matrix.Augment(Matrix::Identity(a_Matrix.GetRawCount()));
Gauss::GaussJordan(a_Matrix, false, true);
a_Matrix.Transpose();
// nombre de colonnes non nulles
std::size_t origine_colonne = Vect(result.SubMatrix(0, 0, m_Matrix.GetRawCount(), m_Matrix.GetColumnCount())).GetCardinal();
std::size_t origine_colonne = Vect(a_Matrix.SubMatrix(0, 0, matrixRawCount, matrixColumnCount)).GetCardinal();
return {result.SubMatrix(m_Matrix.GetRawCount(), origine_colonne, result.GetRawCount() - m_Matrix.GetRawCount(),
result.GetColumnCount() - origine_colonne)};
return {a_Matrix.SubMatrix(
matrixRawCount, origine_colonne, a_Matrix.GetRawCount() - matrixRawCount, a_Matrix.GetColumnCount() - origine_colonne)};
}
VectAffine Solver::TriangularSystem() const {
Matrix mat = m_Matrix;
VectAffine Solver::RectangularSystem(Matrix&& a_MatrixA, const Matrix& a_VectorB) const {
Matrix mat = a_MatrixA;
mat.Augment(a_VectorB);
Gauss::GaussJordan(mat, true, true);
Solver solver {mat.SubMatrix(0, 0, mat.GetRawCount(), mat.GetColumnCount() - 1)};
Solver solver;
Vect noyau = solver.Kernel();
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
// on calcule le vecteur qui dirige l'espace affine
Matrix fullOrigin {mat.GetColumnCount() - 1, 1};
for (int i = 0; i < mat.GetRawCount(); i++) {
fullOrigin.at(i, 0) = origin.at(i, 0);
for (std::size_t i = 0; i < mat.GetRawCount(); i++) {
int pivot_index = FirstNotNullElementIndexOnLine(mat, i);
if (static_cast<std::size_t>(pivot_index) == mat.GetColumnCount() - 1) {
// on a une ligne du type 0 = n. Aucune solution !
return {Matrix {}, Matrix::ColumnVector({0})};
}
for (int i = mat.GetRawCount(); i < mat.GetColumnCount() - 1; i++) {
fullOrigin.at(i, 0) = 0;
// ligne entière de 0
if (pivot_index < 0)
continue;
fullOrigin.at(pivot_index, 0) = origin.at(i, 0);
}
return {noyau, fullOrigin};
}
std::size_t Solver::Rank() const {
return Image().GetCardinal();
std::size_t Solver::Rank(Matrix&& a_Matrix) const {
return Image(std::move(a_Matrix)).GetCardinal();
}

View File

@@ -2,8 +2,6 @@
#include "Gauss.h"
#include "Solver.h"
#include <cassert>
#include <iostream>
static bool IsColumnNull(Matrix& mat, std::size_t column) {
for (std::size_t i = 0; i < mat.GetRawCount(); i++) {
@@ -14,7 +12,10 @@ static bool IsColumnNull(Matrix& mat, std::size_t column) {
return true;
}
Vect::Vect(const Matrix& mat) : m_Data(mat) {
Vect::Vect(Matrix&& a_Matrix) : m_Data(std::move(a_Matrix)) {
m_Data.Transpose();
Gauss::GaussJordan(m_Data, false, true);
m_Data.Transpose();
Simplify();
}
@@ -29,61 +30,61 @@ void Vect::Simplify() {
m_Data = mat;
}
Matrix Vect::GetVector(std::size_t index) const {
return m_Data.SubMatrix(0, index, m_Data.GetRawCount(), 1);
Matrix Vect::GetVector(std::size_t a_Index) const {
return m_Data.SubMatrix(0, a_Index, m_Data.GetRawCount(), 1);
}
std::size_t Vect::GetCardinal() const {
return m_Data.GetColumnCount();
}
bool Vect::IsElementOf(const Matrix& vec) const {
bool Vect::IsElementOf(const Matrix& a_Vector) const {
Vect base = *this;
base.AddVector(vec);
base.AddVector(a_Vector);
return base.GetCardinal() == GetCardinal();
}
bool Vect::operator==(const Vect& other) const {
if (GetDimension() != other.GetDimension() || GetCardinal() != other.GetCardinal())
bool Vect::operator==(const Vect& a_Other) const {
if (GetDimension() != a_Other.GetDimension() || GetCardinal() != a_Other.GetCardinal())
return false;
// on vérifie si chaque vecteur de la deuxième base appartient à l'espace vectoriel engendré par la première base
for (std::size_t i = 0; i < GetCardinal(); i++) {
if (!IsElementOf(other.GetVector(i)))
if (!IsElementOf(a_Other.GetVector(i)))
return false;
}
return true;
}
void Vect::AddVector(const Matrix& mat) {
m_Data.Augment(mat);
void Vect::AddVector(const Matrix& a_Vector) {
m_Data.Augment(a_Vector);
m_Data.Transpose();
Gauss::GaussJordan(m_Data, false, false);
m_Data.Transpose();
Simplify();
}
bool Vect::operator!=(const Vect& other) const {
return !(*this == other);
bool Vect::operator!=(const Vect& a_Other) const {
return !(*this == a_Other);
}
Matrix Vect::GetLinearSystem() const {
Matrix vect = m_Data;
vect.Transpose();
Solver solver {vect};
vect = solver.Kernel().m_Data;
vect.Transpose();
return vect;
Solver solver;
Matrix result = solver.Kernel(std::move(vect)).m_Data;
result.Transpose();
return result;
}
std::size_t Vect::GetDimension() const {
return m_Data.GetRawCount();
}
VectAffine::VectAffine(const Vect& base, const Matrix& origine) :
m_Base(base), m_Origin(origine.SubMatrix(0, 0, m_Base.GetDimension(), 1)) {}
VectAffine::VectAffine(const Vect& a_Base, const Matrix& a_Origin) :
m_Base(a_Base), m_Origin(a_Origin.SubMatrix(0, 0, m_Base.GetDimension(), 1)) {}
bool VectAffine::IsElementOf(const Matrix& vec) const {
return m_Base.IsElementOf(vec - m_Origin);
bool VectAffine::IsElementOf(const Matrix& a_Vector) const {
return m_Base.IsElementOf(a_Vector - m_Origin);
}

311
src/gui/PivotGui.cpp Normal file
View File

@@ -0,0 +1,311 @@
#include "PivotGui.h"
#include "Gauss.h"
#include "Matrix.h"
#include "Solver.h"
#include <imgui.h>
#include <sstream>
static std::string equationsResultImage;
struct GuiMatrix {
std::vector<std::vector<int>> matrixValues;
int matrixSizeX = 4;
int matrixSizeY = 4;
};
static void ResizeGuiMatrix(bool refresh, GuiMatrix& guiMatrix) {
if (refresh) {
guiMatrix.matrixValues.resize(guiMatrix.matrixSizeY);
for (auto& row : guiMatrix.matrixValues) {
row.resize(guiMatrix.matrixSizeX, 0);
}
}
}
static void RenderMatrix(bool& refresh, GuiMatrix& guiMatrix) {
ResizeGuiMatrix(refresh, guiMatrix);
for (int y = 0; y < guiMatrix.matrixSizeY; y++) {
for (int x = 0; x < guiMatrix.matrixSizeX; x++) {
if (x > 0)
ImGui::SameLine();
ImGui::PushID((guiMatrix.matrixSizeX * 20 + guiMatrix.matrixSizeY * 50) + y * guiMatrix.matrixSizeX + x);
ImGui::PushItemWidth(60); // Adjust this value to change the cell size
if (ImGui::InputInt("", &guiMatrix.matrixValues[y][x], 0, 0, ImGuiInputTextFlags_CharsDecimal))
refresh = true;
ImGui::PopItemWidth();
ImGui::PopID();
}
}
}
static Matrix LoadMatrixFromStdVect(const std::vector<std::vector<int>>& data) {
Matrix result {data.size(), data.empty() ? 0 : data[0].size()};
for (std::size_t i = 0; i < result.GetRawCount(); i++) {
for (std::size_t j = 0; j < result.GetColumnCount(); j++) {
result.at(i, j) = static_cast<Matrix::Element>(data[i][j]);
}
}
return result;
}
static std::string ElementToString(Matrix::Element e) {
std::stringstream ss;
ss << e;
return ss.str();
}
static std::string PrintRawMatrix(const Matrix& mat) {
if (mat.GetRawCount() == 0)
return "";
std::string result = " ( ";
for (std::size_t j = 0; j < mat.GetRawCount(); j++) {
result += ElementToString(mat.at(j, 0)) + ", ";
}
result = result.substr(0, result.size() - 2);
result += " )";
return result;
}
static std::string PrintVect(const Vect& vect) {
if (vect.GetCardinal() == 0)
return "{0}";
std::string result = "Vect( ";
for (std::size_t i = 0; i < vect.GetCardinal(); i++) {
Matrix vector = vect.GetVector(i);
result += PrintRawMatrix(vector);
result += ", ";
}
result = result.substr(0, result.size() - 2);
result += " )";
return result;
}
void PivotGui::Init() {}
static void RenderLeftSystemChild(bool& refresh, GuiMatrix& system, GuiMatrix& origin) {
ImVec2 topLeftWindowSize(ImGui::GetContentRegionAvail().x * 0.5f, 0);
ImGui::BeginChild("Left Child", topLeftWindowSize, ImGuiChildFlags_Border);
ImGui::Text("Système de la forme AX=B");
ImGui::Separator();
ImGui::Text("Taille matrice A :");
if (ImGui::InputInt("##RowsMatriceInitiale", &system.matrixSizeY))
refresh = true;
system.matrixSizeY = std::max(1, system.matrixSizeY);
ImGui::SameLine();
ImGui::Text("Lignes");
if (ImGui::InputInt("##ColumnsMatriceInitiale", &system.matrixSizeX))
refresh = true;
system.matrixSizeX = std::max(1, system.matrixSizeX);
ImGui::SameLine();
ImGui::Text("Colonnes");
ImGui::NewLine();
RenderMatrix(refresh, system);
if (refresh) {
origin.matrixSizeX = 1;
origin.matrixSizeY = system.matrixSizeY;
}
ImGui::NewLine();
ImGui::Separator();
ImGui::Text("Matrice B :");
ImGui::NewLine();
RenderMatrix(refresh, origin);
ImGui::EndChild();
}
static void RenderRightSystemChild(bool& refresh, GuiMatrix& system, GuiMatrix& origin) {
ImGui::BeginChild("Right Child", {0, 0}, ImGuiChildFlags_Border);
static std::string result = "";
static Solver solver;
if (refresh) {
VectAffine solutions =
solver.RectangularSystem(LoadMatrixFromStdVect(system.matrixValues), LoadMatrixFromStdVect(origin.matrixValues));
result = "Solutions :\n";
result += PrintVect(solutions.GetBase());
result += "\n\n+\n\n";
result += PrintRawMatrix(solutions.GetOrigin());
}
ImGui::TextWrapped("%s", result.c_str());
ImGui::EndChild();
}
static void RenderSystemTab() {
static GuiMatrix guiMatrix, originMatrix;
static bool refresh = true;
RenderLeftSystemChild(refresh, guiMatrix, originMatrix);
ImGui::SameLine();
RenderRightSystemChild(refresh, guiMatrix, originMatrix);
}
static void RenderLeftGaussChild(bool& refresh, GuiMatrix& guiMatrix) {
// divisions des fenetres
ImVec2 topLeftWindowSize(ImGui::GetContentRegionAvail().x * 0.5f, 0);
// Begin fenetre top left
// ImGui::SetNextWindowPos(ImVec2(0, 0)); // Position at the top-left corner
ImGui::BeginChild("Left Top Window", topLeftWindowSize, ImGuiChildFlags_Border);
/*ImGui::Begin(, nullptr,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
*/
// ImGui::BeginTabBar("MainMenu");
// Get window position
ImVec2 windowPos = ImGui::GetWindowPos();
ImGui::Text("Matrice initiale:");
if (ImGui::InputInt("##RowsMatriceInitiale", &guiMatrix.matrixSizeY))
refresh = true;
guiMatrix.matrixSizeY = std::max(1, guiMatrix.matrixSizeY);
ImGui::SameLine();
ImGui::Text("Lignes");
if (ImGui::InputInt("##ColumnsMatriceInitiale", &guiMatrix.matrixSizeX))
refresh = true;
guiMatrix.matrixSizeX = std::max(1, guiMatrix.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
RenderMatrix(refresh, guiMatrix);
// ImGui::EndChild(); // End Matrice initiale
ImGui::NewLine();
ImGui::Text("Matrice échelonnée:");
// Convert the "result" string back to a matrix
Matrix resultMatrix = LoadMatrixFromStdVect(guiMatrix.matrixValues);
// Apply the Gauss-Jordan elimination to the matrix
Gauss::GaussJordan(resultMatrix, true, true); // Assuming you want to reduce and normalize the matrix
// Display the matrix
for (std::size_t i = 0; i < resultMatrix.GetRawCount(); i++) {
for (std::size_t j = 0; j < resultMatrix.GetColumnCount(); j++) {
ImGui::PushID(i * resultMatrix.GetColumnCount() + j);
if (ImGui::Button(ElementToString(resultMatrix.at(i, j)).c_str(), ImVec2(70, 70))) { // Adjust the size as needed
// Handle button click here if needed
}
ImGui::PopID();
if (j < resultMatrix.GetColumnCount() - 1)
ImGui::SameLine();
}
}
ImGui::EndChild();
}
static void RenderRightGaussChild(bool& refresh, GuiMatrix& guiMatrix) {
static Solver solver;
// Begin fenetre top right
// ImGui::SetNextWindowSize(topRightWindowSize);
// ImGui::SetNextWindowPos(ImVec2(windowPos.x + topLeftWindowSize.x, 0)); // Position at the top-right corner
ImGui::BeginChild("Right Top Window", {0, 0}, ImGuiChildFlags_Border);
// ImGui::Begin("Right Top Window", nullptr,
// ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
// rajouter le code pour la partie top right
static std::string result = "";
if (refresh) {
// Calculate the kernel and image
Vect image = solver.Image(LoadMatrixFromStdVect(guiMatrix.matrixValues));
Matrix linearSystem = image.GetLinearSystem();
// Store the equationsResult strings in the global variable
equationsResultImage = "Equations cartesiennes de l'espace vectoriel (Image):\n";
for (size_t i = 0; i < linearSystem.GetRawCount(); ++i) {
for (size_t j = 0; j < linearSystem.GetColumnCount(); ++j) {
equationsResultImage +=
ElementToString(linearSystem.at(i, j)) + "*" + std::string {static_cast<char>('a' + j)} + " + ";
}
equationsResultImage = equationsResultImage.substr(0, equationsResultImage.size() - 3) + " = 0\n";
}
result = "\nNoyau: \n" + PrintVect(solver.Kernel(LoadMatrixFromStdVect(guiMatrix.matrixValues))) + "\n" +
"\n" + "Rang: " + "\n" + std::to_string(solver.Rank(LoadMatrixFromStdVect(guiMatrix.matrixValues))) + "\n" + "\n" +
"Image: " + "\n" + PrintVect(image);
}
refresh = false;
// Display the equationsResult strings in the GUI if they are not empty
if (!equationsResultImage.empty()) {
ImGui::TextWrapped("%s", equationsResultImage.c_str());
}
ImGui::TextWrapped("%s", result.c_str());
ImGui::EndChild(); // End fenetre top right
}
static void RenderGaussTab() {
static GuiMatrix guiMatrix;
static bool refresh = true;
RenderLeftGaussChild(refresh, guiMatrix);
ImGui::SameLine();
RenderRightGaussChild(refresh, guiMatrix);
}
static void RenderMainWindow() {
ImGuiIO& io = ImGui::GetIO();
ImGui::SetNextWindowSize(io.DisplaySize);
ImGui::SetNextWindowPos({0, 0});
ImGui::Begin("MainWindow", nullptr,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar);
ImGui::BeginTabBar("MainBar");
if (ImGui::BeginTabItem("Noyau et Image")) {
RenderGaussTab();
ImGui::EndTabItem();
}
if (ImGui::BeginTabItem("Systèmes")) {
RenderSystemTab();
ImGui::EndTabItem();
}
ImGui::EndTabBar();
ImGui::End();
}
void PivotGui::Render() {
RenderMainWindow();
#ifndef NDEBUG
ImGui::ShowDemoWindow(nullptr);
#endif
}
void PivotGui::Destroy() {}

9
src/gui/PivotGui.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
namespace PivotGui {
void Init();
void Render();
void Destroy();
} // namespace PivotGui

174
src/gui/mainGui.cpp Normal file
View File

@@ -0,0 +1,174 @@
// Dear ImGui: standalone example application for SDL2 + OpenGL
// (SDL is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.)
// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
// - Getting Started https://dearimgui.com/getting-started
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
// - Introduction, links and more at the top of imgui.cpp
#include <SDL2/SDL.h>
#include <imgui.h>
#include <imgui_impl_opengl3.h>
#include <imgui_impl_opengl3_loader.h>
#include <imgui_impl_sdl2.h>
#include <stdio.h>
#include "PivotGui.h"
// This example can also compile and run with Emscripten! See 'Makefile.emscripten' for details.
#ifdef __EMSCRIPTEN__
#include "../libs/emscripten/emscripten_mainloop_stub.h"
#endif
// Main code
int main(int, char**) {
// Setup SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
printf("Error: %s\n", SDL_GetError());
return -1;
}
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
const char* glsl_version = "#version 100";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#elif defined(__APPLE__)
// GL 3.2 Core + GLSL 150
const char* glsl_version = "#version 150";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
#else
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif
// From 2.0.18: Enable native IME.
#ifdef SDL_HINT_IME_SHOW_UI
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif
// Create window with graphics context
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Window* window = SDL_CreateWindow("Solver Gui", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
if (window == nullptr) {
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
return -1;
}
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
ImGui::StyleColorsDark();
// ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
ImGui_ImplOpenGL3_Init(glsl_version);
// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use
// ImGui::PushFont()/PopFont() to select them.
// - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
// - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an
// assertion, or display an error and quit).
// - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling
// ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
// - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
// - Read 'docs/FONTS.md' for more instructions and details.
// - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
// - Our Emscripten build process allows embedding fonts to be accessible at runtime from the "fonts/" folder. See
// Makefile.emscripten for details.
// io.Fonts->AddFontDefault();
// io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
// io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
// io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
// io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
// ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, nullptr,
// io.Fonts->GetGlyphRangesJapanese()); IM_ASSERT(font != nullptr);
ImFontConfig cfg;
cfg.SizePixels = 30.0f;
io.Fonts->AddFontDefault(&cfg);
PivotGui::Init();
// Main loop
bool done = false;
#ifdef __EMSCRIPTEN__
// For an Emscripten build we are disabling file-system access, so let's not attempt to do a fopen() of the imgui.ini file.
// You may manually call LoadIniSettingsFromMemory() to load settings from your own storage.
io.IniFilename = nullptr;
EMSCRIPTEN_MAINLOOP_BEGIN
#else
while (!done)
#endif
{
// Poll and handle events (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy
// of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your
// copy of the keyboard data. Generally you may always pass all inputs to dear imgui, and hide them from your application based
// on those two flags.
SDL_Event event;
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
done = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE &&
event.window.windowID == SDL_GetWindowID(window))
done = true;
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
PivotGui::Render();
// Rendering
ImGui::Render();
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(window);
}
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_MAINLOOP_END;
#endif
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

View File

@@ -23,10 +23,10 @@ void test() {
Matrix mat2 = LoadMatrix("matrice4x4.mat");
Print(mat2);
Solver solver {mat2};
Solver solver;
Vect image = solver.Image();
Vect noyau = solver.Kernel();
Vect image = solver.Image(Matrix{mat2});
Vect noyau = solver.Kernel(Matrix{mat2});
std::cout << "\tImage :\n";
Print(image);
@@ -38,7 +38,7 @@ void test() {
Print(noyau.GetLinearSystem());
std::cout << "\n\n";
Print(solver.TriangularSystem());
// Print(solver.TriangularSystem(mat2));
}
void prompt() {

42
test/test_assert.h Normal file
View File

@@ -0,0 +1,42 @@
#pragma once
/**
* \file Test.h
* \brief Contient une assertion utilisable avec les optimisations
*/
#include <iostream>
#include <stdexcept>
/**
* \def TEST_SUCCESSFUL
* \brief Indique que le test a été passé
*/
#define TEST_SUCCESSFUL 0
/**
* \def TEST_FAILED
* \brief Indique que le test a échoué
*/
#define TEST_FAILED 1
#ifndef __FUNCTION_NAME__
#ifdef _WIN32
#define __FUNCTION_NAME__ __FUNCTION__
#else
#define __FUNCTION_NAME__ __PRETTY_FUNCTION__
#endif
#endif
/**
* \def test_assert
* \param ... L'expression à évaluer
* \brief Evalue une expression et arrête le programme si elle n'est pas valide
* \note Cette macro équivaut à assert() mais fonctionne également avec les optimisations activées
*/
#define test_assert(...) \
if (!static_cast<bool>(__VA_ARGS__)) { \
std::cout << __FILE__ << ":" << __LINE__ << ": " << __FUNCTION_NAME__ << ": Assertion failed !\n"; \
std::cout << " " << __LINE__ << " |\t" << #__VA_ARGS__ << std::endl; \
throw std::runtime_error("Assertion failed !"); \
}

View File

@@ -1,10 +1,6 @@
#include "Gauss.h"
#include "Matrix.h"
#include <cassert>
#ifdef NDEBUG
#error "Il faut être en debug mode ! xmake f -m debug"
#endif
#include "test_assert.h"
struct Test {
Matrix mat;
@@ -37,7 +33,7 @@ static const std::vector<Test> TEST_MATRICES = {
void test() {
for (Test test : TEST_MATRICES) {
Gauss::GaussJordan(test.mat, true, true);
assert(test.mat == test.res);
test_assert(test.mat == test.res);
}
}

View File

@@ -0,0 +1,80 @@
#include "Solver.h"
#include "test_assert.h"
#include <cstdlib>
#include <future>
#include <iostream>
#include <vector>
static constexpr int EXECUTION_COUNT = 1000;
static constexpr int KERNEL_CHECKS = 100;
static constexpr int MATRIX_MAX_SIZE = 9;
static const Solver solver;
static int GetRandomInt() {
return rand() % 11 - 5;
}
static Matrix GetRandomMatrix(std::size_t a_Raw, std::size_t a_Column) {
Matrix matrix {a_Raw, a_Column};
for (std::size_t i = 0; i < matrix.GetRawCount(); i++) {
for (std::size_t j = 0; j < matrix.GetColumnCount(); j++) {
matrix.at(i, j) = GetRandomInt();
}
}
return matrix;
}
static bool Test() {
Matrix matrix = GetRandomMatrix(rand() % MATRIX_MAX_SIZE + 1, rand() % MATRIX_MAX_SIZE + 1);
for (std::size_t i = 0; i < matrix.GetRawCount(); i++) {
for (std::size_t j = 0; j < matrix.GetColumnCount(); j++) {
matrix.at(i, j) = GetRandomInt();
}
}
Matrix copy = matrix;
Vect kernel = solver.Kernel(std::move(copy));
Matrix nullVector {matrix.GetRawCount(), 1};
nullVector.Fill(0.0);
for (std::size_t i = 0; i < kernel.GetCardinal(); i++) {
test_assert(matrix * kernel.GetVector(i) == nullVector);
}
for (std::size_t i = 0; i < KERNEL_CHECKS; i++) {
Matrix vector = GetRandomMatrix(kernel.GetDimension(), 1);
test_assert(kernel.IsElementOf(vector) == (matrix * vector == nullVector));
}
Vect kernel2 = solver.Kernel(kernel.GetLinearSystem());
test_assert(kernel == kernel2);
return true;
}
int main() {
srand(time(0));
std::vector<std::future<bool>> results;
// appelle la fonction Test() en parallèle
for (int i = 0; i < EXECUTION_COUNT; i++) {
auto handle = std::async(std::launch::async, &Test);
results.push_back(std::move(handle));
}
for (auto& result : results) {
if (!result.get())
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@@ -1,25 +1,25 @@
#include "NR.h"
#include <cassert>
#include "test_assert.h"
static void test() {
assert((NR {1, 5} == NR {5, 25}));
assert((NR {1, 5} != NR {4, 25}));
test_assert((NR {1, 5} == NR {5, 25}));
test_assert((NR {1, 5} != NR {4, 25}));
assert(NR {2} == NR {1} + 1);
assert(NR {1} == (NR {1, 4} + NR {3, 4}));
assert((NR {-3, -4} == NR {1, 2} + NR {1, 4}));
test_assert(NR {2} == NR {1} + 1);
test_assert(NR {1} == (NR {1, 4} + NR {3, 4}));
test_assert((NR {-3, -4} == NR {1, 2} + NR {1, 4}));
assert((NR {-1, 4} == NR {1, 4} - NR {1, 2}));
assert((NR {1, -4} == NR {1, 4} - NR {1, 2}));
assert((-NR {1, 4} == NR {1, 4} - NR {1, 2}));
test_assert((NR {-1, 4} == NR {1, 4} - NR {1, 2}));
test_assert((NR {1, -4} == NR {1, 4} - NR {1, 2}));
test_assert((-NR {1, 4} == NR {1, 4} - NR {1, 2}));
assert((NR {2} == NR {4, 3} * NR {3, 2}));
assert((NR {3, 5} == NR {4, 5} * NR {3, 4}));
test_assert((NR {2} == NR {4, 3} * NR {3, 2}));
test_assert((NR {3, 5} == NR {4, 5} * NR {3, 4}));
assert((NR {21, 16} == NR {7, 8} / NR {6, 9}));
test_assert((NR {21, 16} == NR {7, 8} / NR {6, 9}));
assert((NR {4, 3} == NR {3, 4}.Inverse()));
test_assert((NR {4, 3} == NR {3, 4}.Inverse()));
}
int main(int argc, char** argv) {

View File

@@ -1,14 +1,59 @@
#include <cassert>
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include "IO.h"
#include "Solver.h"
#include "test_assert.h"
namespace fs = std::filesystem;
int main() {
const static int EXECUTION_COUNT = 10000;
static constexpr int MATRIX_MAX_SIZE = 5;
static int GetRandomSize() {
return rand() % MATRIX_MAX_SIZE + 1;
}
static int GetRandomInt() {
return GetRandomSize();
}
static Matrix GetRandomMatrix(std::size_t a_Raw, std::size_t a_Column) {
Matrix matrix {a_Raw, a_Column};
std::generate(matrix.GetLineIterator(0), matrix.GetLineIterator(a_Raw), []() {
return GetRandomInt();
});
return matrix;
}
void TestRectangular(const Matrix& system, const Matrix& origin) {
Solver solver;
VectAffine solution = solver.RectangularSystem(std::move(Matrix(system)), origin);
for (std::size_t i = 0; i < solution.GetBase().GetCardinal(); i++) {
Matrix vector = solution.GetBase().GetVector(i) + solution.GetOrigin();
Matrix product = system * vector;
test_assert(product == origin);
}
}
void RandomRectangular() {
for (int i = 0; i < EXECUTION_COUNT; i++) {
Matrix system = GetRandomMatrix(GetRandomSize(), GetRandomSize());
Matrix origin = GetRandomMatrix(system.GetRawCount(), 1);
TestRectangular(system, origin);
}
}
void TestKernelImage() {
std::string path = "test";
for (const auto& entry : fs::directory_iterator(path)) {
std::string fileName = entry.path().string();
@@ -20,13 +65,24 @@ int main() {
Matrix mat, imageMat, noyauMat;
in >> mat >> imageMat >> noyauMat;
Vect image {imageMat};
Vect noyau {noyauMat};
Vect image {std::move(imageMat)};
Vect noyau {std::move(noyauMat)};
Solver solver {mat};
Solver solver;
assert(solver.Image() == image);
assert(solver.Kernel() == noyau);
Matrix copy = mat;
Vect imageCalc = solver.Image(std::move(copy));
Vect kernelCalc = solver.Kernel(std::move(mat));
test_assert(imageCalc == image);
test_assert(kernelCalc == noyau);
}
}
int main() {
srand(time(0));
TestKernelImage();
RandomRectangular();
return 0;
}

View File

@@ -1,5 +1,26 @@
#include "Vect.h"
#include <cassert>
#include "test_assert.h"
#include <algorithm>
const static int EXECUTION_COUNT = 100000;
static constexpr int MATRIX_MAX_SIZE = 5;
static int GetRandomSize() {
return rand() % MATRIX_MAX_SIZE + 1;
}
static int GetRandomInt() {
return GetRandomSize();
}
static Matrix GetRandomMatrix(std::size_t a_Raw, std::size_t a_Column) {
Matrix matrix {a_Raw, a_Column};
std::generate(matrix.GetLineIterator(0), matrix.GetLineIterator(a_Raw), []() { return GetRandomInt(); });
return matrix;
}
void TestVect() {
Vect vect1 {{3, 2, {
@@ -22,26 +43,42 @@ void TestVect() {
0, 0,
1, 11,
}}};
assert(vect1 == vect3);
assert(vect2 == vect4);
assert(vect1 != vect2);
assert(vect2 != vect3);
assert(vect3 != vect4);
assert(vect1.IsElementOf(Matrix::ColumnVector({3, 7, 11})));
assert(!vect1.IsElementOf(Matrix::ColumnVector({3, 7, 12})));
test_assert(vect1 == vect3);
test_assert(vect2 == vect4);
test_assert(vect1 != vect2);
test_assert(vect2 != vect3);
test_assert(vect3 != vect4);
test_assert(vect1.IsElementOf(Matrix::ColumnVector({3, 7, 11})));
test_assert(!vect1.IsElementOf(Matrix::ColumnVector({3, 7, 12})));
}
void TestVectAffine() {
VectAffine aff {Matrix {3, 1, {-2, 3, 7}}, Matrix::ColumnVector({5, 2, -8})};
VectAffine aff {Matrix::ColumnVector({-2, 3, 7}), Matrix::ColumnVector({5, 2, -8})};
assert(aff.IsElementOf(Matrix::ColumnVector({5, 2, -8})));
assert(aff.IsElementOf(Matrix::ColumnVector({3, 5, -1})));
assert(!aff.IsElementOf(Matrix::ColumnVector({1, 2, 3})));
test_assert(aff.IsElementOf(Matrix::ColumnVector({5, 2, -8})));
test_assert(aff.IsElementOf(Matrix::ColumnVector({3, 5, -1})));
test_assert(!aff.IsElementOf(Matrix::ColumnVector({1, 2, 3})));
}
void TestLinearSystem() {
for (std::size_t i = 0; i < EXECUTION_COUNT; i++) {
Vect vect = GetRandomMatrix(GetRandomSize(), GetRandomSize());
Matrix systeme = vect.GetLinearSystem();
for (std::size_t j = 0; j < vect.GetCardinal(); j++) {
Matrix nullMatrix {systeme.GetColumnCount(), 1};
test_assert(systeme * vect.GetVector(j) == nullMatrix);
}
}
}
int main() {
srand(time(0));
TestVect();
TestVectAffine();
TestLinearSystem();
return 0;
}

View File

@@ -1,9 +1,19 @@
set_project("Pivot")
set_description("Solutionneur de matrice par le pivot de Gauss")
set_license("MIT")
set_xmakever("2.8.5")
add_rules("mode.debug", "mode.release")
set_languages("c++17")
add_requires("imgui", {configs = {sdl2_no_renderer = true, opengl3 = true}})
set_languages("c++20")
set_warnings("all")
add_includedirs("include")
-- Solver Library
target("Pivot")
set_kind("static")
@@ -19,9 +29,19 @@ target("PivotMain")
set_rundir("$(projectdir)/matricies")
add_files("src/main.cpp")
add_deps("Pivot")
set_default(false)
-- Gui interface
target("PivotGui")
set_rundir("$(projectdir)")
add_files("src/gui/*.cpp")
add_deps("Pivot")
set_default(true)
add_packages("libsdl", "imgui")