Manipulation de matrices

Contenu du snippet

voila une petite biliothèque écrite en C++ et avec les templates pour créer des matrices et les manipuler

Source / Exemple :


#ifndef MATRIX_H
#define MATRIX_H
#define DTVECT vector<idtype*>
#include <vector>
#include<math.h>
using namespace std;
template <class idtype> class mat
{private:
DTVECT data;
int nbln;
int nbcol;
void verif(bool& flgb,mat<idtype>& b,int i,int j);
public:

//contructeurs et destructeur
mat(int x, int y);
mat();
mat(mat<idtype>* cp);
~mat();
//ajout de lignes et colonnes
void addln(idtype* tab);
//void addcol(idtype& tab);
mat<idtype>& trans(); //retourne la transposée
int delln(int id);//supprime la idème ligne
int delcol(int id);
mat<idtype>& mul(idtype& val);//retourne une matrice dont les valeurs sont
                             //celles de *this mutipliées par val
//produit matriciel
mat<idtype>& operator*(const mat<idtype>& b);
//surcharge d'opertateurs
idtype* operator[](int n);
mat<idtype>& operator+(const mat<idtype>& b);
mat<idtype>& operator-(const mat<idtype>& b);
void operator=(const mat<idtype>& cp);
bool operator==(const mat<idtype>& cp);
bool operator!=(const mat<idtype>& cp);
mat<idtype>& operator/(const mat<idtype>& b);
mat<idtype>& operator/(idtype val);
mat<idtype>& operator+(idtype val);
mat<idtype>& operator-(idtype val);
        int getnbln() const;
        idtype det();
        mat<idtype>& cof();
        mat<idtype>& inv();
        int getnbcol();
};
template<class idtype>
void mat<idtype>::verif(bool& flgb,mat<idtype>& b,int i,int j)
{
 flgb=(i>=b.nbln || i<0)? true : false;
    if(!flgb) flgb=(j>= b.nbcol || j<0)? true : false;
}
template <class idtype>
mat<idtype>::mat()
{
 nbln=nbcol=0;
}

template <class idtype>
mat<idtype>::mat(int x, int y)
{
 data.reserve(x);
 /*for(int i=0;i<x;i++)
 data[i]=new idtype[y];*/
 nbln=0;
 nbcol=y;
}

template <class idtype>
mat<idtype>::mat(mat<idtype>* cp)
{
 nbcol=cp->nbcol;
 nbln=0;
 /*for(int i=0;i<nbln;i++)
  delete[] data[i];*/ 
 //data.clear();
 data.reserve(cp->nbln);
 idtype* tab=new idtype[cp->nbcol];
 for(int i=0;i<cp->nbln;i++)
   {for(int j=0;j< cp->nbcol;j++)
    tab[j]=cp->data[i][j];
    addln(tab);
   }
delete[] tab;
}

template<class idtype>
mat<idtype>::~mat()
{
 for(DTVECT::iterator i=data.begin();i!=data.end();i++)
  delete[] *i;
}
template<class idtype>
void mat<idtype>::addln(idtype* tab)
{
 idtype* ptr= new idtype[nbcol];
 for(int i=0;i<nbcol;i++)
  ptr[i]=tab[i];
 data.push_back(ptr);
 nbln++;
}
template<class idtype>
mat<idtype>& mat<idtype>::trans()
{
 mat<idtype>* ptr=new mat<idtype>(nbcol,nbln);
 idtype* ptr2=new idtype[nbcol];
 for(int j=0;j<nbcol;j++)
 {int i=0;
  for(DTVECT::iterator it=data.begin();it!=data.end();it++)
   {
    ptr2[i]=(*it)[j];
    i++;
   }
  ptr->addln(ptr2);
 }delete[] ptr2;
 return *ptr;
}
template<class idtype>
int mat<idtype>::delln(int id)
{
 if(id<0 || id>=nbln) return 1;
 delete[] data[id];
 DTVECT::iterator it=data.begin();
 it+=id;
 data.erase(it);
 nbln--;
 return 0;
}
template<class idtype>
mat<idtype>& mat<idtype>::operator*(const mat<idtype>& b)
{if (this->nbcol==b.nbln)
  {
   mat<idtype>* ptr= new mat(this->nbln,b.nbcol);
   int j,k,somme;
   idtype* tab=new idtype[b.nbcol];
   for(int i=0;i<this->nbln;i++)
   {for(j=0;j<b.nbcol;j++)
     {
      somme=0;
      for(k=0;k<this->nbcol;k++)
       somme+=this->data[i][k] * b.data[k][j];
      tab[j]=somme;
     }
    ptr->addln(tab);
   }
   delete[] tab;
   return *ptr;
  }
 else return *this;
}
template<class idtype>
idtype* mat<idtype>::operator[](int n)
{return data[n];}
template<class idtype>
mat<idtype>& mat<idtype>::operator+(const mat<idtype>& b)
{
 int ln,col;
 ln=(this->nbln >b.nbln)? this->nbln : b.nbln;
 col=(this->nbcol >b.nbcol)? this->nbcol : b.nbcol;
 mat<idtype>* ptr=new mat(ln,col);
 idtype* tab=new idtype[col];
 int j;
 for(int i=0;i<ln;i++)
 {
   for(j=0;j<col;j++)
   {
    bool flga,flgb;
    flga=flgb=false;
    flga=(i>this->nbln || i<0)? true : false;
    if(!flga) flga=(j> this->nbcol || j<0)? true : false;
    flgb=(i>b.nbln || i<0)? true : false;
    if(!flgb) flgb=(j> b.nbcol || j<0)? true : false;
    if(!flga && !flgb) tab[j]=(*this).data[i][j]+b.data[i][j];
    else {
          if(!flgb) tab[j]=b.data[i][j];
          else{ if(!flga) tab[j]=this->data[i][j];
                else tab[j]=0;
              }
         }

   }
   ptr->addln(tab);
 }
   delete[] tab;
   return *ptr;
}
template<class idtype>
void mat<idtype>::operator=(const mat<idtype>& cp)
{
 if(this==cp) return this;
 else
  {
   nbcol=cp.nbcol;
 for(int i=0;i<nbln;i++)
  delete[] data[i];
 data.clear();
 nbln=0;
 data.reserve(cp.nbln);
 idtype* tab=new idtype[nbcol];
 for(int i=0;i< cp.nbln;i++)
  {for(int j=0;j<nbcol;j++)
    tab[j]=cp.data[i][j];
   addln(tab);
  }
 delete[] tab; 
  }
}
template<class idtype>
bool mat<idtype>::operator==(const mat<idtype>& cp)
{
 if(nbln==cp.nbln && nbcol==cp.nbcol)
  {
   int j;
   bool flg=true;
   for(int i=0;i<nbln;i++)
    for(j=0;j<nbcol;j++)
     flg=(data[i][j]!=cp.data[i][j])? false : true;
   return flg;
  }
 else return false;
}
template<class idtype>
bool mat<idtype>::operator!=(const mat<idtype>& cp)
{
 if(*this==cp) return false;
 else return true;
}
template<class idtype>
mat<idtype>& mat<idtype>::operator-(const mat<idtype>& b)
{
 int ln,col;
 ln=(nbln> b.nbln)? nbln : b.nbln;
 col=(nbcol> b.nbcol)? nbcol : b.nbcol;
 mat<idtype>* ptr=new mat(ln,col);
 idtype* tab=new idtype[col];
 for(int i=0;i<ln;i++)
  {for(int j=0;j<col;j++)
   {
    bool flga,flgb;
    flga=flgb=false;
    verif(flga,*this,i,j);
    verif(flgb,b,i,j);
    if (!flga && !flgb)
     tab[j]=(data[i][j]) - (b.data[i][j]);
    else {
          if(!flga) tab[j]=data[i][j];
          else {if(!flgb) tab[j]=-(b.data[i][j]);
                 else tab[j]=0;
               }
         }
   }
   ptr->addln(tab);
  }
   delete[] tab;
   return *ptr;
}
template <class idtype>
mat<idtype>& mat<idtype>::mul(idtype& val)
{
 mat<idtype>* ptr=new mat(nbln,nbcol);
 idtype* tab=new idtype[nbcol];
 for(int i=0;i<nbln;i++)
  {for(int j=0;j<nbcol;j++)
    tab[j]=data[i][j]*val;
   ptr->addln(tab);
  }
 delete[] tab;   
 return *ptr;
}
template <class idtype>
mat<idtype>& mat<idtype>::operator/(const mat<idtype>& b)
{
 mat<idtype>* ptr=new mat(b.nbln,b.nbcol);
 bool flg;
 idtype* tab=new idtype[b.nbcol];
 for(int i=0;i< b.nbln;i++)
  {for(int j=0;j< b.nbcol;j++)
   {
    flg=false;
    verif(flg,*this,i,j);
    if(flg || b.data[i][j]==0) tab[j]=0;
    else tab[j]=data[i][j] / b.data[i][j];
   }
   ptr->addln(tab);
  }
 delete[] tab;
 return *ptr;
}
template <class idtype>
mat<idtype>& mat<idtype>::operator/(idtype val)
{
 if(val)
  {
   mat<idtype>* ptr=new mat(nbln,nbcol);
   idtype* tab=new idtype[nbcol];
 for(int i=0;i<nbln;i++)
 {for(int j=0;j<nbcol;j++)
   tab[j]=data[i][j]/val;
  ptr->addln(tab);
 }
 delete[] tab;
 return *ptr;
  }
 else return *this;
}
template<class idtype>
mat<idtype>& mat<idtype>::operator+(idtype val)
{
 mat<idtype>* ptr=new mat(nbln,nbcol);
 idtype* tab=new idtype[nbcol];
 for(int i=0;i<nbln;i++)
  {for(int j=0;j<nbcol;j++)
    tab[j]=data[i][j]+val;
   ptr->addln(tab);
  }
 delete[] tab;  
 return *ptr;
}
template<class idtype>
mat<idtype>& mat<idtype>::operator-(idtype val)
{
 mat<idtype>* ptr=new mat(nbln,nbcol);
 idtype* tab=new idtype[nbcol];
 for(int i=0;i<nbln;i++)
  {for(int j=0;j<nbcol;j++)
    tab[j]=data[i][j]-val;
   ptr->addln(tab);
  }
 delete[] tab;   
 return *ptr;
}
#endif

template<class idtype>
int mat<idtype>::getnbln() const
{ return nbln;
        //TODO: Add your source code here
}
template<class idtype>
int mat<idtype>::delcol(int id)
{if(id<0 || id>=nbcol) return 1;
 for(int i=0;i<nbln;i++)
  {
   int k=0;
   idtype* tab=new idtype[nbcol-1];
   for(int j=0;j<nbcol;j++)
    {
     if(j !=id)
      {
       tab[k]=data[i][j];
       k++;
      }
    }
   delete[] data[i];
   data[i]=tab;
  }
 nbcol--;
 return 0;
}
template<class idtype>
idtype mat<idtype>::det()
{if(nbln==nbcol){
 if(nbln==2)
  return (data[0][0] * data[1][1])-(data[1][0] * data[0][1]);
 else
 {
  idtype somme=0;
  for(int i=0;i<nbcol;i++)
  {mat<idtype> tmp(this);
   tmp.delln(0);
   tmp.delcol(i);
   somme+=pow(-1,i) * data[0][i] * tmp.det();
  }
  return somme;
 }}
 else return 0;
}
template<class idtype>
mat<idtype>& mat<idtype>::cof()
{
 if(nbln==nbcol)
 {
  mat<idtype>* ptr=new mat<idtype>(nbln,nbcol);
  idtype* tab=new idtype[nbcol];
  for(int i=0;i<nbln;i++)
  {
   for(int j=0;j<nbcol;j++)
    {
     mat<idtype> tmp(this);
     tmp.delln(i);
     tmp.delcol(j);
     tab[j]=(pow(-1,i+j)* tmp.det());
    }
   ptr->addln(tab);
  }
  delete[] tab;
  return *ptr;
 }
 else return *this;
}
template<class idtype>
mat<idtype>& mat<idtype>::inv()
{
 if(nbln==nbcol)
 {
  mat<idtype>* ptr=&((*this).trans().cof().mul(1/this->det()));
  return *ptr;
 }
 else return *this;
}
template<class idtype>
int mat<idtype>::getnbcol()
{
 return nbcol;
}

Conclusion :


On a là un fichier d'entête (.h) faut juste mettre le code dans un fichier .h etl'inclure dans le code de votre programme.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.