166 lines
3.9 KiB
C++
166 lines
3.9 KiB
C++
#include "Matrix.h"
|
|
|
|
#include "IO.h"
|
|
#include <algorithm>
|
|
#include <cassert>
|
|
#include <cmath>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
Matrix::Matrix(std::size_t lignes, std::size_t colonnes) : m_Raws(lignes), m_Columns(colonnes) {
|
|
m_Data.resize(m_Raws * m_Columns);
|
|
}
|
|
|
|
Matrix::Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list<Element>&& initList) :
|
|
m_Raws(lignes), m_Columns(colonnes) {
|
|
m_Data = initList;
|
|
m_Data.resize(m_Raws * m_Columns);
|
|
}
|
|
|
|
Matrix Matrix::operator*(const Matrix& other) const {
|
|
if (m_Columns != other.m_Raws) {
|
|
std::cerr << "Mutiplication impossible car la dimensions des matrices est incompatible" << std::endl;
|
|
return {};
|
|
}
|
|
|
|
Matrix result(m_Raws, other.m_Columns);
|
|
|
|
for (std::size_t i = 0; i < m_Raws; ++i) {
|
|
for (std::size_t j = 0; j < other.m_Columns; ++j) {
|
|
Element sum = 0;
|
|
for (std::size_t k = 0; k < m_Columns; k++) {
|
|
sum += at(i, k) * other.at(k, j);
|
|
}
|
|
result.at(i, j) = sum;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void Matrix::Transpose() {
|
|
Matrix result {m_Columns, m_Raws};
|
|
for (std::size_t i = 0; i < m_Raws; i++) {
|
|
for (std::size_t j = 0; j < m_Columns; j++) {
|
|
result.at(j, i) = at(i, j);
|
|
}
|
|
}
|
|
*this = result;
|
|
}
|
|
|
|
Matrix Matrix::Identity(std::size_t taille) {
|
|
Matrix id {taille, taille};
|
|
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 id;
|
|
}
|
|
|
|
Matrix Matrix::ColumnVector(std::initializer_list<Element>&& initList) {
|
|
Matrix result {initList.size(), 1};
|
|
|
|
result.m_Data = initList;
|
|
|
|
return result;
|
|
}
|
|
|
|
Matrix Matrix::RawVector(std::initializer_list<Element>&& initList) {
|
|
Matrix result {1, initList.size()};
|
|
|
|
result.m_Data = initList;
|
|
|
|
return result;
|
|
}
|
|
|
|
void Matrix::Augment(const Matrix& droite) {
|
|
assert(droite.m_Raws == m_Raws);
|
|
Matrix temp {m_Raws, m_Columns + droite.m_Columns};
|
|
|
|
for (std::size_t i = 0; i < m_Raws; i++) {
|
|
for (std::size_t j = 0; j < m_Columns; j++) {
|
|
temp.at(i, j) = at(i, j);
|
|
}
|
|
}
|
|
|
|
for (std::size_t i = 0; i < m_Raws; i++) {
|
|
for (std::size_t j = 0; j < droite.m_Columns; j++) {
|
|
temp.at(i, j + m_Columns) = droite.at(i, j);
|
|
}
|
|
}
|
|
|
|
*this = temp;
|
|
}
|
|
|
|
Matrix Matrix::operator+(const Matrix& other) const {
|
|
assert(GetColumnCount() == other.GetColumnCount() && GetRawCount() == other.GetRawCount());
|
|
|
|
Matrix result = *this;
|
|
|
|
for (std::size_t i = 0; i < GetRawCount(); i++) {
|
|
for (std::size_t j = 0; j < GetColumnCount(); j++) {
|
|
result.at(i, j) += other.at(i, j);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
Matrix Matrix::operator-(const Matrix& other) const {
|
|
assert(GetColumnCount() == other.GetColumnCount() && GetRawCount() == other.GetRawCount());
|
|
|
|
Matrix result = *this;
|
|
|
|
for (std::size_t i = 0; i < GetRawCount(); i++) {
|
|
for (std::size_t j = 0; j < GetColumnCount(); j++) {
|
|
result.at(i, j) -= other.at(i, j);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool Matrix::operator==(const Matrix& other) const {
|
|
if (m_Raws != other.m_Raws || m_Columns != other.m_Columns)
|
|
return false;
|
|
|
|
for (std::size_t i = 0; i < m_Raws; i++) {
|
|
for (std::size_t j = 0; j < m_Columns; j++) {
|
|
if (!IsEqualZero(at(i, j) - other.at(i, j)))
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
Matrix::Element& Matrix::at(std::size_t ligne, std::size_t colonne) {
|
|
return m_Data[ligne * m_Columns + colonne];
|
|
}
|
|
|
|
Matrix::Element Matrix::at(std::size_t ligne, std::size_t colonne) const {
|
|
return m_Data[ligne * m_Columns + colonne];
|
|
}
|
|
|
|
std::size_t Matrix::GetRawCount() const {
|
|
return m_Raws;
|
|
}
|
|
|
|
std::size_t Matrix::GetColumnCount() const {
|
|
return m_Columns;
|
|
}
|
|
|
|
Matrix Matrix::SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne, std::size_t ligne, std::size_t colonne) const {
|
|
assert(m_Raws >= origine_ligne + ligne && m_Columns >= origine_colonne + 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;
|
|
}
|