begin process at 2010 02 10 11:09:43
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Maths & Algorithmes

 > MANIPULATION DE MATRICES

MANIPULATION DE MATRICES


 Information sur la source

 Description

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

Source

  • #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;
  • }
#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.


 Sources du même auteur

Source avec Zip Source avec une capture RENDU FIL DE FER D'UNE SCÈNE 3D AU FORMAT ASE
Source avec Zip TP ANALYSE DE DONNÉES
Source avec Zip CONNOCRYPT
Source avec Zip MANIPULATION DES LISTES LINÉAIRES

 Sources de la même categorie

Source avec Zip OPERATION SUR LES MATRICES CARREES AVEC CLASSE GENERIQUE par chouhad
Source avec une capture OPÉRATIONS SUR MATRICES C++ par Minilogus
[DEV-C++] CALCUL DE LA RACINE CARRÉE D'UN RÉEL par Jhep
PROGRAMME QUI CALCUL LE PPCM ET LE PGCD par AnoSantino
EVALUER UNE EXPRESSION MATHÉMATIQUE par begueradj

 Sources en rapport avec celle ci

[C/C++] DÉTERMINER LES DIVISEURS D'UN NOMBRE AVEC DES INFORM... par soso62fr
RÉSOLUTION AUTOMATIQUE DES TRINOMES DU SECOND DÉGRÉ. par MisterFelx
Source avec Zip MATRICE ZIGZAG par Mjj_Yuna
Source avec Zip PARTIE ALIQUOTE par jimonnet
Source avec Zip Source avec une capture CALCULER LA DÉTERMINANTE D'UNE MATRICE ALÉATOIRE DE GRANDEUR... par dPompei2

Commentaires et avis

Commentaire de Arnaud16022 le 09/08/2005 15:38:28

ouahlala ca se fait pas du tout ca.
la definition dansle header, l implementation dans le .cpp
bon OK si funto lit ca je vais me faire flamber mais il sait pk j ai exeptionnelement fait ca lol
en fait ca cree des pb si tu dois l inculre a partir de differents .cpp, et de toute facon c est pas propre
pour le reste, le contenu, j ai pas encore lu, suis en vacances mdr
++

Commentaire de vecchio56 le 09/08/2005 17:38:43 administrateur CS

Non arnaud, pour les templates on met aussi l'implémentation dans le .h, mais pour tout le reste on sépare en effet

Commentaire de steve_clamage le 09/08/2005 19:29:34

C'est buggé (beaucoup de fuites et code trop complexe), l'arrangement des données nuit aux performances.
Tu devrais oublier tout ca et utiliser boost::numeric::ublas::matrix.
http://www.boost.org/libs/numeric/ublas/doc/matrix.htm

Commentaire de Funto66 le 10/08/2005 02:24:01

Héhé Arnaud espèce de geek t'as réussi à trouver un PC en Autriche ;)

C'est effectivement un cas exceptionnel, les templates. La solution la plus élégante, dont avait parlée Kirua, serait de faire :

// MonFichier.h

// Déclarations...blablabla

#include "MonFichier.hpp"



// MonFichier.hpp

// Implémentation

C'est dû au fait qu'une classe template est recréée chaque fois que le paramètre template passé est différent, or pour recompiler la classe il est nécessaire d'avoir le code. Si l'on créait un .cpp à part ce ne serait pas possible ^^

Commentaire de Arnaud16022 le 12/08/2005 17:51:24

un PC , un PC, c est vite dit, je parlerais plutot d un öoulin a vent

"C'est dû au fait qu'une classe template est recréée chaque fois que le paramètre template passé est différent" -> hahaaa ca explique pas mal de trucs merci bcp.ca va bien m aider, ca, pour mon projet. n'empeche que je vois pas de .hpp dans ce code lol. et pouet :p

a ce propos, qqun sait comment ca se prononce, geek? mdr
bye
ad

Commentaire de Funto66 le 12/08/2005 20:15:10

Ton "nouveau projet", c'est cette histoire de langage script révolutionnaire dont il faut surtout pas parler en public pour pas révéler le futur de la programmation? :p

Pour la prononciation de "geek", moi jdirais "gik", certains penseront "djik" chais pas...

Commentaire de vecchio56 le 12/08/2005 20:32:41 administrateur CS

et Funto66 comment ca se prononce?

Commentaire de Funto66 le 12/08/2005 23:09:57

Ca se prononce à l'espagnole : "Foune To" et le 66 en français :)

Commentaire de PiX3L le 13/08/2005 18:25:37

Non ça se prononce "guik" ;) (Quasi sûr..)

Commentaire de Urgo le 14/08/2005 12:18:47

Dans un épisode de "Friends", Rachel prononce le mot "geek".. Faut pas dire "jeek" comme une "jeep", mais "guik".

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Problème pour dériver une classe [ par arc59 ] J'ai créé une classe Matrice comportant des fonctions get_ele, set_ele (toutes les 2 sont "virtual") et la redéfinition de l'opérateur +.Dans ma class fichier.h [ par bidules ] Bonjour,j'aimerais savoir s'il est possible de mettre des structures dans un fichier d'entete.Car j'ai fais l'essai mais lors de la compilation pour c PB de matrice [ par limax84 ] J'ai un probleme d'allocation dynamique de memoire pour une matrice.pour un tableau, je procede comme ceci:int * t;t = new int [30];mais pour une matr matrice carréé [ par justeroland ] j'ai besoin de l'aide au sujet de l'exercice suivant: une matrice carré est dite balancée si les sommes des elements de ses quatre triangles sont égal les fonctions et le Math [ par djamine ] beh bonsoir les programmeurs j'ai une problemeje veux en utilisant la formuletan x=2t/1-(t au carré) avec t=tg x/2et le fait que si x est petit par ex Besoin d'aide en C - Fonction [ par bugs2600 ] Voici mon programme quelqu'un pourrait-il m'aider je dois faire une fonction et je ne vois pas comment la faire le non de ma fonction doit etre PRODMA une matrice de taille quelconque [ par anaisa ] salut tt le monde saurez vous m'aidez à résoudre un petit probleme: je dois programmé la somme, produit de matrices de taille quelconque en langage C Coord 2D to 3D [ par bat67000 ] Comment optenir d'un point 2D sur l'app les coordonnees du point 3D associé avec la matrice de projection ?(je pige pas bien comment fonctionne la mat Matrice constante. [ par nsoualem ] j'ai crée une classe matrice avec un constructeurdu type:matrice(int nbligne,int nbcolonne)...elle marche a merveille!!!Lors de la création d'un code, inverse de matrice dynamique [ par anaisa ] Aidez nous please c pr programmer en langage Votre texte ICIC l inverse de la matrice dynamique merci bcp !!!!!!


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,983 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales