J'ai ecrit le programme classe matrice...
mais j'ai les error comme:
"matrice.cpp:269: error: no match for 'operator=' in 'b = matrice::Mat_decomposer(a, 0, c)'
matrice.cpp:7: note: candidates are: matrice& matrice::operator=(const matrice&)
"
Comment je peux
redresser une situation!!Merci beaucoup!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<iostream>
using namespace std;
class matrice {
private:
int m,n;
double **M;
public:
matrice(unsigned int,unsigned int);
int getcols() const; //nbre de colonneslignes;
int getlignes() const; //nbre de lignes;
void lire();
void ecrire();
void ecrire_inverse();
void setlignescols();
void Mat_transposer(matrice T);
void Mat_addition(matrice a1,matrice a2);
void Mat_produir(matrice a1,matrice a2);
void allouerMatrice(int l, int c);
void libererMatrice();
void Mat_identiter(int n);
void Mat_nulle(int n);
void Mat_multiplication_scalaire(matrice a, double k);
void Mat_decomposer(matrice a,int ib,int jb);
double Mat_determinant(matrice a);
void Mat_remplirMatrice(matrice a);
void Mat_coMat(matrice a);
void Mat_invMat(matrice a);
int Mat_moindreCarre(matrice a,matrice *u,matrice b);
};
matrice::matrice(unsigned int line=0,unsigned int col=0)
{
n=line;
m=col;
}
int matrice::getlignes()const
{
return n;
}
int matrice::getcols()const
{
return m;
}
void matrice::lire()
{/*scanf la matrice M*/
for(int i=0;i < getlignes();i++)
{
for(int j=0;j < getcols();j++)
{
printf("\n Matrice %dx%d :\n",i,j);
scanf(" %lf",&M[i][j]);
}
}
}
void matrice::ecrire()
{/*affiche la matrice M*/
printf("\n Matrice %dx%d :\n",getlignes(),getcols());
for(int i=0; i < getlignes();i++)
{
for(int j=0;j < getcols();j++)
{
printf("%f\t",M[i][j]);
}
printf("\n");
}
}
void matrice::ecrire_inverse()
{/*affiche la matrice d'inverse de M */
printf("\n Matrice d'inverse %dx%d :\n",getlignes(),getcols());
for (int j=0; j < getlignes(); j++)
{
for(int i=0; i< getcols(); i++)
{
printf("%f\t",M[i][j]);
}
printf("\n");
}
}
void matrice::allouerMatrice(int l, int c)
{/*allouer une matrice de l lignes et c colonnes*/
matrice a;
a.n=l; a.m=c;
a.M=(double**)malloc(l*sizeof(double*));
for(int i=0;i<l;i++)
{
a.M[i]=(double *)malloc(c*sizeof(double ));
}
}
void matrice::libererMatrice()
{/*liberer l'espace memoire occuper par la matrice*/
for(int i=0; i<getlignes() ; i++) free(M[i]);
}
void matrice::Mat_remplirMatrice(matrice a)
{/*remplie la matrice a par saisie au clavier*/
int i,j;
printf("\nRemplissage de matrice %dx%d\n",a.getlignes(),a.getcols());
for(i=0; i<a.getlignes(); i++)
{
for(j=0;j<a.getcols();j++)
{
printf("element (%d,%d)?",i,j);
scanf("%lf",&(a.M[i][j]));
}
}
}
void matrice::Mat_identiter(int n)
{/*renvoie la matrice identiter de dimension n x n */
matrice c(n,n);
c.allouerMatrice(n,n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i!=j) c.M[i][j]=0;
else c.M[i][j]=1;
}
}
c.libererMatrice();
}
void matrice::Mat_nulle(int n)
{/*renvoie une matrice nulle*/
matrice c(n,n);
c.allouerMatrice(n,n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
c.M[i][j]=0;
}
}
c.libererMatrice();
}
void matrice::Mat_multiplication_scalaire(matrice a, double k)
{/*renvoie le produit de a par le scalaire k*/
matrice c(a.getlignes(),a.getcols());
c.allouerMatrice(a.getlignes(),a.getcols());
for(int i=0;i<a.getlignes();i++)
{
for(int j=0;j<a.getcols();j++)
{
c.M[i][j]=k*a.M[i][j];
}
}
c.libererMatrice();
}
void matrice::Mat_addition(matrice a1,matrice a2)
{
matrice c(a1.getlignes(),a2.getcols());
c.allouerMatrice(a1.getlignes(),a2.getcols());
if(a1.getlignes() != a2.getcols())
{
printf("somme impossible car matrices incompatibles");
}
else
{
for( int i=0;i < getlignes();i++)
{
for(int j=0;j < getcols();j++)
c.M[i][j]= a1.M[i][j]+ a2.M[i][j];
}
}
c.libererMatrice();
}
void matrice::Mat_produir(matrice a1,matrice a2)
{
matrice a(a1.getlignes(),a2.getcols());
a.allouerMatrice(a1.getlignes(),a2.getcols());
if( a1.getlignes() != a2.getcols() )
{
printf("Matrices incompatibles,operation impossible");
}
else
{
for(int i=0;i < getlignes();i++)
{
for(int j=0;j < a1.getcols();j++)
{
a.M[i][j]=0;
for(int k=0;k < a2.getcols();k++)
{
a.M[i][j] += a1.M[i][k]*a2.M[k][j] ;
}
}
}
}
a.libererMatrice();
}
void matrice::Mat_transposer(matrice a)
{/*renvoie la transposer de la matrice a*/
matrice T(a.getlignes(),a.getcols());
T.allouerMatrice(a.getlignes(),a.getcols());
for(int i=0;i < a.getlignes();++i)
{
for(int j=0;j< a.getcols();++j)
{
T.M[i][j]=a.M[j][i];
}
}
T.libererMatrice();
}
void matrice::Mat_decomposer(matrice a,int linge,int colonne )
{/* Fonction qui enléve une ligne et une colonne pressise à une matrice,pour l'utiliser dans le calcule du déterminant*/
int x=0,y=0;
matrice b(a.getlignes()-1,a.getcols()-1);
b.allouerMatrice(a.getlignes()-1,a.getcols()-1);
/* parcourire chaque ligne */
for(int i=0;i<a.getlignes();i++)
if(i!=linge) /* si cet ligne n'est pas cel à supprimer: */
{
for(int j=0;j<a.getcols();j++)
if(j!=colonne) /*si ce n'est pas la colonne à supprimer */
{
b.M[x][y]=a.M[i][j];
y++;
if (y>a.getlignes()-2)
{
y=0;
x++;
}
}
}
b.libererMatrice();
}
double matrice::Mat_determinant(matrice a)
{/*renvoie le determinant de la matrice a*/
double det=0.0;
int signe=1;
int c;
matrice b(a.getlignes()-1,a.getcols()-1);
b.allouerMatrice(a.getlignes()-1,a.getcols()-1);
if (a.getlignes()==1)
{
return a.M[0][0];
}
for (c=0; c < a.getlignes();c++)
{
b=Mat_decomposer(a,0,c); det+=signe*a.M[0][c]*Mat_determinant(b);
signe*=(-1);
}
return det;
b.libererMatrice();
}
void matrice::Mat_coMat(matrice a)
{/* Fonction qui calcule la CoMatrice d'une matrice donné,pour l'utiliser dans le calcule de l'inverce */
matrice b(a.getlignes(),a.getcols());
matrice s(a.getlignes()-1,a.getcols()-1);
s.allouerMatrice(a.getlignes()-1,a.getcols()-1);
b.allouerMatrice(a.getlignes(),a.getcols());
for(int i=0;i<b.getlignes();i++)
{
for(int j=0;j<b.getcols();j++)
{
s=Mat_decomposer(a,i,j); b.M[i][j]=Mat_determinant(s);
}
s.libererMatrice();
}
}
void matrice::Mat_invMat(matrice a)
{/* Fonction qui calcule l'inverce d'une matrice donné, et retourne NULL si la matrice est non inversible ( det == 0 ) */
double det=Mat_determinant(a);
matrice coA, t_coA,inv_A;
if(det==0) printf("La matrice est non inversible");
/* calculer la comatrice de la matrice */
coA=Mat_coMat(a); /* calculer la comatrice transposer */
t_coA=Mat_transposer(coA); /* calculer la matrice inverse*/
inv_A=Mat_multiplication_scalaire(t_coA,1/det); coA.libererMatrice();
t_coA.libererMatrice();
inv_A.libererMatrice();
}
int matrice::Mat_moindreCarre(matrice a,matrice *u,matrice b)
{/*resoudre A.U=B par la méthode de moindre carré
renvoie 1 si le calcul est possible, 0 sinon*/
matrice At,AtA,AtA_inv,AtA_inv_At;
At=Mat_transposer(a); AtA=Mat_produir(At,a);
AtA_inv=Mat_invMat(AtA); if(AtA_inv==0)printf("La matrice est non inversible");
AtA_inv_At=Mat_produir(AtA_inv,At); *u=Mat_produir(AtA_inv_At,b);
At.libererMatrice();
AtA.libererMatrice();
AtA_inv.libererMatrice();
AtA_inv_At.libererMatrice();
return 1;
}
int main() {
matrice a(3,3),b(3,3),s(3,3),t(3,3),p(3,3);
char choix;
cout<<"entrer les elements de la premiere matrice :";cout<<endl;
a.lire();
a.ecrire();
cout<<"entrer les elements de la seconde matrice";cout<<endl;
b.lire();
b.ecrire();
cout<<"entrez votre choix : addition = 1 ,transposee = 2 multiplication = 3 : "; cin>>choix; cout <<endl;
if(choix=='1'){
cout<<"somme des deux matrices precedentes: "<<endl<<endl;
s.Mat_addition(a,b);
}
else
{
if(choix=='2'){
cout<<"la transposee de la premiÃ?re matrice est:"<<endl<<endl;
t.Mat_transposer(a);
}
else
{
if(choix=='3'){
cout<<"produit des matrices" <<endl<<endl;
p.Mat_produir(a,b);
}
else
{
cout<<"vous avez choisie une operation non definie :";
}
}
}
}