Compare commits
4 Commits
efd0a88868
...
5a5c247019
| Author | SHA1 | Date | |
|---|---|---|---|
| 5a5c247019 | |||
| 99eca82b3a | |||
| 3b07ae783f | |||
| 82ad2e0696 |
@@ -7,6 +7,8 @@ ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
|||||||
PointerAlignment: Left
|
PointerAlignment: Left
|
||||||
SortIncludes: true
|
SortIncludes: true
|
||||||
SpacesBeforeTrailingComments: 2
|
SpacesBeforeTrailingComments: 2
|
||||||
|
SeparateDefinitionBlocks: Always
|
||||||
|
SpaceBeforeCpp11BracedList: true
|
||||||
UseTab: Always
|
UseTab: Always
|
||||||
MaxEmptyLinesToKeep: 5
|
MaxEmptyLinesToKeep: 5
|
||||||
|
|
||||||
|
|||||||
75
src/Gauss.cpp
Normal file
75
src/Gauss.cpp
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
#include "Gauss.h"
|
||||||
|
|
||||||
|
#include "Matrix.h"
|
||||||
|
|
||||||
|
namespace Gauss {
|
||||||
|
|
||||||
|
static void GaussNonJordan(Matrix& mat, bool reduite) {
|
||||||
|
int r = -1;
|
||||||
|
for (std::size_t j = 0; j < mat.GetColumnCount(); j++) {
|
||||||
|
std::size_t indice_ligne_maximum = r + 1;
|
||||||
|
|
||||||
|
// Recherche maximum
|
||||||
|
for (std::size_t i = r + 1; i < mat.GetRawCount(); i++) {
|
||||||
|
if (std::abs(mat.at(i, j)) > std::abs(mat.at(indice_ligne_maximum, j)))
|
||||||
|
indice_ligne_maximum = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si A[k,j]≠0 alors (A[k,j] désigne la valeur de la ligne k et de la colonne j)
|
||||||
|
if (!IsEqualZero(mat.at(indice_ligne_maximum, j))) {
|
||||||
|
r++;
|
||||||
|
|
||||||
|
// Si k≠r alors
|
||||||
|
if (indice_ligne_maximum != r) {
|
||||||
|
// Échanger les lignes k et r (On place la ligne du pivot en position r)
|
||||||
|
for (std::size_t k = 0; k < mat.GetColumnCount(); k++) {
|
||||||
|
std::swap(mat.at(indice_ligne_maximum, k), mat.at(r, k));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pour i de 1 jusqu'à n (On simplifie les autres lignes)
|
||||||
|
for (std::size_t i = (reduite ? 0 : j); i < mat.GetRawCount(); i++) {
|
||||||
|
// Si i≠r alors
|
||||||
|
if (i != r) {
|
||||||
|
// Soustraire à la ligne i la ligne r multipliée par A[i,j] (de façon à
|
||||||
|
// annuler A[i,j])
|
||||||
|
for (int k = mat.GetColumnCount() - 1; k >= 0; k--) {
|
||||||
|
long double pivot = mat.at(r, j);
|
||||||
|
long double anul = mat.at(i, j);
|
||||||
|
mat.at(i, k) = mat.at(i, k) * pivot - mat.at(r, k) * anul;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void GaussJordan(Matrix& mat, bool reduite) {
|
||||||
|
GaussNonJordan(mat, reduite);
|
||||||
|
for (std::size_t i = 0; i < mat.GetRawCount(); i++) {
|
||||||
|
int k = -1;
|
||||||
|
for (std::size_t j = 0; j < mat.GetColumnCount(); j++) {
|
||||||
|
if (!IsEqualZero(mat.at(i, j))) {
|
||||||
|
k = j;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ligne de 0
|
||||||
|
if (k == -1)
|
||||||
|
break;
|
||||||
|
// on divise la ligne par (i, k)
|
||||||
|
long double annul = mat.at(i, k);
|
||||||
|
for (int j = 0; j < mat.GetColumnCount(); j++) {
|
||||||
|
mat.at(i, j) /= annul;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GaussJordan(Matrix& mat, bool reduite, bool normalise) {
|
||||||
|
if (normalise)
|
||||||
|
GaussJordan(mat, reduite);
|
||||||
|
else
|
||||||
|
GaussNonJordan(mat, reduite);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Gauss
|
||||||
9
src/Gauss.h
Normal file
9
src/Gauss.h
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
class Matrix;
|
||||||
|
|
||||||
|
namespace Gauss {
|
||||||
|
|
||||||
|
void GaussJordan(Matrix& mat, bool reduite, bool normalise);
|
||||||
|
|
||||||
|
} // namespace Gauss
|
||||||
189
src/Matrix.cpp
189
src/Matrix.cpp
@@ -10,28 +10,30 @@ Matrix::Matrix(const std::string& fileNameInput) {
|
|||||||
Load(fileNameInput);
|
Load(fileNameInput);
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Lignes(lignes), m_Colonnes(colonnes) {
|
Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Raws(lignes), m_Columns(colonnes) {
|
||||||
m_Data.resize(m_Lignes * m_Colonnes);
|
m_Data.resize(m_Raws * m_Columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix::Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list<long double>&& initList) :
|
Matrix::Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list<long double>&& initList) :
|
||||||
m_Lignes(lignes), m_Colonnes(colonnes) {
|
m_Raws(lignes), m_Columns(colonnes) {
|
||||||
m_Data = initList;
|
m_Data = initList;
|
||||||
m_Data.resize(m_Lignes * m_Colonnes);
|
m_Data.resize(m_Raws * m_Columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix::~Matrix() {}
|
Matrix::~Matrix() {}
|
||||||
|
|
||||||
Matrix Matrix::operator*(const Matrix& other) const {
|
Matrix Matrix::operator*(const Matrix& other) const {
|
||||||
if (m_Colonnes != other.m_Lignes) {
|
if (m_Columns != other.m_Raws) {
|
||||||
std::cerr << "Mutiplication impossible car la dimensions des matrices est incompatible" << std::endl;
|
std::cerr << "Mutiplication impossible car la dimensions des matrices est incompatible" << std::endl;
|
||||||
|
return {1, 1, {0}};
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix result(m_Lignes, other.m_Colonnes);
|
Matrix result(m_Raws, other.m_Columns);
|
||||||
|
|
||||||
for (std::size_t i = 0; i < m_Lignes; ++i) {
|
for (std::size_t i = 0; i < m_Raws; ++i) {
|
||||||
for (std::size_t j = 0; j < other.m_Colonnes; ++j) {
|
for (std::size_t j = 0; j < other.m_Columns; ++j) {
|
||||||
long double sum = 0;
|
long double sum = 0;
|
||||||
for (std::size_t k = 0; k < m_Colonnes; k++) {
|
for (std::size_t k = 0; k < m_Columns; k++) {
|
||||||
sum += at(i, k) * other.at(k, j);
|
sum += at(i, k) * other.at(k, j);
|
||||||
}
|
}
|
||||||
result.at(i, j) = sum;
|
result.at(i, j) = sum;
|
||||||
@@ -41,10 +43,10 @@ Matrix Matrix::operator*(const Matrix& other) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Matrix::Print() const {
|
void Matrix::Print() const {
|
||||||
for (size_t i = 0; i < m_Lignes; ++i) {
|
for (size_t i = 0; i < m_Raws; ++i) {
|
||||||
std::cout << "[ ";
|
std::cout << "[ ";
|
||||||
for (size_t j = 0; j < m_Colonnes; ++j) {
|
for (size_t j = 0; j < m_Columns; ++j) {
|
||||||
std::size_t indice = i * m_Lignes + j;
|
std::size_t indice = i * m_Raws + j;
|
||||||
std::cout << at(i, j) << " ";
|
std::cout << at(i, j) << " ";
|
||||||
}
|
}
|
||||||
std::cout << "]";
|
std::cout << "]";
|
||||||
@@ -52,16 +54,9 @@ void Matrix::Print() const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Matrix::PrintDebug() {
|
|
||||||
#ifndef NDEBUG
|
|
||||||
Print();
|
|
||||||
std::cout << "\n";
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void Matrix::Insert() {
|
void Matrix::Insert() {
|
||||||
for (size_t i = 0; i < m_Lignes; ++i) {
|
for (size_t i = 0; i < m_Raws; ++i) {
|
||||||
for (size_t j = 0; j < m_Colonnes; ++j) {
|
for (size_t j = 0; j < m_Columns; ++j) {
|
||||||
std::cin >> at(i, j);
|
std::cin >> at(i, j);
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
@@ -69,7 +64,7 @@ void Matrix::Insert() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Matrix::Save(const std::string& fileName) {
|
void Matrix::Save(const std::string& fileName) {
|
||||||
std::ofstream out{fileName};
|
std::ofstream out {fileName};
|
||||||
if (!out) {
|
if (!out) {
|
||||||
std::cerr << "Impossible de sauvegarder la matrice !\n";
|
std::cerr << "Impossible de sauvegarder la matrice !\n";
|
||||||
return;
|
return;
|
||||||
@@ -78,7 +73,7 @@ void Matrix::Save(const std::string& fileName) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Matrix::Load(const std::string& filename) {
|
void Matrix::Load(const std::string& filename) {
|
||||||
std::ifstream in{filename};
|
std::ifstream in {filename};
|
||||||
if (!in) {
|
if (!in) {
|
||||||
std::cerr << "Impossible de charger la matrice !\n";
|
std::cerr << "Impossible de charger la matrice !\n";
|
||||||
return;
|
return;
|
||||||
@@ -87,56 +82,38 @@ void Matrix::Load(const std::string& filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Matrix::Transpose() {
|
void Matrix::Transpose() {
|
||||||
Matrix result{m_Colonnes, m_Lignes};
|
Matrix result {m_Columns, m_Raws};
|
||||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
for (std::size_t i = 0; i < m_Raws; i++) {
|
||||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
for (std::size_t j = 0; j < m_Columns; j++) {
|
||||||
result.at(j, i) = at(i, j);
|
result.at(j, i) = at(i, j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*this = result;
|
*this = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Matrix::Identity() {
|
Matrix Matrix::Identity(std::size_t taille) {
|
||||||
assert(m_Lignes == m_Colonnes);
|
Matrix id {taille, taille};
|
||||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
for (std::size_t i = 0; i < taille; i++) {
|
||||||
for (std::size_t j = i; j < m_Colonnes; j++) {
|
for (std::size_t j = i; j < taille; j++) {
|
||||||
at(i, j) = i == j;
|
id.at(i, j) = (i == j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Matrix Matrix::Identity(std::size_t taille) {
|
|
||||||
Matrix id{taille, taille};
|
|
||||||
id.Identity();
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Matrix::IsInversed() const {
|
void Matrix::Augment(const Matrix& droite) {
|
||||||
for (std::size_t i = 0; i < m_Lignes; ++i) {
|
assert(droite.m_Raws == m_Raws);
|
||||||
std::size_t j;
|
Matrix temp {m_Raws, m_Columns + droite.m_Columns};
|
||||||
for (j = 0; j < m_Colonnes; ++j) {
|
|
||||||
if (!IsEqualZero(at(i, j))) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Matrix::Augmenter(const Matrix& droite) {
|
for (std::size_t i = 0; i < m_Raws; i++) {
|
||||||
assert(droite.m_Lignes == m_Lignes);
|
for (std::size_t j = 0; j < m_Columns; j++) {
|
||||||
Matrix temp{m_Lignes, m_Colonnes + droite.m_Colonnes};
|
|
||||||
|
|
||||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
|
||||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
|
||||||
temp.at(i, j) = at(i, j);
|
temp.at(i, j) = at(i, j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
for (std::size_t i = 0; i < m_Raws; i++) {
|
||||||
for (std::size_t j = 0; j < droite.m_Colonnes; j++) {
|
for (std::size_t j = 0; j < droite.m_Columns; j++) {
|
||||||
temp.at(i, j + m_Colonnes) = droite.at(i, j);
|
temp.at(i, j + m_Columns) = droite.at(i, j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,11 +121,11 @@ void Matrix::Augmenter(const Matrix& droite) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Matrix::operator==(const Matrix& other) const {
|
bool Matrix::operator==(const Matrix& other) const {
|
||||||
if (m_Lignes != other.m_Lignes || m_Colonnes != other.m_Colonnes)
|
if (m_Raws != other.m_Raws || m_Columns != other.m_Columns)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
for (std::size_t i = 0; i < m_Raws; i++) {
|
||||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
for (std::size_t j = 0; j < m_Columns; j++) {
|
||||||
if (!IsEqualZero(at(i, j) - other.at(i, j)))
|
if (!IsEqualZero(at(i, j) - other.at(i, j)))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -157,95 +134,29 @@ bool Matrix::operator==(const Matrix& other) const {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Matrix::GaussNonJordan(bool reduite) {
|
|
||||||
int r = -1;
|
|
||||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
|
||||||
std::size_t indice_ligne_maximum = r + 1;
|
|
||||||
|
|
||||||
// Recherche maximum
|
|
||||||
for (std::size_t i = r + 1; i < m_Lignes; i++) {
|
|
||||||
if (std::abs(at(i, j)) > std::abs(at(indice_ligne_maximum, j)))
|
|
||||||
indice_ligne_maximum = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// std::cout << "l'indice du maximum est : " << indice_ligne_maximum << "\n\n";
|
|
||||||
|
|
||||||
// Si A[k,j]≠0 alors (A[k,j] désigne la valeur de la ligne k et de la colonne j)
|
|
||||||
if (!IsEqualZero(at(indice_ligne_maximum, j))) {
|
|
||||||
r++;
|
|
||||||
|
|
||||||
// PrintDebug();
|
|
||||||
|
|
||||||
// Si k≠r alors
|
|
||||||
if (indice_ligne_maximum != r) {
|
|
||||||
// Échanger les lignes k et r (On place la ligne du pivot en position r)
|
|
||||||
// std::cout << "On échange les lignes " << indice_ligne_maximum << " et " << r << '\n';
|
|
||||||
for (std::size_t k = 0; k < m_Colonnes; k++) {
|
|
||||||
std::swap(at(indice_ligne_maximum, k), at(r, k));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pour i de 1 jusqu'à n (On simplifie les autres lignes)
|
|
||||||
for (std::size_t i = (reduite ? 0 : j); i < m_Lignes; i++) {
|
|
||||||
// Si i≠r alors
|
|
||||||
if (i != r) {
|
|
||||||
// Soustraire à la ligne i la ligne r multipliée par A[i,j] (de façon à
|
|
||||||
// annuler A[i,j])
|
|
||||||
for (int k = m_Colonnes - 1; k >= 0; k--) {
|
|
||||||
long double pivot = at(r, j);
|
|
||||||
long double anul = at(i, j);
|
|
||||||
at(i, k) = at(i, k) * pivot - at(r, k) * anul;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Matrix::GaussJordan(bool reduite) {
|
|
||||||
GaussNonJordan(reduite);
|
|
||||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
|
||||||
int k = -1;
|
|
||||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
|
||||||
if (!IsEqualZero(at(i, j))) {
|
|
||||||
k = j;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// ligne de 0
|
|
||||||
if (k == -1)
|
|
||||||
break;
|
|
||||||
// on divise la ligne par (i, k)
|
|
||||||
long double annul = at(i, k);
|
|
||||||
for (int j = 0; j < m_Colonnes; j++) {
|
|
||||||
at(i, j) /= annul;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
long double& Matrix::operator[](std::size_t indice) {
|
long double& Matrix::operator[](std::size_t indice) {
|
||||||
return m_Data[indice];
|
return m_Data[indice];
|
||||||
}
|
}
|
||||||
|
|
||||||
long double& Matrix::at(std::size_t ligne, std::size_t colonne) {
|
long double& Matrix::at(std::size_t ligne, std::size_t colonne) {
|
||||||
return m_Data[ligne * m_Colonnes + colonne];
|
return m_Data[ligne * m_Columns + colonne];
|
||||||
}
|
}
|
||||||
|
|
||||||
long double Matrix::at(std::size_t ligne, std::size_t colonne) const {
|
long double Matrix::at(std::size_t ligne, std::size_t colonne) const {
|
||||||
return m_Data[ligne * m_Colonnes + colonne];
|
return m_Data[ligne * m_Columns + colonne];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t Matrix::GetRawCount() const {
|
std::size_t Matrix::GetRawCount() const {
|
||||||
return m_Lignes;
|
return m_Raws;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t Matrix::GetColumnCount() const {
|
std::size_t Matrix::GetColumnCount() const {
|
||||||
return m_Colonnes;
|
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 {
|
Matrix Matrix::SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne, std::size_t ligne, std::size_t colonne) const {
|
||||||
assert(m_Lignes >= ligne && m_Colonnes >= colonne);
|
assert(m_Raws >= ligne && m_Columns >= colonne);
|
||||||
Matrix result{ligne, colonne};
|
Matrix result {ligne, colonne};
|
||||||
|
|
||||||
for (std::size_t i = 0; i < ligne; i++) {
|
for (std::size_t i = 0; i < ligne; i++) {
|
||||||
for (std::size_t j = 0; j < colonne; j++) {
|
for (std::size_t j = 0; j < colonne; j++) {
|
||||||
@@ -257,9 +168,9 @@ Matrix Matrix::SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne,
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& stream, const Matrix& mat) {
|
std::ostream& operator<<(std::ostream& stream, const Matrix& mat) {
|
||||||
stream << mat.m_Lignes << " " << mat.m_Colonnes << "\n";
|
stream << mat.m_Raws << " " << mat.m_Columns << "\n";
|
||||||
for (std::size_t i = 0; i < mat.m_Lignes; i++) {
|
for (std::size_t i = 0; i < mat.m_Raws; i++) {
|
||||||
for (std::size_t j = 0; j < mat.m_Colonnes; j++) {
|
for (std::size_t j = 0; j < mat.m_Columns; j++) {
|
||||||
stream << mat.at(i, j) << " ";
|
stream << mat.at(i, j) << " ";
|
||||||
}
|
}
|
||||||
stream << "\n";
|
stream << "\n";
|
||||||
@@ -268,10 +179,10 @@ std::ostream& operator<<(std::ostream& stream, const Matrix& mat) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::istream& operator>>(std::istream& stream, Matrix& mat) {
|
std::istream& operator>>(std::istream& stream, Matrix& mat) {
|
||||||
stream >> mat.m_Lignes >> mat.m_Colonnes;
|
stream >> mat.m_Raws >> mat.m_Columns;
|
||||||
mat.m_Data.resize(mat.m_Lignes * mat.m_Colonnes);
|
mat.m_Data.resize(mat.m_Raws * mat.m_Columns);
|
||||||
for (std::size_t i = 0; i < mat.m_Lignes; i++) {
|
for (std::size_t i = 0; i < mat.m_Raws; i++) {
|
||||||
for (std::size_t j = 0; j < mat.m_Colonnes; j++) {
|
for (std::size_t j = 0; j < mat.m_Columns; j++) {
|
||||||
stream >> mat.at(i, j);
|
stream >> mat.at(i, j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
39
src/Matrix.h
39
src/Matrix.h
@@ -7,54 +7,39 @@
|
|||||||
|
|
||||||
class Matrix {
|
class Matrix {
|
||||||
private:
|
private:
|
||||||
std::size_t m_Lignes;
|
std::size_t m_Raws;
|
||||||
std::size_t m_Colonnes;
|
std::size_t m_Columns;
|
||||||
std::vector<long double> m_Data;
|
std::vector<long double> m_Data;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Matrix(const std::string& fileNameInput);
|
Matrix(const std::string& fileNameInput);
|
||||||
Matrix(std::size_t lignes, std::size_t colonnes);
|
Matrix(std::size_t raws, std::size_t columns);
|
||||||
Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list<long double>&& initList);
|
Matrix(std::size_t raws, std::size_t columns, std::initializer_list<long double>&& initList);
|
||||||
~Matrix();
|
~Matrix();
|
||||||
|
|
||||||
std::size_t GetRawCount() const;
|
std::size_t GetRawCount() const;
|
||||||
std::size_t GetColumnCount() const;
|
std::size_t GetColumnCount() const;
|
||||||
|
|
||||||
Matrix operator*(const Matrix& other) const;
|
void Insert();
|
||||||
|
|
||||||
void GaussNonJordan(bool reduite);
|
|
||||||
|
|
||||||
void GaussJordan(bool reduite);
|
|
||||||
|
|
||||||
void Print() const;
|
void Print() const;
|
||||||
|
|
||||||
void PrintDebug();
|
|
||||||
|
|
||||||
void Insert();
|
|
||||||
|
|
||||||
void Save(const std::string& fileName);
|
void Save(const std::string& fileName);
|
||||||
|
|
||||||
void Load(const std::string& filename);
|
void Load(const std::string& filename);
|
||||||
|
|
||||||
void Transpose();
|
void Transpose();
|
||||||
|
|
||||||
void Identity();
|
static Matrix Identity(std::size_t size);
|
||||||
|
|
||||||
static Matrix Identity(std::size_t taille);
|
void Augment(const Matrix& right);
|
||||||
|
|
||||||
bool IsInversed() const;
|
Matrix SubMatrix(std::size_t raw_origin, std::size_t column_origin, std::size_t raw, std::size_t column) const;
|
||||||
|
|
||||||
void Augmenter(const Matrix& droite);
|
|
||||||
|
|
||||||
Matrix SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne, std::size_t ligne, std::size_t colonne) const;
|
|
||||||
|
|
||||||
bool operator==(const Matrix& other) const;
|
bool operator==(const Matrix& other) const;
|
||||||
|
Matrix operator*(const Matrix& other) const;
|
||||||
|
long double& operator[](std::size_t index);
|
||||||
|
|
||||||
long double& operator[](std::size_t indice);
|
long double& at(std::size_t raw, std::size_t column);
|
||||||
|
long double at(std::size_t raw, std::size_t column) const;
|
||||||
long double& at(std::size_t ligne, std::size_t colonne);
|
|
||||||
|
|
||||||
long double at(std::size_t ligne, std::size_t colonne) const;
|
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& stream, const Matrix& mat);
|
friend std::ostream& operator<<(std::ostream& stream, const Matrix& mat);
|
||||||
friend std::istream& operator>>(std::istream& stream, Matrix& mat);
|
friend std::istream& operator>>(std::istream& stream, Matrix& mat);
|
||||||
|
|||||||
2
src/NR.h
2
src/NR.h
@@ -7,6 +7,8 @@ class NR {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
NR() : m_Numerator(0), m_Denominator(1) {}
|
NR() : m_Numerator(0), m_Denominator(1) {}
|
||||||
|
|
||||||
NR(int entier) : m_Numerator(entier), m_Denominator(1) {}
|
NR(int entier) : m_Numerator(entier), m_Denominator(1) {}
|
||||||
|
|
||||||
NR(int numerator, int denominator) : m_Numerator(numerator), m_Denominator(denominator) {}
|
NR(int numerator, int denominator) : m_Numerator(numerator), m_Denominator(denominator) {}
|
||||||
};
|
};
|
||||||
@@ -1,21 +1,23 @@
|
|||||||
#include "Solver.h"
|
#include "Solver.h"
|
||||||
|
|
||||||
|
#include "Gauss.h"
|
||||||
|
|
||||||
Solver::Solver(const Matrix& mat) : m_Matrix(mat) {}
|
Solver::Solver(const Matrix& mat) : m_Matrix(mat) {}
|
||||||
|
|
||||||
Vect Solver::Image() const {
|
Vect Solver::Image() const {
|
||||||
Matrix result = m_Matrix;
|
Matrix result = m_Matrix;
|
||||||
result.Transpose();
|
result.Transpose();
|
||||||
result.GaussJordan(true);
|
Gauss::GaussJordan(result, true, true);
|
||||||
result.Transpose();
|
result.Transpose();
|
||||||
return {result};
|
return {result};
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://en.wikipedia.org/wiki/Kernel_(linear_algebra)#Computation_by_Gaussian_elimination
|
// https://en.wikipedia.org/wiki/Kernel_(linear_algebra)#Computation_by_Gaussian_elimination
|
||||||
Vect Solver::Noyau() const {
|
Vect Solver::Kernel() const {
|
||||||
Matrix result = m_Matrix;
|
Matrix result = m_Matrix;
|
||||||
result.Transpose();
|
result.Transpose();
|
||||||
result.Augmenter(Matrix::Identity(result.GetRawCount()));
|
result.Augment(Matrix::Identity(result.GetRawCount()));
|
||||||
result.GaussJordan(true);
|
Gauss::GaussJordan(result, true, true);
|
||||||
result.Transpose();
|
result.Transpose();
|
||||||
|
|
||||||
// nombre de colonnes non nulles
|
// nombre de colonnes non nulles
|
||||||
@@ -25,19 +27,19 @@ Vect Solver::Noyau() const {
|
|||||||
result.GetColumnCount() - origine_colonne)};
|
result.GetColumnCount() - origine_colonne)};
|
||||||
}
|
}
|
||||||
|
|
||||||
VectAffine Solver::SystemeTriangulaire() const {
|
VectAffine Solver::TriangularSystem() const {
|
||||||
Matrix mat = m_Matrix;
|
Matrix mat = m_Matrix;
|
||||||
mat.GaussJordan(true);
|
Gauss::GaussJordan(mat, true, true);
|
||||||
|
|
||||||
Solver solver{mat.SubMatrix(0, 0, mat.GetRawCount(), mat.GetColumnCount() - 1)};
|
Solver solver {mat.SubMatrix(0, 0, mat.GetRawCount(), mat.GetColumnCount() - 1)};
|
||||||
|
|
||||||
Vect noyau = solver.Noyau();
|
Vect noyau = solver.Kernel();
|
||||||
Matrix origin = mat.SubMatrix(0, mat.GetColumnCount() - 1, mat.GetRawCount(), 1);
|
Matrix origin = mat.SubMatrix(0, mat.GetColumnCount() - 1, mat.GetRawCount(), 1);
|
||||||
|
|
||||||
return {noyau, origin};
|
return {noyau, origin};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t Solver::Rang() const {
|
std::size_t Solver::Rank() const {
|
||||||
Vect image = Image();
|
Vect image = Image();
|
||||||
return image.GetCardinal();
|
return image.GetCardinal();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,12 +8,13 @@ class Solver {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Solver(const Matrix& mat);
|
Solver(const Matrix& mat);
|
||||||
|
|
||||||
~Solver() {}
|
~Solver() {}
|
||||||
|
|
||||||
Vect Image() const;
|
Vect Image() const;
|
||||||
Vect Noyau() const;
|
Vect Kernel() const;
|
||||||
|
|
||||||
VectAffine SystemeTriangulaire() const;
|
VectAffine TriangularSystem() const;
|
||||||
|
|
||||||
std::size_t Rang() const;
|
std::size_t Rank() const;
|
||||||
};
|
};
|
||||||
11
src/Vect.cpp
11
src/Vect.cpp
@@ -1,5 +1,6 @@
|
|||||||
#include "Vect.h"
|
#include "Vect.h"
|
||||||
|
|
||||||
|
#include "Gauss.h"
|
||||||
#include "Solver.h"
|
#include "Solver.h"
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -43,9 +44,9 @@ bool Vect::operator==(const Vect& other) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Vect::AddVector(const Matrix& mat) {
|
void Vect::AddVector(const Matrix& mat) {
|
||||||
m_Data.Augmenter(mat);
|
m_Data.Augment(mat);
|
||||||
m_Data.Transpose();
|
m_Data.Transpose();
|
||||||
m_Data.GaussNonJordan(false);
|
Gauss::GaussJordan(m_Data, false, false);
|
||||||
m_Data.Transpose();
|
m_Data.Transpose();
|
||||||
Simplify();
|
Simplify();
|
||||||
}
|
}
|
||||||
@@ -58,8 +59,8 @@ Matrix Vect::GetLinearSystem() const {
|
|||||||
Matrix vect = m_Data;
|
Matrix vect = m_Data;
|
||||||
vect.Transpose();
|
vect.Transpose();
|
||||||
|
|
||||||
Solver solver{vect};
|
Solver solver {vect};
|
||||||
vect = solver.Noyau().m_Data;
|
vect = solver.Kernel().m_Data;
|
||||||
vect.Transpose();
|
vect.Transpose();
|
||||||
return vect;
|
return vect;
|
||||||
}
|
}
|
||||||
@@ -68,7 +69,7 @@ void Vect::Print() const {
|
|||||||
std::cout << "Espace vectoriel de dimension " << GetCardinal() << " de base :\n\n";
|
std::cout << "Espace vectoriel de dimension " << GetCardinal() << " de base :\n\n";
|
||||||
for (std::size_t i = 0; i < m_Data.GetRawCount(); i++) {
|
for (std::size_t i = 0; i < m_Data.GetRawCount(); i++) {
|
||||||
for (std::size_t j = 0; j < m_Data.GetColumnCount(); j++) {
|
for (std::size_t j = 0; j < m_Data.GetColumnCount(); j++) {
|
||||||
printf("[ %u ]\t", static_cast<float>(m_Data.at(i, j)));
|
std::cout << "[ " << m_Data.at(i, j) << " ]\t";
|
||||||
}
|
}
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ class Vect {
|
|||||||
void Simplify();
|
void Simplify();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class VectAffine {
|
class VectAffine {
|
||||||
private:
|
private:
|
||||||
Vect m_Base;
|
Vect m_Base;
|
||||||
|
|||||||
11
src/main.cpp
11
src/main.cpp
@@ -1,3 +1,4 @@
|
|||||||
|
#include "Gauss.h"
|
||||||
#include "Solver.h"
|
#include "Solver.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@@ -16,13 +17,13 @@ void test() {
|
|||||||
mat.Print();
|
mat.Print();
|
||||||
// mat.Save("matrice4x4echelonne.mat"); */
|
// mat.Save("matrice4x4echelonne.mat"); */
|
||||||
|
|
||||||
Matrix mat2{"matrice4x4.mat"};
|
Matrix mat2 {"matrice4x4.mat"};
|
||||||
mat2.Print();
|
mat2.Print();
|
||||||
|
|
||||||
Solver solver{mat2};
|
Solver solver {mat2};
|
||||||
|
|
||||||
Vect image = solver.Image();
|
Vect image = solver.Image();
|
||||||
Vect noyau = solver.Noyau();
|
Vect noyau = solver.Kernel();
|
||||||
|
|
||||||
std::cout << "\tImage :\n";
|
std::cout << "\tImage :\n";
|
||||||
image.Print();
|
image.Print();
|
||||||
@@ -34,7 +35,7 @@ void test() {
|
|||||||
noyau.GetLinearSystem().Print();
|
noyau.GetLinearSystem().Print();
|
||||||
|
|
||||||
std::cout << "\n\n";
|
std::cout << "\n\n";
|
||||||
solver.SystemeTriangulaire().Print();
|
solver.TriangularSystem().Print();
|
||||||
}
|
}
|
||||||
|
|
||||||
void prompt() {
|
void prompt() {
|
||||||
@@ -52,7 +53,7 @@ void prompt() {
|
|||||||
|
|
||||||
mat.Print();
|
mat.Print();
|
||||||
|
|
||||||
mat.GaussJordan(true);
|
Gauss::GaussJordan(mat, true, true);
|
||||||
|
|
||||||
mat.Print();
|
mat.Print();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include "Gauss.h"
|
||||||
#include "Matrix.h"
|
#include "Matrix.h"
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
@@ -5,9 +6,9 @@
|
|||||||
#error "Il faut être en debug mode ! xmake f -m debug"
|
#error "Il faut être en debug mode ! xmake f -m debug"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct Test{
|
struct Test {
|
||||||
Matrix mat;
|
Matrix mat;
|
||||||
Matrix res;
|
Matrix res;
|
||||||
};
|
};
|
||||||
|
|
||||||
static const std::vector<Test> TEST_MATRICES = {
|
static const std::vector<Test> TEST_MATRICES = {
|
||||||
@@ -35,9 +36,9 @@ static const std::vector<Test> TEST_MATRICES = {
|
|||||||
|
|
||||||
void test() {
|
void test() {
|
||||||
for (Test test : TEST_MATRICES) {
|
for (Test test : TEST_MATRICES) {
|
||||||
test.mat.GaussJordan(true);
|
Gauss::GaussJordan(test.mat, true, true);
|
||||||
assert(test.mat == test.res);
|
assert(test.mat == test.res);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
|||||||
@@ -14,18 +14,18 @@ int main() {
|
|||||||
|
|
||||||
std::cout << "Opening " << fileName << " ...\n";
|
std::cout << "Opening " << fileName << " ...\n";
|
||||||
|
|
||||||
std::ifstream in{fileName};
|
std::ifstream in {fileName};
|
||||||
|
|
||||||
Matrix mat{1, 1}, imageMat{1, 1}, noyauMat{1, 1};
|
Matrix mat {1, 1}, imageMat {1, 1}, noyauMat {1, 1};
|
||||||
in >> mat >> imageMat >> noyauMat;
|
in >> mat >> imageMat >> noyauMat;
|
||||||
|
|
||||||
Vect image{imageMat};
|
Vect image {imageMat};
|
||||||
Vect noyau{noyauMat};
|
Vect noyau {noyauMat};
|
||||||
|
|
||||||
Solver solver{mat};
|
Solver solver {mat};
|
||||||
|
|
||||||
assert(solver.Image() == image);
|
assert(solver.Image() == image);
|
||||||
assert(solver.Noyau() == noyau);
|
assert(solver.Kernel() == noyau);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user