add some stuff
This commit is contained in:
88
src/Matrix.h
88
src/Matrix.h
@@ -33,6 +33,25 @@ class Matrix {
|
|||||||
}
|
}
|
||||||
~Matrix() {}
|
~Matrix() {}
|
||||||
|
|
||||||
|
Matrix operator*(const Matrix& other) const {
|
||||||
|
if (m_Colonnes != other.m_Lignes) {
|
||||||
|
std::cerr << "Mutiplication impossible car la dimensions des matrices est incompatible" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix result(m_Lignes, other.m_Colonnes);
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < m_Lignes; ++i) {
|
||||||
|
for (std::size_t j = 0; j < other.m_Colonnes; ++j) {
|
||||||
|
T sum = 0;
|
||||||
|
for (std::size_t k = 0; k < m_Colonnes; k++) {
|
||||||
|
sum += at(i, k) * other.at(k, j);
|
||||||
|
}
|
||||||
|
result.at(i, j) = sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
void Print() const {
|
void Print() const {
|
||||||
for (size_t i = 0; i < m_Lignes; ++i) {
|
for (size_t i = 0; i < m_Lignes; ++i) {
|
||||||
std::cout << "[ ";
|
std::cout << "[ ";
|
||||||
@@ -99,7 +118,55 @@ class Matrix {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GaussNonJordan(bool reduite) {
|
void Identity() {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsInverse(const Matrix& mat, const Matrix& aug) const {
|
||||||
|
Matrix result = mat * aug;
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < m_Lignes; ++i) {
|
||||||
|
for (std::size_t j = 0; j < m_Colonnes; ++j) {
|
||||||
|
if ((i == j && !EqualZero(result.at(i, j) - 1.0)) || (i != j && EqualZero(result.at(i, j)))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix GaussNonJordan(bool reduite, bool augmentee) {
|
||||||
|
Matrix<float> aug = Matrix(m_Lignes, m_Colonnes);
|
||||||
|
if (augmentee) {
|
||||||
|
std::size_t choix;
|
||||||
|
std::cout
|
||||||
|
<< "Entrez 1 pour rentrer la matrice augmentee que je vous voulez ou 2 pour la matrice augmentee Id pour trouver "
|
||||||
|
"l'inverse si elle existe."
|
||||||
|
<< std::endl;
|
||||||
|
std::cin >> choix;
|
||||||
|
while (choix != 1 && choix != 2) {
|
||||||
|
std::cout
|
||||||
|
<< "Entrez 1 pour rentrer la matrice augmentee que je vous voulez ou 2 pour la matrice augmentee Id pour trouver "
|
||||||
|
"l'inverse si elle existe."
|
||||||
|
<< std::endl;
|
||||||
|
std::cin >> choix;
|
||||||
|
}
|
||||||
|
if (choix == 1) {
|
||||||
|
std::cout << "Rentrez les coefficients de la matrice" << std::endl;
|
||||||
|
aug.Insert();
|
||||||
|
} else {
|
||||||
|
aug.Identity();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
aug.Print();
|
||||||
int r = -1;
|
int r = -1;
|
||||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
||||||
std::size_t indice_ligne_maximum = r + 1;
|
std::size_t indice_ligne_maximum = r + 1;
|
||||||
@@ -116,7 +183,7 @@ class Matrix {
|
|||||||
if (at(indice_ligne_maximum, j) != 0) {
|
if (at(indice_ligne_maximum, j) != 0) {
|
||||||
r++;
|
r++;
|
||||||
|
|
||||||
PrintDebug();
|
// PrintDebug();
|
||||||
|
|
||||||
// Si k≠r alors
|
// Si k≠r alors
|
||||||
if (indice_ligne_maximum != r) {
|
if (indice_ligne_maximum != r) {
|
||||||
@@ -124,33 +191,37 @@ class Matrix {
|
|||||||
// std::cout << "On échange les lignes " << indice_ligne_maximum << " et " << r << '\n';
|
// std::cout << "On échange les lignes " << indice_ligne_maximum << " et " << r << '\n';
|
||||||
for (std::size_t k = 0; k < m_Colonnes; k++) {
|
for (std::size_t k = 0; k < m_Colonnes; k++) {
|
||||||
std::swap(at(indice_ligne_maximum, k), at(r, k));
|
std::swap(at(indice_ligne_maximum, k), at(r, k));
|
||||||
|
// matrice augmentee
|
||||||
|
std::swap(aug.at(indice_ligne_maximum, k), aug.at(r, k));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PrintDebug();
|
|
||||||
|
|
||||||
// Pour i de 1 jusqu'à n (On simplifie les autres lignes)
|
// Pour i de 1 jusqu'à n (On simplifie les autres lignes)
|
||||||
for (std::size_t i = (reduite ? 0 : j); i < m_Lignes; i++) {
|
for (std::size_t i = (reduite ? 0 : j); i < m_Lignes; i++) {
|
||||||
// Si i≠r alors
|
// Si i≠r alors
|
||||||
if (i != r) {
|
if (i != r) {
|
||||||
// Soustraire à la ligne i la ligne r multipliée par A[i,j] (de façon à
|
// Soustraire à la ligne i la ligne r multipliée par A[i,j] (de façon à
|
||||||
// annuler A[i,j])
|
// annuler A[i,j])
|
||||||
|
T anulid = at(i, j);
|
||||||
for (int k = m_Colonnes - 1; k >= 0; k--) {
|
for (int k = m_Colonnes - 1; k >= 0; k--) {
|
||||||
T pivot = at(r, j);
|
T pivot = at(r, j);
|
||||||
T anul = at(i, j);
|
T anul = at(i, j);
|
||||||
at(i, k) = at(i, k) * pivot - at(r, k) * anul;
|
at(i, k) = at(i, k) * pivot - at(r, k) * anul;
|
||||||
PrintDebug();
|
// matrice augmentee
|
||||||
|
aug.at(i, k) = aug.at(i, k) * pivot - aug.at(r, k) * anulid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return (aug);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GaussJordan(bool reduite) {
|
Matrix GaussJordan(bool reduite, bool augmentee) {
|
||||||
GaussNonJordan(reduite);
|
Matrix<float> aug = GaussNonJordan(reduite, augmentee);
|
||||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
for (std::size_t i = 0; i < m_Lignes; i++) {
|
||||||
int k = -1;
|
int k = -1;
|
||||||
|
int kaugmentee = -1;
|
||||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
||||||
if (at(i, j) != 0) {
|
if (at(i, j) != 0) {
|
||||||
k = j;
|
k = j;
|
||||||
@@ -164,8 +235,11 @@ class Matrix {
|
|||||||
T annul = at(i, k);
|
T annul = at(i, k);
|
||||||
for (int j = 0; j < m_Colonnes; j++) {
|
for (int j = 0; j < m_Colonnes; j++) {
|
||||||
at(i, j) /= annul;
|
at(i, j) /= annul;
|
||||||
|
// augmentee
|
||||||
|
aug.at(i, j) /= annul;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return aug;
|
||||||
}
|
}
|
||||||
|
|
||||||
T& operator[](std::size_t indice) {
|
T& operator[](std::size_t indice) {
|
||||||
|
|||||||
10
src/main.cpp
10
src/main.cpp
@@ -4,11 +4,10 @@ void test() {
|
|||||||
Matrix<float> mat{"matrice5x5.mat"};
|
Matrix<float> mat{"matrice5x5.mat"};
|
||||||
mat.Print();
|
mat.Print();
|
||||||
// mat.Save("matrice3x3.mat");
|
// mat.Save("matrice3x3.mat");
|
||||||
mat.GaussJordan(true);
|
|
||||||
std::cout << "sdfdjiofoseifheoiefhoig\n";
|
std::cout << "sdfdjiofoseifheoiefhoig\n";
|
||||||
mat.Print();
|
mat.Print();
|
||||||
mat = {"matrice5x5.mat"};
|
mat = {"matrice5x5.mat"};
|
||||||
mat.GaussJordan(false);
|
mat.GaussJordan(false, true);
|
||||||
std::cout << "\nResultat :\n";
|
std::cout << "\nResultat :\n";
|
||||||
mat.Print();
|
mat.Print();
|
||||||
mat.Transpose();
|
mat.Transpose();
|
||||||
@@ -30,13 +29,16 @@ void prompt() {
|
|||||||
mat.Insert();
|
mat.Insert();
|
||||||
|
|
||||||
mat.Print();
|
mat.Print();
|
||||||
mat.GaussJordan(true);
|
Matrix<float> aug = mat.GaussJordan(true, true);
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
std::cout << "Matrice echelonnee reduite" << std::endl;
|
||||||
mat.Print();
|
mat.Print();
|
||||||
|
std::cout << "Matrice augmentee" << std::endl;
|
||||||
|
aug.Print();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
test();
|
// test();
|
||||||
prompt();
|
prompt();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user