Compare commits
4 Commits
a016163b86
...
4ffeea8900
| Author | SHA1 | Date | |
|---|---|---|---|
| 4ffeea8900 | |||
| e1f501237d | |||
| 8ef738db55 | |||
| cf64e4d33c |
@@ -109,17 +109,20 @@ void Matrix::Transpose() {
|
||||
}
|
||||
|
||||
void Matrix::Identity() {
|
||||
assert(m_Lignes == m_Colonnes);
|
||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
||||
for (std::size_t j = i; j < m_Colonnes; j++) {
|
||||
if (i != j) {
|
||||
at(i, j) = 0;
|
||||
} else {
|
||||
at(i, j) = 1;
|
||||
}
|
||||
at(i, j) = i == j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Matrix Matrix::Identity(std::size_t taille) {
|
||||
Matrix id{taille, taille};
|
||||
id.Identity();
|
||||
return id;
|
||||
}
|
||||
|
||||
bool Matrix::IsInversed() const {
|
||||
for (std::size_t i = 0; i < m_Lignes; ++i) {
|
||||
std::size_t j;
|
||||
@@ -133,6 +136,25 @@ bool Matrix::IsInversed() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Matrix::Augmenter(const Matrix& droite) {
|
||||
assert(droite.m_Lignes == m_Lignes);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
*this = temp;
|
||||
}
|
||||
|
||||
bool Matrix::operator==(const Matrix& other) const {
|
||||
if (m_Lignes != other.m_Lignes || m_Colonnes != other.m_Colonnes)
|
||||
return false;
|
||||
|
||||
@@ -40,8 +40,12 @@ class Matrix {
|
||||
|
||||
void Identity();
|
||||
|
||||
static Matrix Identity(std::size_t taille);
|
||||
|
||||
bool IsInversed() 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;
|
||||
|
||||
31
src/Solver.cpp
Normal file
31
src/Solver.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "Solver.h"
|
||||
|
||||
Solver::Solver(const Matrix& mat) : m_Matrix(mat) {}
|
||||
|
||||
Vect Solver::Image() const {
|
||||
Matrix result = m_Matrix;
|
||||
result.Transpose();
|
||||
result.GaussJordan(true);
|
||||
result.Transpose();
|
||||
return {result};
|
||||
}
|
||||
|
||||
// https://en.wikipedia.org/wiki/Kernel_(linear_algebra)#Computation_by_Gaussian_elimination
|
||||
Vect Solver::Noyau() const {
|
||||
Matrix result = m_Matrix;
|
||||
result.Transpose();
|
||||
result.Augmenter(Matrix::Identity(result.GetRawCount()));
|
||||
result.GaussJordan(true);
|
||||
result.Transpose();
|
||||
|
||||
// nombre de colonnes non nulles
|
||||
std::size_t origine_colonne = Vect(result.SubMatrix(0, 0, m_Matrix.GetRawCount(), m_Matrix.GetColumnCount())).GetDimension();
|
||||
|
||||
return {result.SubMatrix(m_Matrix.GetRawCount(), origine_colonne, result.GetRawCount() - m_Matrix.GetRawCount(),
|
||||
result.GetColumnCount() - origine_colonne)};
|
||||
}
|
||||
|
||||
std::size_t Solver::Rang() const {
|
||||
Vect image = Image();
|
||||
return image.GetDimension();
|
||||
}
|
||||
16
src/Solver.h
Normal file
16
src/Solver.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "Vect.h"
|
||||
|
||||
class Solver {
|
||||
private:
|
||||
Matrix m_Matrix;
|
||||
public:
|
||||
Solver(const Matrix& mat);
|
||||
~Solver() {}
|
||||
|
||||
Vect Image() const;
|
||||
Vect Noyau() const;
|
||||
|
||||
std::size_t Rang() const;
|
||||
};
|
||||
@@ -22,11 +22,15 @@ void Vect::Simplify() {
|
||||
}
|
||||
|
||||
void Vect::Print() const {
|
||||
std::cout << "Espace vectoriel de dimension " << m_Data.GetColumnCount() << " de base :\n\n";
|
||||
std::cout << "Espace vectoriel de dimension " << GetDimension() << " de base :\n\n";
|
||||
for (std::size_t i = 0; i < m_Data.GetRawCount(); i++) {
|
||||
for (std::size_t j = 0; j < m_Data.GetColumnCount(); j++) {
|
||||
printf("[ %.3f ]\t", static_cast<float>(m_Data.at(i, j)));
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t Vect::GetDimension() const {
|
||||
return m_Data.GetColumnCount();
|
||||
}
|
||||
@@ -20,6 +20,8 @@ class Vect {
|
||||
*/
|
||||
void Print() const;
|
||||
|
||||
std::size_t GetDimension() const;
|
||||
|
||||
private:
|
||||
void Simplify();
|
||||
};
|
||||
26
src/main.cpp
26
src/main.cpp
@@ -1,8 +1,8 @@
|
||||
#include "Vect.h"
|
||||
#include "Solver.h"
|
||||
#include <iostream>
|
||||
|
||||
void test() {
|
||||
Matrix mat{"matrice4x4.mat"};
|
||||
/* Matrix mat{"matrice4x4.mat"};
|
||||
mat.Print();
|
||||
// mat.Save("matrice3x3.mat");
|
||||
std::cout << "sdfdjiofoseifheoiefhoig\n";
|
||||
@@ -14,23 +14,17 @@ void test() {
|
||||
mat.Transpose();
|
||||
std::cout << "<<\nTransposée:\n";
|
||||
mat.Print();
|
||||
// mat.Save("matrice4x4echelonne.mat");
|
||||
// mat.Save("matrice4x4echelonne.mat"); */
|
||||
|
||||
Matrix mat2 {"matrice4x4.mat"};
|
||||
mat2.Print();
|
||||
|
||||
mat2.Transpose();
|
||||
std::cout << "Transposée : \n";
|
||||
mat2.Print();
|
||||
|
||||
mat2.GaussJordan(true);
|
||||
mat2.Transpose();
|
||||
|
||||
std::cout << "Echelonnée en colonne :\n";
|
||||
Matrix mat2{"matrice4x4.mat"};
|
||||
mat2.Print();
|
||||
|
||||
Vect sol {mat2};
|
||||
sol.Print();
|
||||
Solver solver{mat2};
|
||||
|
||||
std::cout << "\tImage :\n";
|
||||
solver.Image().Print();
|
||||
std::cout << "\tNoyau :\n";
|
||||
solver.Noyau().Print();
|
||||
}
|
||||
|
||||
void prompt() {
|
||||
|
||||
Reference in New Issue
Block a user