1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #include "math.h"
#include "matrix.h"
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Matrix mat;
mat.setSize(3,4);
mat(0,0)=1;
mat(0,1)=-1;
mat(0,2)=2;
mat(1,0)=0;
mat(1,1)=5;
mat(1,2)=-5;
mat(2,0)=0;
mat(2,1)=-1;
mat(2,2)=-6;
mat(0,3)=5;
mat(1,3)=-5;
mat(2,3)=-20;
mat.display();
//Début de l'algorithme du pivot de GAUSS
int r = -1;
int k = 0;
double maxPivot = 0;
for(int j=0; j<mat.columnCount()-1;j++)
{
maxPivot = 0; //On met "maxPivot" à une valeur minimale
//On cherche le coef max de la colonne j et on le stocke dans "maxPivot". L'indice de la ligne du pivot est k.
for(int i=r+1;i<mat.rowCount();i++)
{
if(abs(mat(i,j))>abs(maxPivot))
{
maxPivot = mat(i,j);
k = i;
}
}
if(mat(k,j)!=0)//Si le pivot est non nul, on procède aux opérations. Sinon, cela veut dire que toute la colonne est nulle
{
r++;
for(int i = 0; i<mat.columnCount();i++) //On divise la ligne par le pivot (1 en début de ligne)
{
mat(k,i) /= maxPivot;
}
for(int i = 0; i<mat.columnCount()-1;i++) //Echange des lignes r et k
{
double coef = mat(k,i);
mat(k,i) = mat(r,i); //On modifie la ligne k
mat(r,i) = coef; //On modifie la ligne r
}
for(int i=0; i<mat.rowCount();i++)
{
if(i!=r)
{
double coefLigne = mat(i,j);
for(int l=0;l<mat.columnCount();l++)
{
mat(i,l) -= mat(r,l)*coefLigne; // [Li] = [Li] - [Lr]*coefLigne
}
}
}
}
}
return a.exec();
} |
Partager