add some stuff
This commit is contained in:
88
src/Matrix.h
88
src/Matrix.h
@@ -33,6 +33,25 @@ class 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 {
|
||||
for (size_t i = 0; i < m_Lignes; ++i) {
|
||||
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;
|
||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
||||
std::size_t indice_ligne_maximum = r + 1;
|
||||
@@ -116,7 +183,7 @@ class Matrix {
|
||||
if (at(indice_ligne_maximum, j) != 0) {
|
||||
r++;
|
||||
|
||||
PrintDebug();
|
||||
// PrintDebug();
|
||||
|
||||
// Si k≠r alors
|
||||
if (indice_ligne_maximum != r) {
|
||||
@@ -124,33 +191,37 @@ class Matrix {
|
||||
// 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));
|
||||
// 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)
|
||||
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])
|
||||
T anulid = at(i, j);
|
||||
for (int k = m_Colonnes - 1; k >= 0; k--) {
|
||||
T pivot = at(r, j);
|
||||
T anul = at(i, j);
|
||||
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) {
|
||||
GaussNonJordan(reduite);
|
||||
Matrix GaussJordan(bool reduite, bool augmentee) {
|
||||
Matrix<float> aug = GaussNonJordan(reduite, augmentee);
|
||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
||||
int k = -1;
|
||||
int kaugmentee = -1;
|
||||
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
||||
if (at(i, j) != 0) {
|
||||
k = j;
|
||||
@@ -164,8 +235,11 @@ class Matrix {
|
||||
T annul = at(i, k);
|
||||
for (int j = 0; j < m_Colonnes; j++) {
|
||||
at(i, j) /= annul;
|
||||
// augmentee
|
||||
aug.at(i, j) /= annul;
|
||||
}
|
||||
}
|
||||
return aug;
|
||||
}
|
||||
|
||||
T& operator[](std::size_t indice) {
|
||||
|
||||
Reference in New Issue
Block a user