Compare commits
10 Commits
0d101a13d3
...
e360589bd9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e360589bd9 | ||
| 5043b67df1 | |||
| 3398e38b0b | |||
| 4b032ffaeb | |||
| 6136a8f88b | |||
| 6ea893f700 | |||
| e9321631c5 | |||
| 464c94ccf1 | |||
| deefbf9682 | |||
| 7aaf457586 |
3
.vscode/launch.json
vendored
3
.vscode/launch.json
vendored
@@ -9,8 +9,7 @@
|
||||
"request": "launch",
|
||||
"name": "Debug XMake target",
|
||||
"target": "Pivot",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"stopAtEntry": true
|
||||
"cwd": "${workspaceFolder}/matricies",
|
||||
}
|
||||
]
|
||||
}
|
||||
3
matricies/matrice2x2.mat
Normal file
3
matricies/matrice2x2.mat
Normal file
@@ -0,0 +1,3 @@
|
||||
2 2
|
||||
6 9
|
||||
9 6
|
||||
4
matricies/matrice3x3.mat
Normal file
4
matricies/matrice3x3.mat
Normal file
@@ -0,0 +1,4 @@
|
||||
3 3
|
||||
1 2 3
|
||||
4 5 6
|
||||
1 2 3
|
||||
4
matricies/matrice3x3echelonne.mat
Normal file
4
matricies/matrice3x3echelonne.mat
Normal file
@@ -0,0 +1,4 @@
|
||||
3 3
|
||||
1 0 -1
|
||||
0 1 2
|
||||
0 0 0
|
||||
5
matricies/matrice4x4.mat
Normal file
5
matricies/matrice4x4.mat
Normal file
@@ -0,0 +1,5 @@
|
||||
4 4
|
||||
1 2 3 4
|
||||
8 9 10 11
|
||||
5 6 7 8
|
||||
12 13 14 15
|
||||
5
matricies/matrice4x4echelonne.mat
Normal file
5
matricies/matrice4x4echelonne.mat
Normal file
@@ -0,0 +1,5 @@
|
||||
4 4
|
||||
1 0 0 -0.786594
|
||||
0 1 0 8.94049
|
||||
0 0 1 -2.0538
|
||||
0 0 0 0
|
||||
6
matricies/matrice5x5.mat
Normal file
6
matricies/matrice5x5.mat
Normal file
@@ -0,0 +1,6 @@
|
||||
5 5
|
||||
1 2 3 4 5
|
||||
16 17 18 19 20
|
||||
6 7 8 9 10
|
||||
11 12 13 14 15
|
||||
21 22 23 24 25
|
||||
256
src/Matrix.h
Normal file
256
src/Matrix.h
Normal file
@@ -0,0 +1,256 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
template <typename T>
|
||||
static bool EqualZero(T var) {
|
||||
return std::abs(var) < std::pow(10, -5);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class Matrix {
|
||||
private:
|
||||
std::size_t m_Lignes;
|
||||
std::size_t m_Colonnes;
|
||||
std::size_t m_Dimension;
|
||||
std::vector<T> m_Data;
|
||||
|
||||
public:
|
||||
Matrix(const std::string& fileNameInput) {
|
||||
Load(fileNameInput);
|
||||
}
|
||||
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(std::size_t lignes, std::size_t colonnes, std::initializer_list<T>&& initList) :
|
||||
m_Lignes(lignes), m_Colonnes(colonnes), m_Dimension(lignes * colonnes) {
|
||||
m_Data = initList;
|
||||
m_Data.resize(m_Dimension);
|
||||
}
|
||||
~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 << "[ ";
|
||||
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 PrintDebug() {
|
||||
#ifndef NDEBUG
|
||||
Print();
|
||||
std::cout << "\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
void 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 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 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 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// 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 (at(indice_ligne_maximum, j) != 0) {
|
||||
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));
|
||||
// matrice augmentee
|
||||
std::swap(aug.at(indice_ligne_maximum, k), aug.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])
|
||||
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;
|
||||
// matrice augmentee
|
||||
aug.at(i, k) = aug.at(i, k) * pivot - aug.at(r, k) * anulid;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return (aug);
|
||||
}
|
||||
|
||||
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;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// ligne de 0
|
||||
if (k == -1)
|
||||
break;
|
||||
// on divise la ligne par (i, k)
|
||||
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) {
|
||||
return at(indice);
|
||||
}
|
||||
|
||||
T& at(std::size_t ligne, std::size_t colonne) {
|
||||
return m_Data[ligne * m_Lignes + colonne];
|
||||
}
|
||||
|
||||
T at(std::size_t ligne, std::size_t colonne) const {
|
||||
return m_Data[ligne * m_Lignes + colonne];
|
||||
}
|
||||
};
|
||||
12
src/NR.h
Normal file
12
src/NR.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
class NR {
|
||||
private:
|
||||
int m_Numerator;
|
||||
int m_Denominator;
|
||||
|
||||
public:
|
||||
NR() : m_Numerator(0), m_Denominator(1) {}
|
||||
NR(int entier) : m_Numerator(entier), m_Denominator(1) {}
|
||||
NR(int numerator, int denominator) : m_Numerator(numerator), m_Denominator(denominator) {}
|
||||
};
|
||||
198
src/main.cpp
198
src/main.cpp
@@ -1,166 +1,44 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include "Matrix.h"
|
||||
|
||||
template <typename T>
|
||||
class Matrix
|
||||
{
|
||||
private:
|
||||
std::size_t m_Lignes;
|
||||
std::size_t m_Colonnes;
|
||||
std::size_t m_Dimension;
|
||||
std::vector<T> m_Data;
|
||||
|
||||
public:
|
||||
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() {}
|
||||
|
||||
void 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 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 DivideLine(T div, std::size_t line){
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
void GaussJordan()
|
||||
{
|
||||
int r = -1;
|
||||
for (std::size_t j = 0; j < m_Colonnes; ++j)
|
||||
{
|
||||
std::size_t k = 0;
|
||||
// Rechercher
|
||||
for (std::size_t i = r + 1; i < m_Lignes; ++i)
|
||||
{
|
||||
if (at(i, j) > at(k, j))
|
||||
{
|
||||
k = i;
|
||||
}
|
||||
}
|
||||
Matrix<float> mat_pivot(1, 1);
|
||||
mat_pivot.at(0, 0) = at(k, j);
|
||||
if (at(k, j) != 0)
|
||||
{
|
||||
++r;
|
||||
// Diviser
|
||||
for (std::size_t z = 0; z < m_Colonnes; ++z)
|
||||
{
|
||||
at(k, z) /= mat_pivot.at(0, 0);
|
||||
}
|
||||
if (k != r)
|
||||
{
|
||||
// Echanger
|
||||
for (std::size_t z = 0; z < m_Colonnes; ++z)
|
||||
{
|
||||
std::swap(at(k, z), at(r, z));
|
||||
}
|
||||
}
|
||||
for (std::size_t i = 0; i < m_Lignes; ++i)
|
||||
{
|
||||
if (i != r)
|
||||
{
|
||||
//Soustraire
|
||||
for (std::size_t z = 0; z < m_Colonnes; ++z)
|
||||
{
|
||||
at(i, z) -= at(r, z) * at(i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
T &operator[](std::size_t indice)
|
||||
{
|
||||
return at(indice);
|
||||
}
|
||||
|
||||
T &at(std::size_t ligne, std::size_t colonne)
|
||||
{
|
||||
return m_Data[ligne * m_Lignes + colonne];
|
||||
}
|
||||
|
||||
T at(std::size_t ligne, std::size_t colonne) const
|
||||
{
|
||||
return m_Data[ligne * m_Lignes + colonne];
|
||||
}
|
||||
|
||||
/*std::vector<T>::iterator begin() {
|
||||
return m_Data.begin();
|
||||
}
|
||||
|
||||
std::vector<T>::iterator end() {
|
||||
return m_Data.end();
|
||||
}*/
|
||||
};
|
||||
|
||||
/*
|
||||
void test()
|
||||
{
|
||||
Matrix<float> mat(2, 2);
|
||||
mat.at(0, 0) = 4;
|
||||
mat.at(0, 1) = 3;
|
||||
mat.at(1, 0) = 2;
|
||||
mat.at(1, 1) = 1;
|
||||
|
||||
mat.Print();
|
||||
mat.GaussJordan();
|
||||
mat.Print();
|
||||
void test() {
|
||||
Matrix<float> mat{"matrice5x5.mat"};
|
||||
mat.Print();
|
||||
// mat.Save("matrice3x3.mat");
|
||||
std::cout << "sdfdjiofoseifheoiefhoig\n";
|
||||
mat.Print();
|
||||
mat = {"matrice5x5.mat"};
|
||||
mat.GaussJordan(false, true);
|
||||
std::cout << "\nResultat :\n";
|
||||
mat.Print();
|
||||
mat.Transpose();
|
||||
mat.Print();
|
||||
// mat.Save("matrice4x4echelonne.mat");
|
||||
}
|
||||
*/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
//test();
|
||||
|
||||
std::cout << "hello world!" << std::endl;
|
||||
std::cout << "Quelle est le nombre de lignes de votre matrice ?" << std::endl;
|
||||
std::size_t lignes;
|
||||
std::cin >> lignes;
|
||||
std::cout << "Quelle est le nombre de colonnes de votre matrice ?" << std::endl;
|
||||
std::size_t colonnes;
|
||||
std::cin >> colonnes;
|
||||
std::size_t dimension = lignes * colonnes;
|
||||
std::cout << "Rentrez les coefficients de la matrice" << std::endl;
|
||||
Matrix<float> mat(lignes, colonnes);
|
||||
void prompt() {
|
||||
std::cout << "Quelle est le nombre de lignes de votre matrice ?" << std::endl;
|
||||
std::size_t lignes;
|
||||
std::cin >> lignes;
|
||||
std::cout << "Quelle est le nombre de colonnes de votre matrice ?" << std::endl;
|
||||
std::size_t colonnes;
|
||||
std::cin >> colonnes;
|
||||
std::size_t dimension = lignes * colonnes;
|
||||
std::cout << "Rentrez les coefficients de la matrice" << std::endl;
|
||||
Matrix<float> mat(lignes, colonnes);
|
||||
|
||||
mat.Insert();
|
||||
mat.Insert();
|
||||
|
||||
mat.Print();
|
||||
mat.GaussJordan();
|
||||
std::cout << std::endl;
|
||||
mat.Print();
|
||||
|
||||
return 0;
|
||||
mat.Print();
|
||||
Matrix<float> aug = mat.GaussJordan(true, true);
|
||||
std::cout << std::endl;
|
||||
std::cout << "Matrice echelonnee reduite" << std::endl;
|
||||
mat.Print();
|
||||
std::cout << "Matrice augmentee" << std::endl;
|
||||
aug.Print();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// test();
|
||||
prompt();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user