269 lines
6.3 KiB
C++
269 lines
6.3 KiB
C++
#include "Matrix.h"
|
|
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <cmath>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
Matrix::Matrix(const std::string& fileNameInput) {
|
|
Load(fileNameInput);
|
|
}
|
|
|
|
Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Lignes(lignes), m_Colonnes(colonnes) {
|
|
m_Data.resize(m_Lignes * m_Colonnes);
|
|
}
|
|
Matrix::Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list<long double>&& initList) :
|
|
m_Lignes(lignes), m_Colonnes(colonnes) {
|
|
m_Data = initList;
|
|
m_Data.resize(m_Lignes * m_Colonnes);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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) {
|
|
long double 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 Matrix::Print() const {
|
|
for (size_t i = 0; i < m_Lignes; ++i) {
|
|
std::cout << "[ ";
|
|
for (size_t j = 0; j < m_Colonnes; ++j) {
|
|
std::size_t indice = i * m_Lignes + j;
|
|
std::cout << at(i, j) << " ";
|
|
}
|
|
std::cout << "]";
|
|
std::cout << std::endl;
|
|
}
|
|
}
|
|
|
|
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) {
|
|
std::cin >> at(i, j);
|
|
}
|
|
std::cout << std::endl;
|
|
}
|
|
}
|
|
|
|
void Matrix::Save(const std::string& fileName) {
|
|
std::ofstream out{fileName};
|
|
if (!out) {
|
|
std::cerr << "Impossible de sauvegarder la matrice !\n";
|
|
return;
|
|
}
|
|
out << m_Lignes << " " << m_Colonnes << "\n";
|
|
for (std::size_t i = 0; i < m_Lignes; i++) {
|
|
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
|
out << at(i, j) << " ";
|
|
}
|
|
out << "\n";
|
|
}
|
|
}
|
|
|
|
void Matrix::Load(const std::string& filename) {
|
|
std::ifstream in{filename};
|
|
if (!in) {
|
|
std::cerr << "Impossible de charger la matrice !\n";
|
|
return;
|
|
}
|
|
in >> m_Lignes >> m_Colonnes;
|
|
m_Data.resize(m_Lignes * m_Colonnes);
|
|
for (std::size_t i = 0; i < m_Lignes; i++) {
|
|
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
|
in >> at(i, j);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Matrix::Transpose() {
|
|
Matrix result{m_Colonnes, m_Lignes};
|
|
for (std::size_t i = 0; i < m_Lignes; i++) {
|
|
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
|
result.at(j, i) = at(i, j);
|
|
}
|
|
}
|
|
*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;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void Matrix::Augmenter(const Matrix& droite) {
|
|
assert(droite.m_Lignes == m_Lignes);
|
|
Matrix temp{m_Lignes, m_Colonnes + droite.m_Colonnes};
|
|
|
|
for (std::size_t i = 0; i < m_Lignes; i++) {
|
|
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
|
temp.at(i, j) = at(i, j);
|
|
}
|
|
}
|
|
|
|
for (std::size_t i = 0; i < m_Lignes; i++) {
|
|
for (std::size_t j = 0; j < droite.m_Colonnes; j++) {
|
|
temp.at(i, j + m_Colonnes) = droite.at(i, j);
|
|
}
|
|
}
|
|
|
|
*this = temp;
|
|
}
|
|
|
|
bool Matrix::operator==(const Matrix& other) const {
|
|
if (m_Lignes != other.m_Lignes || m_Colonnes != other.m_Colonnes)
|
|
return false;
|
|
|
|
for (std::size_t i = 0; i < m_Lignes; i++) {
|
|
for (std::size_t j = 0; j < m_Colonnes; j++) {
|
|
if (!IsEqualZero(at(i, j) - other.at(i, j)))
|
|
return false;
|
|
}
|
|
}
|
|
|
|
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];
|
|
}
|
|
|
|
long double& Matrix::at(std::size_t ligne, std::size_t colonne) {
|
|
return m_Data[ligne * m_Colonnes + colonne];
|
|
}
|
|
|
|
long double Matrix::at(std::size_t ligne, std::size_t colonne) const {
|
|
return m_Data[ligne * m_Colonnes + colonne];
|
|
}
|
|
|
|
std::size_t Matrix::GetRawCount() const {
|
|
return m_Lignes;
|
|
}
|
|
|
|
std::size_t Matrix::GetColumnCount() const {
|
|
return m_Colonnes;
|
|
}
|
|
|
|
Matrix Matrix::SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne, std::size_t ligne, std::size_t colonne) const {
|
|
assert(m_Lignes >= ligne && m_Colonnes >= colonne);
|
|
Matrix result{ligne, colonne};
|
|
|
|
for (std::size_t i = 0; i < ligne; i++) {
|
|
for (std::size_t j = 0; j < colonne; j++) {
|
|
result.at(i, j) = at(i + origine_ligne, j + origine_colonne);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
} |