From 4b032ffaeb325bdca4b889ee891bae00ab99c517 Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Fri, 2 Feb 2024 16:10:34 +0100 Subject: [PATCH] better jordan --- matricies/matrice2x2.mat | 3 ++ matricies/matrice4x4.mat | 5 +++ matricies/matrice4x4echelonne.mat | 5 +++ matricies/matrice5x5.mat | 6 +++ src/Matrix.h | 63 +++++++++++++++++++------------ src/main.cpp | 16 +++++--- 6 files changed, 68 insertions(+), 30 deletions(-) create mode 100644 matricies/matrice2x2.mat create mode 100644 matricies/matrice4x4.mat create mode 100644 matricies/matrice4x4echelonne.mat create mode 100644 matricies/matrice5x5.mat diff --git a/matricies/matrice2x2.mat b/matricies/matrice2x2.mat new file mode 100644 index 0000000..39cb320 --- /dev/null +++ b/matricies/matrice2x2.mat @@ -0,0 +1,3 @@ +2 2 +6 9 +9 6 \ No newline at end of file diff --git a/matricies/matrice4x4.mat b/matricies/matrice4x4.mat new file mode 100644 index 0000000..97b2683 --- /dev/null +++ b/matricies/matrice4x4.mat @@ -0,0 +1,5 @@ +4 4 +1 2 3 4 +8 9 10 11 +5 6 7 8 +12 13 14 15 \ No newline at end of file diff --git a/matricies/matrice4x4echelonne.mat b/matricies/matrice4x4echelonne.mat new file mode 100644 index 0000000..61d4751 --- /dev/null +++ b/matricies/matrice4x4echelonne.mat @@ -0,0 +1,5 @@ +4 4 +1 0 0 -0.786594 +0 1 0 8.94049 +0 0 1 -2.0538 +0 0 0 0 diff --git a/matricies/matrice5x5.mat b/matricies/matrice5x5.mat new file mode 100644 index 0000000..52108eb --- /dev/null +++ b/matricies/matrice5x5.mat @@ -0,0 +1,6 @@ +5 5 +1 2 3 4 5 +16 17 18 19 20 +6 7 8 9 10 +11 12 13 14 15 +21 22 23 24 25 \ No newline at end of file diff --git a/src/Matrix.h b/src/Matrix.h index 0fccbad..3b85be1 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -91,7 +91,15 @@ class Matrix { } } - void GaussJordan() { + void Transpose() { + for (std::size_t i = 0; i < m_Lignes; i++) { + for (std::size_t j = i; j < m_Colonnes; j++) { + std::swap(at(i, j), at(j, i)); + } + } + } + + void GaussNonJordan(bool reduite) { int r = -1; for (std::size_t j = 0; j < m_Colonnes; j++) { std::size_t indice_ligne_maximum = r + 1; @@ -102,25 +110,18 @@ class Matrix { indice_ligne_maximum = i; } - std::cout << "l'indice du maximum est : " << indice_ligne_maximum << "\n\n"; + // 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 (!EqualZero(at(indice_ligne_maximum, j))) { + if (at(indice_ligne_maximum, j) != 0) { r++; - std::cout << "On divise la ligne " << indice_ligne_maximum << " par " << at(indice_ligne_maximum, j) << "\n"; - // Diviser la ligne k par A[k,j] (On normalise la ligne de pivot de façon que le - // pivot prenne la valeur 1) - for (int l = m_Colonnes - 1; l >= 0; l--) { - at(indice_ligne_maximum, l) /= at(indice_ligne_maximum, j); - } - 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'; + // 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)); } @@ -129,16 +130,15 @@ class Matrix { PrintDebug(); // Pour i de 1 jusqu'à n (On simplifie les autres lignes) - for (std::size_t i = 0; i < m_Lignes; i++) { + 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]) - std::cout << "On soustrait à la ligne " << i << " la ligne " << r << " multipliée par " << at(i, j) << "\n"; for (int k = m_Colonnes - 1; k >= 0; k--) { - at(i, k) -= at(r, k) * at(i, j); - std::cout << "On soustrait à (" << i << "," << k << ") , [ (" << r << "," << k << ") = " << at(r, k) - << " ] * [ (" << i << "," << j << ") = " << at(i, j) << " ]\n"; + T pivot = at(r, j); + T anul = at(i, j); + at(i, k) = at(i, k) * pivot - at(r, k) * anul; PrintDebug(); } } @@ -147,6 +147,27 @@ class Matrix { } } + void 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 (at(i, j) != 0) { + k = j; + break; + } + } + // ligne de 0 + if (k == -1) + break; + // on divise la ligne par (i, k) + T annul = at(i, k); + for (int j = 0; j < m_Colonnes; j++) { + at(i, j) /= annul; + } + } + } + T& operator[](std::size_t indice) { return at(indice); } @@ -158,12 +179,4 @@ class Matrix { T at(std::size_t ligne, std::size_t colonne) const { return m_Data[ligne * m_Lignes + colonne]; } - - /*std::vector::iterator begin() { - return m_Data.begin(); - } - - std::vector::iterator end() { - return m_Data.end(); - }*/ -}; \ No newline at end of file +}; diff --git a/src/main.cpp b/src/main.cpp index 94e04f9..8959408 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,19 @@ #include "Matrix.h" void test() { - Matrix mat{"matrice3x3.mat"}; + Matrix mat{"matrice5x5.mat"}; mat.Print(); - //mat.Save("matrice3x3.mat"); - mat.GaussJordan(); + // mat.Save("matrice3x3.mat"); + mat.GaussJordan(true); + std::cout << "sdfdjiofoseifheoiefhoig\n"; + mat.Print(); + mat = {"matrice5x5.mat"}; + mat.GaussJordan(false); std::cout << "\nResultat :\n"; mat.Print(); - mat.Save("matrice3x3echelonne.mat"); + mat.Transpose(); + mat.Print(); + // mat.Save("matrice4x4echelonne.mat"); } void prompt() { @@ -24,7 +30,7 @@ void prompt() { mat.Insert(); mat.Print(); - mat.GaussJordan(); + mat.GaussJordan(true); std::cout << std::endl; mat.Print(); }