This commit is contained in:
75
src/Gauss.cpp
Normal file
75
src/Gauss.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "Gauss.h"
|
||||
|
||||
#include "Matrix.h"
|
||||
|
||||
namespace Gauss {
|
||||
|
||||
static void GaussNonJordan(Matrix& mat, bool reduite) {
|
||||
int r = -1;
|
||||
for (std::size_t j = 0; j < mat.GetColumnCount(); j++) {
|
||||
std::size_t indice_ligne_maximum = r + 1;
|
||||
|
||||
// Recherche maximum
|
||||
for (std::size_t i = r + 1; i < mat.GetRawCount(); i++) {
|
||||
if (std::abs(mat.at(i, j)) > std::abs(mat.at(indice_ligne_maximum, j)))
|
||||
indice_ligne_maximum = i;
|
||||
}
|
||||
|
||||
// Si A[k,j]≠0 alors (A[k,j] désigne la valeur de la ligne k et de la colonne j)
|
||||
if (!IsEqualZero(mat.at(indice_ligne_maximum, j))) {
|
||||
r++;
|
||||
|
||||
// Si k≠r alors
|
||||
if (indice_ligne_maximum != r) {
|
||||
// Échanger les lignes k et r (On place la ligne du pivot en position r)
|
||||
for (std::size_t k = 0; k < mat.GetColumnCount(); k++) {
|
||||
std::swap(mat.at(indice_ligne_maximum, k), mat.at(r, k));
|
||||
}
|
||||
}
|
||||
|
||||
// Pour i de 1 jusqu'à n (On simplifie les autres lignes)
|
||||
for (std::size_t i = (reduite ? 0 : j); i < mat.GetRawCount(); 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 = mat.GetColumnCount() - 1; k >= 0; k--) {
|
||||
long double pivot = mat.at(r, j);
|
||||
long double anul = mat.at(i, j);
|
||||
mat.at(i, k) = mat.at(i, k) * pivot - mat.at(r, k) * anul;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void GaussJordan(Matrix& mat, bool reduite) {
|
||||
GaussNonJordan(mat, reduite);
|
||||
for (std::size_t i = 0; i < mat.GetRawCount(); i++) {
|
||||
int k = -1;
|
||||
for (std::size_t j = 0; j < mat.GetColumnCount(); j++) {
|
||||
if (!IsEqualZero(mat.at(i, j))) {
|
||||
k = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// ligne de 0
|
||||
if (k == -1)
|
||||
break;
|
||||
// on divise la ligne par (i, k)
|
||||
long double annul = mat.at(i, k);
|
||||
for (int j = 0; j < mat.GetColumnCount(); j++) {
|
||||
mat.at(i, j) /= annul;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GaussJordan(Matrix& mat, bool reduite, bool normalise) {
|
||||
if (normalise)
|
||||
GaussJordan(mat, reduite);
|
||||
else
|
||||
GaussNonJordan(mat, reduite);
|
||||
}
|
||||
|
||||
} // namespace Gauss
|
||||
9
src/Gauss.h
Normal file
9
src/Gauss.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
class Matrix;
|
||||
|
||||
namespace Gauss {
|
||||
|
||||
void GaussJordan(Matrix& mat, bool reduite, bool normalise);
|
||||
|
||||
} // namespace Gauss
|
||||
240
src/Matrix.cpp
240
src/Matrix.cpp
@@ -1,35 +1,39 @@
|
||||
#include "Matrix.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "Matrix.h"
|
||||
|
||||
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_Dimension(lignes * colonnes) {
|
||||
m_Data.resize(m_Dimension);
|
||||
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) :
|
||||
m_Lignes(lignes), m_Colonnes(colonnes), m_Dimension(lignes * colonnes) {
|
||||
m_Raws(lignes), m_Columns(colonnes) {
|
||||
m_Data = initList;
|
||||
m_Data.resize(m_Dimension);
|
||||
m_Data.resize(m_Raws * m_Columns);
|
||||
}
|
||||
|
||||
Matrix::~Matrix() {}
|
||||
|
||||
Matrix Matrix::operator*(const Matrix& other) const {
|
||||
if (m_Colonnes != other.m_Lignes) {
|
||||
if (m_Columns != other.m_Raws) {
|
||||
std::cerr << "Mutiplication impossible car la dimensions des matrices est incompatible" << std::endl;
|
||||
return {1, 1, {0}};
|
||||
}
|
||||
|
||||
Matrix result(m_Lignes, other.m_Colonnes);
|
||||
Matrix result(m_Raws, other.m_Columns);
|
||||
|
||||
for (std::size_t i = 0; i < m_Lignes; ++i) {
|
||||
for (std::size_t j = 0; j < other.m_Colonnes; ++j) {
|
||||
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;
|
||||
for (std::size_t k = 0; k < m_Colonnes; k++) {
|
||||
for (std::size_t k = 0; k < m_Columns; k++) {
|
||||
sum += at(i, k) * other.at(k, j);
|
||||
}
|
||||
result.at(i, j) = sum;
|
||||
@@ -38,15 +42,11 @@ Matrix Matrix::operator*(const Matrix& other) const {
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool IsEqualZero(long double var) {
|
||||
return std::abs(var) < std::pow(10, -5);
|
||||
}
|
||||
|
||||
void Matrix::Print() const {
|
||||
for (size_t i = 0; i < m_Lignes; ++i) {
|
||||
for (size_t i = 0; i < m_Raws; ++i) {
|
||||
std::cout << "[ ";
|
||||
for (size_t j = 0; j < m_Colonnes; ++j) {
|
||||
std::size_t indice = i * m_Lignes + j;
|
||||
for (size_t j = 0; j < m_Columns; ++j) {
|
||||
std::size_t indice = i * m_Raws + j;
|
||||
std::cout << at(i, j) << " ";
|
||||
}
|
||||
std::cout << "]";
|
||||
@@ -54,16 +54,9 @@ 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) {
|
||||
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;
|
||||
@@ -71,142 +64,127 @@ void Matrix::Insert() {
|
||||
}
|
||||
|
||||
void Matrix::Save(const std::string& fileName) {
|
||||
std::ofstream out{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";
|
||||
}
|
||||
out << *this;
|
||||
}
|
||||
|
||||
void Matrix::Load(const std::string& filename) {
|
||||
std::ifstream in{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);
|
||||
}
|
||||
}
|
||||
in >> *this;
|
||||
}
|
||||
|
||||
void Matrix::Transpose() {
|
||||
for (std::size_t i = 0; i < m_Lignes; i++) {
|
||||
for (std::size_t j = i; j < m_Colonnes; j++) {
|
||||
std::swap(at(i, j), at(j, i));
|
||||
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;
|
||||
}
|
||||
|
||||
void Matrix::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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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_Lignes + colonne];
|
||||
return m_Data[ligne * m_Columns + colonne];
|
||||
}
|
||||
|
||||
long double Matrix::at(std::size_t ligne, std::size_t colonne) const {
|
||||
return m_Data[ligne * m_Lignes + colonne];
|
||||
}
|
||||
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 >= ligne && m_Columns >= 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
45
src/Matrix.h
45
src/Matrix.h
@@ -1,48 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Matrix {
|
||||
private:
|
||||
std::size_t m_Lignes;
|
||||
std::size_t m_Colonnes;
|
||||
std::size_t m_Dimension;
|
||||
std::size_t m_Raws;
|
||||
std::size_t m_Columns;
|
||||
std::vector<long double> m_Data;
|
||||
|
||||
public:
|
||||
Matrix(const std::string& fileNameInput);
|
||||
Matrix(std::size_t lignes, std::size_t colonnes);
|
||||
Matrix(std::size_t lignes, std::size_t colonnes, std::initializer_list<long double>&& initList);
|
||||
Matrix(std::size_t raws, std::size_t columns);
|
||||
Matrix(std::size_t raws, std::size_t columns, std::initializer_list<long double>&& initList);
|
||||
~Matrix();
|
||||
|
||||
Matrix operator*(const Matrix& other) const;
|
||||
|
||||
void GaussNonJordan(bool reduite);
|
||||
|
||||
void GaussJordan(bool reduite);
|
||||
|
||||
void Print() const;
|
||||
|
||||
void PrintDebug();
|
||||
std::size_t GetRawCount() const;
|
||||
std::size_t GetColumnCount() const;
|
||||
|
||||
void Insert();
|
||||
void Print() const;
|
||||
|
||||
void Save(const std::string& fileName);
|
||||
|
||||
void Load(const std::string& filename);
|
||||
|
||||
void Transpose();
|
||||
|
||||
void Identity();
|
||||
static Matrix Identity(std::size_t size);
|
||||
|
||||
bool IsInversed() const;
|
||||
void Augment(const Matrix& right);
|
||||
|
||||
long double& operator[](std::size_t indice);
|
||||
Matrix SubMatrix(std::size_t raw_origin, std::size_t column_origin, std::size_t raw, std::size_t column) const;
|
||||
|
||||
long double& at(std::size_t ligne, std::size_t colonne);
|
||||
bool operator==(const Matrix& other) const;
|
||||
Matrix operator*(const Matrix& other) const;
|
||||
long double& operator[](std::size_t index);
|
||||
|
||||
long double at(std::size_t ligne, std::size_t colonne) const;
|
||||
long double& at(std::size_t raw, std::size_t column);
|
||||
long double at(std::size_t raw, std::size_t column) const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& stream, const Matrix& mat);
|
||||
friend std::istream& operator>>(std::istream& stream, Matrix& mat);
|
||||
};
|
||||
|
||||
static bool IsEqualZero(long double var);
|
||||
template <typename T>
|
||||
bool IsEqualZero(T var) {
|
||||
return std::abs(var) < std::pow(10, -5);
|
||||
}
|
||||
3
src/NR.h
3
src/NR.h
@@ -9,6 +9,7 @@ class NR {
|
||||
|
||||
public:
|
||||
NR() : m_Numerator(0), m_Denominator(1) {}
|
||||
|
||||
NR(int entier) : m_Numerator(entier), m_Denominator(1) {}
|
||||
NR(int numerator, int denominator); //check if denominator != 0
|
||||
void NRset(int numerator, int denominator); //same
|
||||
@@ -39,4 +40,4 @@ class NR {
|
||||
static void test();
|
||||
};
|
||||
|
||||
int PGCD(int x, int y);
|
||||
int PGCD(int x, int y);
|
||||
|
||||
45
src/Solver.cpp
Normal file
45
src/Solver.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "Solver.h"
|
||||
|
||||
#include "Gauss.h"
|
||||
|
||||
Solver::Solver(const Matrix& mat) : m_Matrix(mat) {}
|
||||
|
||||
Vect Solver::Image() const {
|
||||
Matrix result = m_Matrix;
|
||||
result.Transpose();
|
||||
Gauss::GaussJordan(result, true, true);
|
||||
result.Transpose();
|
||||
return {result};
|
||||
}
|
||||
|
||||
// https://en.wikipedia.org/wiki/Kernel_(linear_algebra)#Computation_by_Gaussian_elimination
|
||||
Vect Solver::Kernel() const {
|
||||
Matrix result = m_Matrix;
|
||||
result.Transpose();
|
||||
result.Augment(Matrix::Identity(result.GetRawCount()));
|
||||
Gauss::GaussJordan(result, true, true);
|
||||
result.Transpose();
|
||||
|
||||
// nombre de colonnes non nulles
|
||||
std::size_t origine_colonne = Vect(result.SubMatrix(0, 0, m_Matrix.GetRawCount(), m_Matrix.GetColumnCount())).GetCardinal();
|
||||
|
||||
return {result.SubMatrix(m_Matrix.GetRawCount(), origine_colonne, result.GetRawCount() - m_Matrix.GetRawCount(),
|
||||
result.GetColumnCount() - origine_colonne)};
|
||||
}
|
||||
|
||||
VectAffine Solver::TriangularSystem() const {
|
||||
Matrix mat = m_Matrix;
|
||||
Gauss::GaussJordan(mat, true, true);
|
||||
|
||||
Solver solver {mat.SubMatrix(0, 0, mat.GetRawCount(), mat.GetColumnCount() - 1)};
|
||||
|
||||
Vect noyau = solver.Kernel();
|
||||
Matrix origin = mat.SubMatrix(0, mat.GetColumnCount() - 1, mat.GetRawCount(), 1);
|
||||
|
||||
return {noyau, origin};
|
||||
}
|
||||
|
||||
std::size_t Solver::Rank() const {
|
||||
Vect image = Image();
|
||||
return image.GetCardinal();
|
||||
}
|
||||
20
src/Solver.h
Normal file
20
src/Solver.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "Vect.h"
|
||||
|
||||
class Solver {
|
||||
private:
|
||||
Matrix m_Matrix;
|
||||
|
||||
public:
|
||||
Solver(const Matrix& mat);
|
||||
|
||||
~Solver() {}
|
||||
|
||||
Vect Image() const;
|
||||
Vect Kernel() const;
|
||||
|
||||
VectAffine TriangularSystem() const;
|
||||
|
||||
std::size_t Rank() const;
|
||||
};
|
||||
90
src/Vect.cpp
Normal file
90
src/Vect.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#include "Vect.h"
|
||||
|
||||
#include "Gauss.h"
|
||||
#include "Solver.h"
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
Vect::Vect(const Matrix& mat) : m_Data(mat) {
|
||||
Simplify();
|
||||
}
|
||||
|
||||
void Vect::Simplify() {
|
||||
Matrix mat = m_Data;
|
||||
for (std::size_t j = 0; j < mat.GetColumnCount(); j++) {
|
||||
std::size_t i;
|
||||
for (i = 0; i < mat.GetRawCount(); i++) {
|
||||
if (!IsEqualZero(mat.at(i, j)))
|
||||
break;
|
||||
}
|
||||
if (i == mat.GetRawCount()) {
|
||||
m_Data = mat.SubMatrix(0, 0, mat.GetRawCount(), j);
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_Data = mat;
|
||||
}
|
||||
|
||||
std::size_t Vect::GetCardinal() const {
|
||||
return m_Data.GetColumnCount();
|
||||
}
|
||||
|
||||
bool Vect::operator==(const Vect& other) const {
|
||||
if (GetDimension() != other.GetDimension() || GetCardinal() != other.GetCardinal())
|
||||
return false;
|
||||
|
||||
// on vérifie si chaque vecteur de la deuxième base appartient à la première base
|
||||
for (std::size_t i = 0; i < GetCardinal(); i++) {
|
||||
Vect base = *this;
|
||||
base.AddVector(other.m_Data.SubMatrix(0, i, GetDimension(), 1));
|
||||
if (base.GetCardinal() != GetCardinal())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Vect::AddVector(const Matrix& mat) {
|
||||
m_Data.Augment(mat);
|
||||
m_Data.Transpose();
|
||||
Gauss::GaussJordan(m_Data, false, false);
|
||||
m_Data.Transpose();
|
||||
Simplify();
|
||||
}
|
||||
|
||||
bool Vect::operator!=(const Vect& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
Matrix Vect::GetLinearSystem() const {
|
||||
Matrix vect = m_Data;
|
||||
vect.Transpose();
|
||||
|
||||
Solver solver {vect};
|
||||
vect = solver.Kernel().m_Data;
|
||||
vect.Transpose();
|
||||
return vect;
|
||||
}
|
||||
|
||||
void Vect::Print() const {
|
||||
std::cout << "Espace vectoriel de dimension " << GetCardinal() << " de base :\n\n";
|
||||
for (std::size_t i = 0; i < m_Data.GetRawCount(); i++) {
|
||||
for (std::size_t j = 0; j < m_Data.GetColumnCount(); j++) {
|
||||
std::cout << "[ " << m_Data.at(i, j) << " ]\t";
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t Vect::GetDimension() const {
|
||||
return m_Data.GetRawCount();
|
||||
}
|
||||
|
||||
VectAffine::VectAffine(const Vect& base, const Matrix& origine) :
|
||||
m_Base(base), m_Origin(origine.SubMatrix(0, 0, m_Base.GetDimension(), 1)) {}
|
||||
|
||||
void VectAffine::Print() const {
|
||||
std::cout << "\tEspace Affine :\n\n";
|
||||
m_Base.Print();
|
||||
std::cout << "\nOrigine :\n\n";
|
||||
m_Origin.Print();
|
||||
}
|
||||
58
src/Vect.h
Normal file
58
src/Vect.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include "Matrix.h"
|
||||
|
||||
// espace vectoriel
|
||||
class Vect {
|
||||
private:
|
||||
Matrix m_Data;
|
||||
|
||||
public:
|
||||
/**
|
||||
* \brief Construit une base d'un espace vectoriel à partir des colonnes d'une matrice.
|
||||
* Ne prend pas en compte les colonnes de 0
|
||||
* \param mat Une matrice échelonnée.
|
||||
*/
|
||||
Vect(const Matrix& mat);
|
||||
|
||||
/**
|
||||
* \brief Affiche la base de l'espace vectoriel dans la console
|
||||
*/
|
||||
void Print() const;
|
||||
|
||||
std::size_t GetDimension() const;
|
||||
std::size_t GetCardinal() const;
|
||||
|
||||
Matrix GetLinearSystem() const;
|
||||
|
||||
/**
|
||||
* \brief Concatène la base actuelle avec un nouveau vecteur
|
||||
* \param mat Une matrice colonne de taille GetDimension()
|
||||
*/
|
||||
void AddVector(const Matrix& mat);
|
||||
|
||||
bool operator==(const Vect& other) const;
|
||||
bool operator!=(const Vect& other) const;
|
||||
|
||||
private:
|
||||
void Simplify();
|
||||
};
|
||||
|
||||
class VectAffine {
|
||||
private:
|
||||
Vect m_Base;
|
||||
Matrix m_Origin;
|
||||
|
||||
public:
|
||||
VectAffine(const Vect& base, const Matrix& origin);
|
||||
|
||||
void Print() const;
|
||||
|
||||
const Vect& GetBase() const {
|
||||
return m_Base;
|
||||
}
|
||||
|
||||
const Matrix& GetOrigin() const {
|
||||
return m_Origin;
|
||||
}
|
||||
};
|
||||
28
src/main.cpp
28
src/main.cpp
@@ -1,9 +1,11 @@
|
||||
#include "Matrix.h"
|
||||
#include "NR.h"
|
||||
#include "Gauss.h"
|
||||
#include "Solver.h"
|
||||
#include <iostream>
|
||||
|
||||
void test() {
|
||||
Matrix mat{"matrice4x4.mat"};
|
||||
/* Matrix mat{"matrice4x4.mat"};
|
||||
mat.Print();
|
||||
// mat.Save("matrice3x3.mat");
|
||||
std::cout << "sdfdjiofoseifheoiefhoig\n";
|
||||
@@ -15,7 +17,27 @@ void test() {
|
||||
mat.Transpose();
|
||||
std::cout << "<<\nTransposée:\n";
|
||||
mat.Print();
|
||||
// mat.Save("matrice4x4echelonne.mat");
|
||||
// mat.Save("matrice4x4echelonne.mat"); */
|
||||
|
||||
Matrix mat2 {"matrice4x4.mat"};
|
||||
mat2.Print();
|
||||
|
||||
Solver solver {mat2};
|
||||
|
||||
Vect image = solver.Image();
|
||||
Vect noyau = solver.Kernel();
|
||||
|
||||
std::cout << "\tImage :\n";
|
||||
image.Print();
|
||||
std::cout << "Système :\n";
|
||||
image.GetLinearSystem().Print();
|
||||
std::cout << "\tNoyau :\n";
|
||||
noyau.Print();
|
||||
std::cout << "Système :\n";
|
||||
noyau.GetLinearSystem().Print();
|
||||
|
||||
std::cout << "\n\n";
|
||||
solver.TriangularSystem().Print();
|
||||
}
|
||||
|
||||
void prompt() {
|
||||
@@ -33,7 +55,7 @@ void prompt() {
|
||||
|
||||
mat.Print();
|
||||
|
||||
mat.GaussJordan(true);
|
||||
Gauss::GaussJordan(mat, true, true);
|
||||
|
||||
mat.Print();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user