refactor project
This commit is contained in:
100
src/Matrix.cpp
100
src/Matrix.cpp
@@ -25,6 +25,7 @@ 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;
|
||||
return {1, 1, {0}};
|
||||
}
|
||||
|
||||
Matrix result(m_Lignes, other.m_Colonnes);
|
||||
@@ -53,13 +54,6 @@ void Matrix::Print() const {
|
||||
}
|
||||
}
|
||||
|
||||
void Matrix::PrintDebug() {
|
||||
#ifndef NDEBUG
|
||||
Print();
|
||||
std::cout << "\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
void Matrix::Insert() {
|
||||
for (size_t i = 0; i < m_Lignes; ++i) {
|
||||
for (size_t j = 0; j < m_Colonnes; ++j) {
|
||||
@@ -97,32 +91,14 @@ void Matrix::Transpose() {
|
||||
*this = result;
|
||||
}
|
||||
|
||||
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++) {
|
||||
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;
|
||||
for (j = 0; j < m_Colonnes; ++j) {
|
||||
if (!IsEqualZero(at(i, j))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
for (std::size_t i = 0; i < taille; i++) {
|
||||
for (std::size_t j = i; j < taille; j++) {
|
||||
id.at(i, j) = (i == j);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return id;
|
||||
}
|
||||
|
||||
void Matrix::Augmenter(const Matrix& droite) {
|
||||
@@ -158,72 +134,6 @@ bool Matrix::operator==(const Matrix& other) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Matrix::GaussNonJordan(bool reduite) {
|
||||
int r = -1;
|
||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
||||
std::size_t indice_ligne_maximum = r + 1;
|
||||
|
||||
// Recherche maximum
|
||||
for (std::size_t i = r + 1; i < m_Lignes; i++) {
|
||||
if (std::abs(at(i, j)) > std::abs(at(indice_ligne_maximum, j)))
|
||||
indice_ligne_maximum = i;
|
||||
}
|
||||
|
||||
// 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 (!IsEqualZero(at(indice_ligne_maximum, j))) {
|
||||
r++;
|
||||
|
||||
// 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';
|
||||
for (std::size_t k = 0; k < m_Colonnes; k++) {
|
||||
std::swap(at(indice_ligne_maximum, k), at(r, k));
|
||||
}
|
||||
}
|
||||
|
||||
// Pour i de 1 jusqu'à n (On simplifie les autres lignes)
|
||||
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])
|
||||
for (int k = m_Colonnes - 1; k >= 0; k--) {
|
||||
long double pivot = at(r, j);
|
||||
long double anul = at(i, j);
|
||||
at(i, k) = at(i, k) * pivot - at(r, k) * anul;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Matrix::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 (!IsEqualZero(at(i, j))) {
|
||||
k = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// ligne de 0
|
||||
if (k == -1)
|
||||
break;
|
||||
// on divise la ligne par (i, k)
|
||||
long double annul = at(i, k);
|
||||
for (int j = 0; j < m_Colonnes; j++) {
|
||||
at(i, j) /= annul;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
long double& Matrix::operator[](std::size_t indice) {
|
||||
return m_Data[indice];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user