75 lines
2.0 KiB
C++
75 lines
2.0 KiB
C++
#include "Gauss.h"
|
|
|
|
#include "Matrix.h"
|
|
|
|
namespace Gauss {
|
|
|
|
static void GaussNonJordan(Matrix& mat, bool reduite) {
|
|
int r = -1;
|
|
for (std::size_t j = 0; j < mat.GetColumnCount(); j++) {
|
|
std::size_t indice_ligne_maximum = r + 1;
|
|
|
|
// Recherche maximum
|
|
for (std::size_t i = r + 1; i < mat.GetRawCount(); i++) {
|
|
if (std::abs(mat.at(i, j)) > std::abs(mat.at(indice_ligne_maximum, j)))
|
|
indice_ligne_maximum = i;
|
|
}
|
|
|
|
// Si A[k,j]≠0 alors (A[k,j] désigne la valeur de la ligne k et de la colonne j)
|
|
if (!IsEqualZero(mat.at(indice_ligne_maximum, j))) {
|
|
r++;
|
|
|
|
// Si k≠r alors
|
|
if (indice_ligne_maximum != r) {
|
|
// Échanger les lignes k et r (On place la ligne du pivot en position r)
|
|
for (std::size_t k = 0; k < mat.GetColumnCount(); k++) {
|
|
std::swap(mat.at(indice_ligne_maximum, k), mat.at(r, k));
|
|
}
|
|
}
|
|
|
|
// Pour i de 1 jusqu'à n (On simplifie les autres lignes)
|
|
for (std::size_t i = (reduite ? 0 : j); i < mat.GetRawCount(); 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])
|
|
for (int k = mat.GetColumnCount() - 1; k >= 0; k--) {
|
|
long double pivot = mat.at(r, j);
|
|
long double anul = mat.at(i, j);
|
|
mat.at(i, k) = mat.at(i, k) * pivot - mat.at(r, k) * anul;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
static void GaussJordan(Matrix& mat, bool reduite) {
|
|
GaussNonJordan(mat, reduite);
|
|
for (std::size_t i = 0; i < mat.GetRawCount(); i++) {
|
|
int k = -1;
|
|
for (std::size_t j = 0; j < mat.GetColumnCount(); j++) {
|
|
if (!IsEqualZero(mat.at(i, j))) {
|
|
k = j;
|
|
break;
|
|
}
|
|
}
|
|
// ligne de 0
|
|
if (k == -1)
|
|
break;
|
|
// on divise la ligne par (i, k)
|
|
long double annul = mat.at(i, k);
|
|
for (int j = 0; j < mat.GetColumnCount(); j++) {
|
|
mat.at(i, j) /= annul;
|
|
}
|
|
}
|
|
}
|
|
|
|
void GaussJordan(Matrix& mat, bool reduite, bool normalise) {
|
|
if (normalise)
|
|
GaussJordan(mat, reduite);
|
|
else
|
|
GaussNonJordan(mat, reduite);
|
|
}
|
|
|
|
} // namespace Gauss
|