big internal rework
This commit is contained in:
@@ -1,20 +1,17 @@
|
||||
#include "Matrix.h"
|
||||
|
||||
#include "IO.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_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<long double>&& initList) :
|
||||
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);
|
||||
@@ -25,14 +22,14 @@ Matrix::~Matrix() {}
|
||||
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 {1, 1, {0}};
|
||||
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) {
|
||||
long double sum = 0;
|
||||
Element sum = 0;
|
||||
for (std::size_t k = 0; k < m_Columns; k++) {
|
||||
sum += at(i, k) * other.at(k, j);
|
||||
}
|
||||
@@ -42,44 +39,6 @@ Matrix Matrix::operator*(const Matrix& other) const {
|
||||
return result;
|
||||
}
|
||||
|
||||
void Matrix::Print() const {
|
||||
for (size_t i = 0; i < m_Raws; ++i) {
|
||||
std::cout << "[ ";
|
||||
for (size_t j = 0; j < m_Columns; ++j) {
|
||||
std::cout << at(i, j) << " ";
|
||||
}
|
||||
std::cout << "]";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void Matrix::Insert() {
|
||||
for (size_t i = 0; i < m_Raws; ++i) {
|
||||
for (size_t j = 0; j < m_Columns; ++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 << *this;
|
||||
}
|
||||
|
||||
void Matrix::Load(const std::string& filename) {
|
||||
std::ifstream in {filename};
|
||||
if (!in) {
|
||||
std::cerr << "Impossible de charger la matrice !\n";
|
||||
return;
|
||||
}
|
||||
in >> *this;
|
||||
}
|
||||
|
||||
void Matrix::Transpose() {
|
||||
Matrix result {m_Columns, m_Raws};
|
||||
for (std::size_t i = 0; i < m_Raws; i++) {
|
||||
@@ -133,15 +92,15 @@ bool Matrix::operator==(const Matrix& other) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
long double& Matrix::operator[](std::size_t indice) {
|
||||
Matrix::Element& Matrix::operator[](std::size_t indice) {
|
||||
return m_Data[indice];
|
||||
}
|
||||
|
||||
long double& Matrix::at(std::size_t ligne, std::size_t colonne) {
|
||||
Matrix::Element& Matrix::at(std::size_t ligne, std::size_t colonne) {
|
||||
return m_Data[ligne * m_Columns + colonne];
|
||||
}
|
||||
|
||||
long double Matrix::at(std::size_t ligne, std::size_t colonne) const {
|
||||
Matrix::Element Matrix::at(std::size_t ligne, std::size_t colonne) const {
|
||||
return m_Data[ligne * m_Columns + colonne];
|
||||
}
|
||||
|
||||
@@ -165,25 +124,3 @@ Matrix Matrix::SubMatrix(std::size_t origine_ligne, std::size_t origine_colonne,
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& stream, const Matrix& mat) {
|
||||
stream << mat.m_Raws << " " << mat.m_Columns << "\n";
|
||||
for (std::size_t i = 0; i < mat.m_Raws; i++) {
|
||||
for (std::size_t j = 0; j < mat.m_Columns; j++) {
|
||||
stream << mat.at(i, j) << " ";
|
||||
}
|
||||
stream << "\n";
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
|
||||
std::istream& operator>>(std::istream& stream, Matrix& mat) {
|
||||
stream >> mat.m_Raws >> mat.m_Columns;
|
||||
mat.m_Data.resize(mat.m_Raws * mat.m_Columns);
|
||||
for (std::size_t i = 0; i < mat.m_Raws; i++) {
|
||||
for (std::size_t j = 0; j < mat.m_Columns; j++) {
|
||||
stream >> mat.at(i, j);
|
||||
}
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user