-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrice.h
253 lines (239 loc) · 8.74 KB
/
matrice.h
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <iostream>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <vector>
#include "vecteur_double.h"
using namespace std;
class matrice
{
private:
int size1; // Dimension 1 de la matrice (nb de lignes)
int size2; // Dimension 2 de la matrice (nb de colonnes)
vector<double>* matrix; // Chaque ligne est un vector<double> de doubles
public:
matrice() {this->size1 = this->size2 = 0;}; // Constructeur par defaut
matrice(int,int); // Constructeur en donnant les 2 dimensions
matrice(const matrice&); // Constructeur par recopie
~matrice(); // Destructeur
int dim1() const {return this->size1;} // Retourne la 1ere dimension de la matrice
int dim2() const {return this->size2;} // Retourne la 2eme dimension de la matrice
vector<double>& operator [] (int) const; // Retourne une ligne complete
double& operator () (int,int) const; // Retourne un coefficient
matrice& operator = (const matrice&); // Surcharge de = (affectation)
matrice transpose(); // Transpose une matrice
vector<double> colonne(const int); // Une colonne de la matrice
matrice operator+(const matrice&); // Somme de 2 matrices
matrice operator-(const matrice&); // Difference de 2 matrices
matrice operator*(matrice&); // Produit de 2 matrices, pas const matrice& à cause de transpose()
matrice operator*(const double&); // Matrice x cte
vector<double> operator*(const vector<double>&); // Produit A*v
friend vector<double> operator*(const vector<double>&, matrice&); // Produit v^t * A
friend ostream& operator << (ostream&, const matrice &); // Surcharge << pour une matrice
friend istream& operator >> (istream&, const matrice &); // Surcharge >> pour une matrice
friend void save_matr(const char*, const matrice &); // Ecriture dans un fichier
friend matrice read_matr(const char *); // Lecture dans un fichier
};
//------------------------------------------------------
// Constructeur v1
matrice::matrice(int n, int m)
{
assert((n>0)&&(m>0));
this->size1 = n;
this->size2 = m;
this->matrix = new vector<double>[n];
vector<double> v(m,0);
for (int i=0; i<n ; i++)
this->matrix[i]=v;
}
//-----------------------------------------------------
// Constructeur par copie
matrice::matrice(const matrice & mat)
{
assert((mat.size1)&&(mat.size2));
this->size1=mat.size1;
this->size2=mat.size2;
this->matrix=new vector<double>[size1];
for (int i=0; i<mat.size1 ; i++)
this->matrix[i]=mat.matrix[i];
}
//-----------------------------------------------------
// Destructeur
matrice::~matrice()
{
if (this->size1||this->size2) delete[] this->matrix;
this->size1=0;
this->size2=0;
}
//-------------------------------------------------------
// Retourne la ligne i
vector<double>& matrice::operator [] (int i) const
{
assert((i>=0)&&(i<this->size1));
return this->matrix[i];
}
//------------------------------------------------------
// Retourne le coefficient (i,j)
double& matrice::operator () (int i, int j) const
{
assert((i>=0)&&(i<this->size1)&&(j>=0)&&(j<this->size2));
return this->matrix[i][j];
}
//-------------------------------------------------------
// Surcharge = (affectation)
matrice& matrice::operator = (const matrice& mat)
{
assert((mat.size1>0)&&(mat.size2>0));
if ((!this->size1)||(!this->size2))
{this->size1=mat.size1;
this->size2=mat.size2;
this->matrix=new vector<double>[size1];}
assert((this->size1==mat.size1)&&(this->size2==mat.size2));
for (int i=0; i<this->size1 ; i++)
this->matrix[i]=mat.matrix[i];
return (*this);
}
//-------------------------------------------------------
// Transposee d'une matrice
matrice matrice::transpose()
{
matrice T(this->size2,this->size1);
for (int i=0; i<this->size2; i++)
for (int j=0; j<this->size1; j++)
T(i,j) = matrix[j][i];
return T;
}
//-------------------------------------------------------
// Retourner la colonne j d'une matrice
vector<double> matrice::colonne (const int j)
{
assert((j>=0)&&(j<this->size2));
vector<double> vcol(this->size1);
for (int i=0; i<this->size1; i++)
vcol[i] = this->matrix[i][j];
return vcol;
}
//-------------------------------------------------------
// Surcharge operateur + non symetrique : somme de 2 matrices
matrice matrice::operator + (const matrice& A)
{
assert((this->size1>0)&&(this->size1==A.size1));
assert((this->size2>0)&&(this->size2==A.size2));
matrice C(this->size1,this->size2);
for (int i=0; i<this->size1; i++)
C[i] = this->matrix[i]+A[i];
return C;
}
//-------------------------------------------------------
// Surcharge operateur - non symetrique : difference de 2 matrices
matrice matrice::operator - (const matrice& A)
{
assert((this->size1>0)&&(this->size1==A.size1));
assert((this->size2>0)&&(this->size2==A.size2));
matrice C(this->size1,this->size2);
for (int i=0; i<this->size1; i++)
C[i] = this->matrix[i]-A[i];
return C;
}
//-------------------------------------------------------
// Surcharge operateur * non symetrique : produit de 2 matrices
matrice matrice::operator * (matrice& A) // pas const A à cause de la fonction transpose
{
assert((this->size1>0)&&(this->size2>0)&&(A.size1>0)&&(A.size2>0));
assert(this->size2==A.size1);
matrice TA=A.transpose();
matrice C(this->size1,A.size2);
for (int i=0; i<this->size1; i++)
for (int j=0; j<A.size2; j++)
C(i,j) = matrix[i]*TA[j];
return C;
}
//-------------------------------------------------------
// Surcharge operateur * non symetrique : matrice x cte
matrice matrice::operator * (const double& cte)
{
assert((this->size1>0)&&(this->size2>0));
matrice C(this->size1,this->size2);
for (int i=0; i<this->size1; i++)
C[i] = this->matrix[i]*cte;
return C;
}
//-------------------------------------------------------
// Surcharge operateur * non symetrique : produit A*v
vector<double> matrice::operator * (const vector<double>& v)
{
assert((this->size1>0)&&(this->size2>0)&&(this->size2==v.size()));
vector<double> w(this->size1);
for (int i=0; i<this->size1; i++)
w[i] = this->matrix[i]*v;
return w;
}
//-------------------------------------------------------
// Surcharge operateur * symetrique : produit v^t * A
vector<double> operator * (const vector<double>& v, matrice& A) // pas const A à cause de la fonction transpose
{
assert((A.size1>0)&&(A.size2>0)&&(A.size1==v.size()));
vector<double> w(A.size2);
matrice TA=A.transpose();
for (int i=0; i<A.size2; i++)
w[i] = v*TA[i];
return w;
}
//-------------------------------------------------------
// Surcharge operator << pour une matrice
ostream& operator << (ostream& s, const matrice & mat)
{
assert((mat.size1>0)&&(mat.size2>0));
for (int i=0; i<mat.size1; i++)
s << mat[i];
return s;
}
//-------------------------------------------------------
// Surcharge operator >> pour une matrice
istream& operator >> (istream& s, const matrice & mat)
{
assert((mat.size1>0)&&(mat.size2>0));
cout << "Entrer au clavier une matrice avec " << mat.size1 <<" lignes et " << mat.size2 << " colonnes \n";
for (int i=0; i<mat.size1; i++)
for (int j=0; j<mat.size2; j++)
s >> mat(i,j);
return s;
}
//---------------------------------------------
// Sauvegarde d'une matrice dans un fichier
void save_matr(const char *Nomfich, const matrice & mat)
{
ofstream fichier;
fichier.open(Nomfich); // ouverture du fichier
assert(mat.size1>0 && mat.size2>0);
fichier << "Dimension de la matrice = " << mat.dim1() << " " << mat.dim2() << "\n";
for (int i=0; i<mat.size1 ; i++)
fichier << mat[i];
fichier.close(); // fermature du fichier
}
//---------------------------------------------
// lecture d'une matrice dans un fichier
// on va obtenir une matrice de la bonne taille
matrice read_matr(const char *Nomfich)
{
ifstream fichier;
fichier.open(Nomfich); // ouverture du fichier
if (!fichier){
cerr << "Probleme d'ouverture de fichier " << endl;
exit(1);
}
int m,n;
if (!fichier.eof())
fichier >> m >> n;
matrice M(m,n);
for (int i=0;i<M.size1;i++){
for (int j=0;j<M.size2;j++){
fichier >> M(i,j);
if (fichier.eof())
cout << " Erreur dans le fichier, matrice incomplete \n ";
}
}
fichier.close(); // fermeture du fichier
return M;
}