This commit is contained in:
@@ -10,30 +10,30 @@ Matrix::Matrix(const std::string& fileNameInput) {
|
||||
Load(fileNameInput);
|
||||
}
|
||||
|
||||
Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Lignes(lignes), m_Colonnes(colonnes) {
|
||||
m_Data.resize(m_Lignes * m_Colonnes);
|
||||
Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Raws(lignes), m_Columns(colonnes) {
|
||||
m_Data.resize(m_Raws * m_Columns);
|
||||
}
|
||||
|
||||
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.resize(m_Lignes * m_Colonnes);
|
||||
m_Data.resize(m_Raws * m_Columns);
|
||||
}
|
||||
|
||||
Matrix::~Matrix() {}
|
||||
|
||||
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;
|
||||
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 j = 0; j < other.m_Colonnes; ++j) {
|
||||
for (std::size_t i = 0; i < m_Raws; ++i) {
|
||||
for (std::size_t j = 0; j < other.m_Columns; ++j) {
|
||||
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);
|
||||
}
|
||||
result.at(i, j) = sum;
|
||||
@@ -43,10 +43,10 @@ Matrix Matrix::operator*(const Matrix& other) 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 << "[ ";
|
||||
for (size_t j = 0; j < m_Colonnes; ++j) {
|
||||
std::size_t indice = i * m_Lignes + j;
|
||||
for (size_t j = 0; j < m_Columns; ++j) {
|
||||
std::size_t indice = i * m_Raws + j;
|
||||
std::cout << at(i, j) << " ";
|
||||
}
|
||||
std::cout << "]";
|
||||
@@ -55,8 +55,8 @@ void Matrix::Print() const {
|
||||
}
|
||||
|
||||
void Matrix::Insert() {
|
||||
for (size_t i = 0; i < m_Lignes; ++i) {
|
||||
for (size_t j = 0; j < m_Colonnes; ++j) {
|
||||
for (size_t i = 0; i < m_Raws; ++i) {
|
||||
for (size_t j = 0; j < m_Columns; ++j) {
|
||||
std::cin >> at(i, j);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
@@ -82,9 +82,9 @@ void Matrix::Load(const std::string& filename) {
|
||||
}
|
||||
|
||||
void Matrix::Transpose() {
|
||||
Matrix result {m_Colonnes, m_Lignes};
|
||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
||||
Matrix result {m_Columns, m_Raws};
|
||||
for (std::size_t i = 0; i < m_Raws; i++) {
|
||||
for (std::size_t j = 0; j < m_Columns; j++) {
|
||||
result.at(j, i) = at(i, j);
|
||||
}
|
||||
}
|
||||
@@ -101,19 +101,19 @@ Matrix Matrix::Identity(std::size_t taille) {
|
||||
return id;
|
||||
}
|
||||
|
||||
void Matrix::Augmenter(const Matrix& droite) {
|
||||
assert(droite.m_Lignes == m_Lignes);
|
||||
Matrix temp {m_Lignes, m_Colonnes + droite.m_Colonnes};
|
||||
void Matrix::Augment(const Matrix& droite) {
|
||||
assert(droite.m_Raws == m_Raws);
|
||||
Matrix temp {m_Raws, m_Columns + droite.m_Columns};
|
||||
|
||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
||||
for (std::size_t i = 0; i < m_Raws; i++) {
|
||||
for (std::size_t j = 0; j < m_Columns; j++) {
|
||||
temp.at(i, j) = at(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
||||
for (std::size_t j = 0; j < droite.m_Colonnes; j++) {
|
||||
temp.at(i, j + m_Colonnes) = droite.at(i, j);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,11 +121,11 @@ void Matrix::Augmenter(const Matrix& droite) {
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
||||
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)))
|
||||
return false;
|
||||
}
|
||||
@@ -139,23 +139,23 @@ long double& Matrix::operator[](std::size_t indice) {
|
||||
}
|
||||
|
||||
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 {
|
||||
return m_Data[ligne * m_Colonnes + colonne];
|
||||
return m_Data[ligne * m_Columns + colonne];
|
||||
}
|
||||
|
||||
std::size_t Matrix::GetRawCount() const {
|
||||
return m_Lignes;
|
||||
return m_Raws;
|
||||
}
|
||||
|
||||
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 {
|
||||
assert(m_Lignes >= ligne && m_Colonnes >= colonne);
|
||||
assert(m_Raws >= ligne && m_Columns >= colonne);
|
||||
Matrix result {ligne, colonne};
|
||||
|
||||
for (std::size_t i = 0; i < ligne; i++) {
|
||||
@@ -168,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) {
|
||||
stream << mat.m_Lignes << " " << mat.m_Colonnes << "\n";
|
||||
for (std::size_t i = 0; i < mat.m_Lignes; i++) {
|
||||
for (std::size_t j = 0; j < mat.m_Colonnes; j++) {
|
||||
stream << mat.m_Raws << " " << mat.m_Columns << "\n";
|
||||
for (std::size_t i = 0; i < mat.m_Raws; i++) {
|
||||
for (std::size_t j = 0; j < mat.m_Columns; j++) {
|
||||
stream << mat.at(i, j) << " ";
|
||||
}
|
||||
stream << "\n";
|
||||
@@ -179,10 +179,10 @@ std::ostream& operator<<(std::ostream& stream, const Matrix& mat) {
|
||||
}
|
||||
|
||||
std::istream& operator>>(std::istream& stream, Matrix& mat) {
|
||||
stream >> mat.m_Lignes >> mat.m_Colonnes;
|
||||
mat.m_Data.resize(mat.m_Lignes * mat.m_Colonnes);
|
||||
for (std::size_t i = 0; i < mat.m_Lignes; i++) {
|
||||
for (std::size_t j = 0; j < mat.m_Colonnes; j++) {
|
||||
stream >> mat.m_Raws >> mat.m_Columns;
|
||||
mat.m_Data.resize(mat.m_Raws * mat.m_Columns);
|
||||
for (std::size_t i = 0; i < mat.m_Raws; i++) {
|
||||
for (std::size_t j = 0; j < mat.m_Columns; j++) {
|
||||
stream >> mat.at(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
20
src/Matrix.h
20
src/Matrix.h
@@ -7,14 +7,14 @@
|
||||
|
||||
class Matrix {
|
||||
private:
|
||||
std::size_t m_Lignes;
|
||||
std::size_t m_Colonnes;
|
||||
std::size_t m_Raws;
|
||||
std::size_t m_Columns;
|
||||
std::vector<long double> m_Data;
|
||||
|
||||
public:
|
||||
Matrix(const std::string& fileNameInput);
|
||||
Matrix(std::size_t lignes, std::size_t colonnes);
|
||||
Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list<long double>&& initList);
|
||||
Matrix(std::size_t raws, std::size_t columns);
|
||||
Matrix(std::size_t raws, std::size_t columns, std::initializer_list<long double>&& initList);
|
||||
~Matrix();
|
||||
|
||||
std::size_t GetRawCount() const;
|
||||
@@ -28,18 +28,18 @@ class Matrix {
|
||||
|
||||
void Transpose();
|
||||
|
||||
static Matrix Identity(std::size_t taille);
|
||||
static Matrix Identity(std::size_t size);
|
||||
|
||||
void Augmenter(const Matrix& droite);
|
||||
void Augment(const Matrix& right);
|
||||
|
||||
Matrix SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne, std::size_t ligne, std::size_t colonne) const;
|
||||
Matrix SubMatrix(std::size_t raw_origin, std::size_t column_origin, std::size_t raw, std::size_t column) const;
|
||||
|
||||
bool operator==(const Matrix& other) const;
|
||||
Matrix operator*(const Matrix& other) const;
|
||||
long double& operator[](std::size_t indice);
|
||||
long double& operator[](std::size_t index);
|
||||
|
||||
long double& at(std::size_t ligne, std::size_t colonne);
|
||||
long double at(std::size_t ligne, std::size_t colonne) const;
|
||||
long double& at(std::size_t raw, std::size_t column);
|
||||
long double at(std::size_t raw, std::size_t column) const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& stream, const Matrix& mat);
|
||||
friend std::istream& operator>>(std::istream& stream, Matrix& mat);
|
||||
|
||||
@@ -13,10 +13,10 @@ Vect Solver::Image() const {
|
||||
}
|
||||
|
||||
// https://en.wikipedia.org/wiki/Kernel_(linear_algebra)#Computation_by_Gaussian_elimination
|
||||
Vect Solver::Noyau() const {
|
||||
Vect Solver::Kernel() const {
|
||||
Matrix result = m_Matrix;
|
||||
result.Transpose();
|
||||
result.Augmenter(Matrix::Identity(result.GetRawCount()));
|
||||
result.Augment(Matrix::Identity(result.GetRawCount()));
|
||||
Gauss::GaussJordan(result, true, true);
|
||||
result.Transpose();
|
||||
|
||||
@@ -27,19 +27,19 @@ Vect Solver::Noyau() const {
|
||||
result.GetColumnCount() - origine_colonne)};
|
||||
}
|
||||
|
||||
VectAffine Solver::SystemeTriangulaire() const {
|
||||
VectAffine Solver::TriangularSystem() const {
|
||||
Matrix mat = m_Matrix;
|
||||
Gauss::GaussJordan(mat, true, true);
|
||||
|
||||
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);
|
||||
|
||||
return {noyau, origin};
|
||||
}
|
||||
|
||||
std::size_t Solver::Rang() const {
|
||||
std::size_t Solver::Rank() const {
|
||||
Vect image = Image();
|
||||
return image.GetCardinal();
|
||||
}
|
||||
|
||||
@@ -12,9 +12,9 @@ class Solver {
|
||||
~Solver() {}
|
||||
|
||||
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;
|
||||
};
|
||||
@@ -44,7 +44,7 @@ bool Vect::operator==(const Vect& other) const {
|
||||
}
|
||||
|
||||
void Vect::AddVector(const Matrix& mat) {
|
||||
m_Data.Augmenter(mat);
|
||||
m_Data.Augment(mat);
|
||||
m_Data.Transpose();
|
||||
Gauss::GaussJordan(m_Data, false, false);
|
||||
m_Data.Transpose();
|
||||
@@ -60,7 +60,7 @@ Matrix Vect::GetLinearSystem() const {
|
||||
vect.Transpose();
|
||||
|
||||
Solver solver {vect};
|
||||
vect = solver.Noyau().m_Data;
|
||||
vect = solver.Kernel().m_Data;
|
||||
vect.Transpose();
|
||||
return vect;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ void test() {
|
||||
Solver solver {mat2};
|
||||
|
||||
Vect image = solver.Image();
|
||||
Vect noyau = solver.Noyau();
|
||||
Vect noyau = solver.Kernel();
|
||||
|
||||
std::cout << "\tImage :\n";
|
||||
image.Print();
|
||||
@@ -35,7 +35,7 @@ void test() {
|
||||
noyau.GetLinearSystem().Print();
|
||||
|
||||
std::cout << "\n\n";
|
||||
solver.SystemeTriangulaire().Print();
|
||||
solver.TriangularSystem().Print();
|
||||
}
|
||||
|
||||
void prompt() {
|
||||
|
||||
@@ -25,7 +25,7 @@ int main() {
|
||||
Solver solver {mat};
|
||||
|
||||
assert(solver.Image() == image);
|
||||
assert(solver.Noyau() == noyau);
|
||||
assert(solver.Kernel() == noyau);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user