better jordan

This commit is contained in:
2024-02-02 16:10:34 +01:00
parent 6136a8f88b
commit 4b032ffaeb
6 changed files with 68 additions and 30 deletions

3
matricies/matrice2x2.mat Normal file
View File

@@ -0,0 +1,3 @@
2 2
6 9
9 6

5
matricies/matrice4x4.mat Normal file
View File

@@ -0,0 +1,5 @@
4 4
1 2 3 4
8 9 10 11
5 6 7 8
12 13 14 15

View File

@@ -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

6
matricies/matrice5x5.mat Normal file
View File

@@ -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

View File

@@ -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; 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;
@@ -102,25 +110,18 @@ class Matrix {
indice_ligne_maximum = i; 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) // 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++; 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(); PrintDebug();
// Si k≠r alors // Si k≠r alors
if (indice_ligne_maximum != r) { if (indice_ligne_maximum != r) {
// Échanger les lignes k et r (On place la ligne du pivot en position 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++) { 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));
} }
@@ -129,16 +130,15 @@ class Matrix {
PrintDebug(); 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 = 0; 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])
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--) { for (int k = m_Colonnes - 1; k >= 0; k--) {
at(i, k) -= at(r, k) * at(i, j); T pivot = at(r, j);
std::cout << "On soustrait à (" << i << "," << k << ") , [ (" << r << "," << k << ") = " << at(r, k) T anul = at(i, j);
<< " ] * [ (" << i << "," << j << ") = " << at(i, j) << " ]\n"; at(i, k) = at(i, k) * pivot - at(r, k) * anul;
PrintDebug(); 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) { T& operator[](std::size_t indice) {
return at(indice); return at(indice);
} }
@@ -158,12 +179,4 @@ class Matrix {
T at(std::size_t ligne, std::size_t colonne) const { T at(std::size_t ligne, std::size_t colonne) const {
return m_Data[ligne * m_Lignes + colonne]; return m_Data[ligne * m_Lignes + colonne];
} }
/*std::vector<T>::iterator begin() {
return m_Data.begin();
}
std::vector<T>::iterator end() {
return m_Data.end();
}*/
}; };

View File

@@ -1,13 +1,19 @@
#include "Matrix.h" #include "Matrix.h"
void test() { void test() {
Matrix<float> mat{"matrice3x3.mat"}; Matrix<float> mat{"matrice5x5.mat"};
mat.Print(); mat.Print();
// mat.Save("matrice3x3.mat"); // mat.Save("matrice3x3.mat");
mat.GaussJordan(); mat.GaussJordan(true);
std::cout << "sdfdjiofoseifheoiefhoig\n";
mat.Print();
mat = {"matrice5x5.mat"};
mat.GaussJordan(false);
std::cout << "\nResultat :\n"; std::cout << "\nResultat :\n";
mat.Print(); mat.Print();
mat.Save("matrice3x3echelonne.mat"); mat.Transpose();
mat.Print();
// mat.Save("matrice4x4echelonne.mat");
} }
void prompt() { void prompt() {
@@ -24,7 +30,7 @@ void prompt() {
mat.Insert(); mat.Insert();
mat.Print(); mat.Print();
mat.GaussJordan(); mat.GaussJordan(true);
std::cout << std::endl; std::cout << std::endl;
mat.Print(); mat.Print();
} }