solver rework + refactor
This commit is contained in:
@@ -15,6 +15,6 @@ namespace Gauss {
|
||||
* \param reduite Mets des 0 au dessus des pivots
|
||||
* \param 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
|
||||
@@ -30,11 +30,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 remplit 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,11 +42,11 @@ 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() {}
|
||||
|
||||
@@ -67,51 +67,51 @@ class Matrix {
|
||||
|
||||
/**
|
||||
* \brief Augmente la matrice actuelle avec une autre
|
||||
* \param right Une matrice avec le bon nombre de lignes
|
||||
* \pre GetRawCount() = right.GetRawCount()
|
||||
* \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 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 +121,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,7 +131,7 @@ 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);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
||||
@@ -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(const 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(const 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(const 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(const Matrix& a_Matrix) const;
|
||||
};
|
||||
@@ -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(const 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,17 @@ 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();
|
||||
/**
|
||||
* \brief Exprime l'espace vectoriel comme les solutions d'un système linéaire des coordonnées des vecteurs
|
||||
* \return Une matrice représentant le système linéaire
|
||||
*/
|
||||
Matrix GetLinearSystem() const;
|
||||
|
||||
bool operator==(const VectAffine& a_VectAffine) const {
|
||||
return m_Origin == a_VectAffine.GetOrigin() && m_Base == a_VectAffine.GetBase();
|
||||
};
|
||||
};
|
||||
@@ -34,12 +34,12 @@ static void SimplifyLine(Matrix& mat, std::size_t line, std::size_t pivot_line,
|
||||
}
|
||||
}
|
||||
|
||||
void GaussJordan(Matrix& mat, bool reduite, bool normalise) {
|
||||
void GaussJordan(Matrix& a_Matrix, bool a_Reduite, 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,20 +47,20 @@ 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);
|
||||
}
|
||||
|
||||
// On simplifie les autres lignes
|
||||
for (std::size_t i = (reduite ? 0 : j); i < mat.GetRawCount(); i++) {
|
||||
for (std::size_t i = (a_Reduite ? 0 : j); i < a_Matrix.GetRawCount(); i++) {
|
||||
// Pour les lignes autre que la ligne pivot
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,29 +7,29 @@
|
||||
#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) {
|
||||
Matrix Matrix::operator*(const Matrix& a_Other) const {
|
||||
if (m_Columns != a_Other.m_Raws) {
|
||||
std::cerr << "Mutiplication impossible car la dimensions des matrices est incompatible" << std::endl;
|
||||
return {};
|
||||
}
|
||||
|
||||
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 +47,35 @@ 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::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 +84,49 @@ 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 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) += other.at(i, j);
|
||||
result.at(i, j) += a_Other.at(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Matrix Matrix::operator-(const Matrix& other) const {
|
||||
assert(GetColumnCount() == other.GetColumnCount() && GetRawCount() == other.GetRawCount());
|
||||
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) -= other.at(i, j);
|
||||
result.at(i, j) -= a_Other.at(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Matrix::operator==(const Matrix& other) const {
|
||||
if (m_Raws != other.m_Raws || m_Columns != other.m_Columns)
|
||||
bool Matrix::operator==(const Matrix& a_Other) const {
|
||||
if (m_Raws != a_Other.m_Raws || m_Columns != a_Other.m_Columns)
|
||||
return false;
|
||||
|
||||
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)))
|
||||
if (!IsEqualZero(at(i, j) - a_Other.at(i, j)))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -134,12 +134,12 @@ 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) {
|
||||
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 {
|
||||
return m_Data[a_Raw * m_Columns + a_Column];
|
||||
}
|
||||
|
||||
std::size_t Matrix::GetRawCount() const {
|
||||
@@ -150,14 +150,15 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,8 @@
|
||||
|
||||
#include "Gauss.h"
|
||||
|
||||
Solver::Solver(const Matrix& mat) : m_Matrix(mat) {}
|
||||
|
||||
Vect Solver::Image() const {
|
||||
Matrix result = m_Matrix;
|
||||
Vect Solver::Image(const Matrix& a_Matrix) const {
|
||||
Matrix result = a_Matrix;
|
||||
result.Transpose();
|
||||
Gauss::GaussJordan(result, true, true);
|
||||
result.Transpose();
|
||||
@@ -13,27 +11,28 @@ Vect Solver::Image() const {
|
||||
}
|
||||
|
||||
// https://en.wikipedia.org/wiki/Kernel_(linear_algebra)#Computation_by_Gaussian_elimination
|
||||
Vect Solver::Kernel() const {
|
||||
Matrix result = m_Matrix;
|
||||
Vect Solver::Kernel(const Matrix& a_Matrix) const {
|
||||
Matrix result = a_Matrix;
|
||||
result.Transpose();
|
||||
result.Augment(Matrix::Identity(result.GetRawCount()));
|
||||
Gauss::GaussJordan(result, true, true);
|
||||
result.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(result.SubMatrix(0, 0, a_Matrix.GetRawCount(), a_Matrix.GetColumnCount())).GetCardinal();
|
||||
|
||||
return {result.SubMatrix(m_Matrix.GetRawCount(), origine_colonne, result.GetRawCount() - m_Matrix.GetRawCount(),
|
||||
return {result.SubMatrix(a_Matrix.GetRawCount(), origine_colonne, result.GetRawCount() - a_Matrix.GetRawCount(),
|
||||
result.GetColumnCount() - origine_colonne)};
|
||||
}
|
||||
|
||||
VectAffine Solver::TriangularSystem() const {
|
||||
Matrix mat = m_Matrix;
|
||||
VectAffine Solver::RectangularSystem(const 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(a_MatrixA);
|
||||
Matrix origin = mat.SubMatrix(0, mat.GetColumnCount() - 1, mat.GetRawCount(), 1);
|
||||
|
||||
// on rajoute des 0 si il faut
|
||||
@@ -50,6 +49,6 @@ VectAffine Solver::TriangularSystem() const {
|
||||
return {noyau, fullOrigin};
|
||||
}
|
||||
|
||||
std::size_t Solver::Rank() const {
|
||||
return Image().GetCardinal();
|
||||
std::size_t Solver::Rank(const Matrix& a_Matrix) const {
|
||||
return Image(a_Matrix).GetCardinal();
|
||||
}
|
||||
|
||||
44
src/Vect.cpp
44
src/Vect.cpp
@@ -14,7 +14,7 @@ static bool IsColumnNull(Matrix& mat, std::size_t column) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Vect::Vect(const Matrix& mat) : m_Data(mat) {
|
||||
Vect::Vect(const Matrix& a_Matrix) : m_Data(a_Matrix) {
|
||||
Simplify();
|
||||
}
|
||||
|
||||
@@ -29,50 +29,50 @@ 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;
|
||||
Solver solver;
|
||||
vect = solver.Kernel(vect).m_Data;
|
||||
vect.Transpose();
|
||||
return vect;
|
||||
}
|
||||
@@ -81,9 +81,17 @@ 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);
|
||||
}
|
||||
|
||||
Matrix VectAffine::GetLinearSystem() const {
|
||||
Matrix result = m_Base.GetLinearSystem();
|
||||
|
||||
result.Augment(m_Origin.SubMatrix(0, 0, result.GetRawCount(), 1));
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -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(mat2);
|
||||
Vect noyau = solver.Kernel(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() {
|
||||
|
||||
@@ -8,7 +8,21 @@
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
int main() {
|
||||
void TestRectangular() {
|
||||
Matrix mat2 = {2, 4, {
|
||||
1, 1, 1, 1,
|
||||
1, -1, -1, 2
|
||||
}};
|
||||
|
||||
VectAffine aff {Matrix::ColumnVector({0, -1, 1}), Matrix::ColumnVector({3.0 / 2.0, 0, -1.0 / 2.0})};
|
||||
|
||||
Solver solver;
|
||||
|
||||
std::cout << solver.RectangularSystem(mat2, Matrix::ColumnVector({1, 2})).GetLinearSystem() << std::endl;
|
||||
std::cout << aff.GetLinearSystem() << std::endl;
|
||||
}
|
||||
|
||||
void TestKernelImage() {
|
||||
std::string path = "test";
|
||||
for (const auto& entry : fs::directory_iterator(path)) {
|
||||
std::string fileName = entry.path().string();
|
||||
@@ -23,10 +37,15 @@ int main() {
|
||||
Vect image {imageMat};
|
||||
Vect noyau {noyauMat};
|
||||
|
||||
Solver solver {mat};
|
||||
Solver solver;
|
||||
|
||||
assert(solver.Image() == image);
|
||||
assert(solver.Kernel() == noyau);
|
||||
assert(solver.Image(mat) == image);
|
||||
assert(solver.Kernel(mat) == noyau);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
TestKernelImage();
|
||||
TestRectangular();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user