Coloration Syntaxique

Télécharger le script ! - mod phpBB

/****************************************************
* matrice.cpp: implémentation de la classe matrice. *
****************************************************/

#include "matrice.h"
#include <iostream.h>
#include <assert.h>
#include <math.h>
#include <conio.h>
#include <process.h>

// Constructeur.
matrice::matrice(int l,int c)
{
   this->lig = l;
   this->col = c;
   this->_adrm = new double [l*c];
   for(int i=0;i<l;i++)
      for(int j=0;j<c;j++)
         (*this)(i,j) = 0;
}

// Constructeur de recopie.
matrice::matrice(matrice & mat)
{
   this->lig = mat.lig;
    this->col = mat.col;
    this->_adrm = new double [lig*col];
   for(int i=0;i<lig;i++)
      for(int j=0;j<col;j++)
         (*this)(i,j) = mat(i,j);
}

// Destructeur.
matrice::~matrice()
{
   delete [] _adrm;
}

// Affichage de la matrice.
matrice::afficher()
{
    for (int i=0; i<lig;i++)
    {
        for (int j=0;j<col; j++)
        cout<<" "<<(*this)(i,j)<<" ";
        cout<<endl;
    }
}

// Operator ().
double & matrice::operator ()(int i, int j)
{
    assert((i>=0) && (i<lig) && (j>=0) && (j<col));
    return _adrm[(i*col)+j];
}

// Remplir la matrice.
matrice::Remplirmat()
{
    for(int i=0;i<this->lig;i++)
        for(int j=0;j<this->col;j++)
        {
            cout<<" donner l'element :"<<i+1<<j+1<<" = ";
            cin>>(*this)(i,j);
        }
}

// Initialiser la matrice.
matrice::inialiser()
{
    for(int i=0;i<lig;i++)
        for(int j=0;j<col;j++)
            (*this)(i,j) = 0;
}

// Transposer la matrice.
matrice& matrice::Transposer()
{
    matrice tmp(lig,col);
    
    // Initialisation de la matrice tmp.
    for(int i=0;i<lig;i++)
        for(int j=0;j<col;j++)
            tmp(i,j) = (*this)(i,j);

     // La tronsposition.
    for(i=0;i<lig;i++)    
        for(int j=0;j<col;j++)  
            (*this)(i,j) = tmp(j,i);

    return (*this);      
}

// Produit de matrices.
matrice matrice::Produit(matrice & mat)
{
    assert((col==mat.lig));
    matrice prod(col,mat.lig);

    for(int i=0; i<lig; i++)
        for (int j=0;j<mat.col;j++)
            for(int k=0;k<col;k++)
                prod(i,j)=prod(i,j)+ ((*this)(i,k) * mat(k,j));

    return (prod);
}

// Copie de colonnes d'une matrice.
matrice::Copiecol(vecteur& cop,int numcol)
{
    assert(numcol<=col);  
    for(int i=0;i<lig;i++)
    {
        cop[i] = (*this)(i,numcol-1);
    }
}

// Passage d'un vecteur à une matrice.
matrice::vecttomat(vecteur & v, int numcol)
{

    assert(numcol<=col);  
    for(int i=0;i<lig;i++)
    {
        (*this)(i,numcol-1)= v[i];
    }
}

// Operator =.
matrice& matrice::operator =(matrice & mat)
{
    for(int i=0;i<lig;i++)    
     for(int j=0;j<col;j++)  
     {(*this)(i,j)= mat(i,j);}
          
    return (*this);
}

// Méthode Cholesky.
matrice matrice::Cholevsky()
{
    cout<<"************************************************************************"<<endl;
    cout<<"*                        La Methode de Cholesky                        *"<<endl;    
    cout<<"************************************************************************"<<endl;

    matrice L(this->lig,this->col);
    
    if ((*this)(0,0)>0)
    {  // Décomposition.

        L(0,0) = pow((*this)(0,0),0.5);

        for(int i=1;i<lig;i++) // Initialiser la 1ère colonne de L.
        {
            L(i,0) = (*this)(i,0)/ L(0,0);
        }
              
        for(int j=1;j<col;j++) // Intialiser les autre colonnes.
        {
            L(j,j) = (*this)(j,j);
            for(int k=0;k<j;k++)
            {
                L(j,j) = L(j,j) - pow(L(j,k),2);
            }
        
          if (L(j,j) > 0) // Le 1er element de la colonne.
           {  
                L(j,j) = pow(L(j,j),0.5); // Affecter à Ljj = racine(s) diagonale.
                for(i=j+1;i<lig;i++)  // Les lignes de la colonne j.
                {
                    L(i,j) = (*this)(i,j);
                    for(k=0;k<j;k++)
                     {
                        L(i,j) = L(i,j) - (L(i,k)*L(j,k));
                    }
                    L(i,j) = L(i,j) / L(j,j); // Calcule de Lij.
                }
            }
            else
            {
                cout<<"! system impossible le L"<<j+1<<j+1<<" est < 0 !"<<endl;
                getch();
                exit(1);
            }
        }  
    }
     else
    {
        cout<<" Systeme impossible a decomposer ";
    }
    return(L); // Sortie de la fonction.
}

//
matrice::TriongInf(vecteur& x,vecteur& b)
{
    // Vérifier la matrice.
    int i = 0;
    while (i < lig)
    {
        if((*this)(i,i) == 0)
        {
            cout<<" systeme impossible detA = 0 "<<endl;
            exit(1);
        }
        else
            i = i + 1;
    }

    // Matrice inferieure.
    x[0] = b[0] / (*this)(0,0); // x[1]= b[1]/a[1,1]  
  
    double s;
  
    for(i=1;i<lig;i++)
    {
        s = b[i];
        for(int j=0;j<i;j++)
        {
            s = s - ( (*this)(i,j) * x[j]);
        }
        x[i] = (s / (*this)(i,i));
    }
}

//
matrice::TriongSup(vecteur& x, vecteur& b)
{
    // Vérifier la matrice.
    int i=0;
    while (i < lig)
    {
        if( (*this)(i,i) == 0)
        {
            cout<<" system impossible detA = 0 "<<endl;
            exit(1);
        }
        else
            i = i + 1;
    }

    // Matrice superieure.
    x[lig-1] = b[lig-1] / (*this)(lig-1,lig-1); // x[n]= b[n]/a[n,n]  
  
   double s;

    for(i=lig-2;i>=0;i=i-1)
    {
        s = b[i];
        for(int j=i+1;j<col;j++)
        {
            s = s - ( (*this)(i,j) * x[j]);
        }
        x[i] = (s / (*this)(i,i));
    }
}

// Méthode de Gauss.
matrice matrice::Gauss(vecteur & b)
{
    cout<<"************************************************************************"<<endl;
    cout<<"*                         La Methode de GAUSS                          *"<<endl;
    cout<<"************************************************************************"<<endl;

    matrice x(this->lig,this->col+1);
    matrice y(this->lig,this->col+1);
    matrice sol(this->lig,this->col);
    double piv;
    int i,j,k;

    for(i=0;i<this->lig;i++)
        for(j=0;j<this->col;j++)
        {
            x(i,j) = (*this)(i,j);
        }
    for(i=0;i<this->lig;i++)
    {
        x(i,(x.col-1)) = b[i];
    }

    // Traitement de Gauss.
    for(k=1;k<this->lig;k++)
   { //k
        y.inialiser();
        for(i=0;i<k;i++)
            for(j=0;j<y.col;j++)
            {
                y(i,j) = x(i,j);
            }

        for(i=k;i<this->lig;i++)
        {
            piv = x(i,k-1)/x(k-1,k-1);
            for(j=k;j<x.col;j++)
            {
                y(i,j) = x(i,j)-(piv * x(k-1,j));
            }
        }

        // Affectatipn de Xij a Yij
        for(i=0;i<x.lig;i++)
            for(j=0;j<y.col;j++)
            {
                x(i,j) = y(i,j);
            }
    } //k  

    // Transformation de la matrice de calcule Yij
    for(i=0;i<this->lig;i++)
        for(j=0;j<this->col;j++)
        {
            sol(i,j) = y(i,j);
        }
    for(i=0;i<this->lig;i++)
   {
        b[i] = x(i,(x.col-1));
    }

    // cout<<" la matrice de y "<<endl;  
    // y.afficher();

    return sol;
}

80556 octets
326 lignes générées en 0.02837 secondes,
soit une vitesse de 11455 lignes par seconde

Apache/2.4.38 (Debian)

Texte à analyser:

Langage :