-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquareMatrix.cs
218 lines (194 loc) · 7.11 KB
/
SquareMatrix.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CalMat
{
[Serializable]
class SquareMatrix : Matrix //defini une classe SquareMatrix qui hérite de la classe Matrix
{
public SquareMatrix(int c, string nom) : base(c,c,nom) //constructeur de la classe SquareMatrix pour les matrices carrées "principal"
{
this.Elements = new double[c, c];
this.Lines = c;
this.Columns = c;
this.name = nom; //pas besoin d'ajouter dans le dico car appele la base
}
/*protected int SubMatrix(SquareMatrix a, int x)
{
if (a.Columns == 2)
{
return a.Elements[0, 0] * a.Elements[1, 1] - a.Elements[0, 1] * a.Elements[1, 0];
}
else
{
SquareMatrix b = new SquareMatrix(a.Lines - 1);
int f = 0, g = 0, interDet = 0;
for (int i = 1; i < a.Lines; i++)
{
g = 0;
for(int j = 0; j < a.Columns; j++)
{
if (j == x)
{
continue;
}
else
{
b.Elements[f, g] = a.Elements[i, j];
g++;
}
}
f ++;
}
Console.WriteLine("\n");
for(int i = 0;i < a.Lines; i++)
{
interDet += b.SubMatrix(b,i) * (int) Math.Pow(-1,i)* a.Elements[0,i];
Console.WriteLine("det:" + interDet);
//Console.WriteLine("\n");
}
return interDet;
}
}
public int Det()
{
if (this.Lines == 1)
{
return this.Elements[0, 0];
}
else
{
int det = 0;
for ( int j = 0; j < this.Columns; j++)
{
det += (int) Math.Pow(-1, j) * this.Elements[0, j] * this.SubMatrix(this, j);
}
Console.WriteLine(det);
return det;
}
}
*/ //tentative de faire un algoritme récursif pour le determinant
/*public int Det ()
{
if (this.Lines == 1)
{
return this.Elements[0, 0];
}
else if (this.Columns == 2)
{
return this.Elements[0, 0] * this.Elements[1, 1] - this.Elements[0, 1] * this.Elements[1, 0];
}
else
{
SquareMatrix subMatrix = new SquareMatrix(this.Lines - 1);
int f, g, det = 0;
for(int x = 0 ; x < this .Columns; x++)
{
f = 0;
for (int i = 1; i < this.Lines; i++)
{
g = 0;
for(int j = 0; j < this.Columns; j++)
{
if (j != x)
{
subMatrix.Elements[f, g] = this.Elements[i, j];
g++;
}
}
f ++;
}
Console.WriteLine("\n");
Console.Write(subMatrix);
for(int i = 0;i < this.Lines; i++)
{
det += subMatrix.Det() * (int) Math.Pow(-1,i)* this.Elements[0,i];
}
}
Console.WriteLine("det");
return det;
}
}*/ //tentative de faire un algoritme récursif pour le determinant
public Object Det() //methode de calcul pour calculer le déterminant (principe: triangulation de la matrice par pivot de gausse)
{
//création des variables
int Max;
double k, det = 1;
SquareMatrix c = this;
double[] Array = new double[this.Columns];
//on cherche le maximum de la colonne j et en dessous de la jiem ligne incluse
for (int j = 0; j < this.Columns; j++)
{
Max = j;
for (int i = j; i < this.Lines - 1; i++)
{
if (this.Elements[j, i] > this.Elements[Max, i])
{
Max = j;
}
}
// on interverti la ligne j et la ligne contenant le maximum trouvé
for (int x = 0; x < this.Columns; x++)
{
Array [x] = c.Elements[x,j];
}
for (int x = 0; x < this.Columns; x++)
{
c.Elements[x, j] = c.Elements[x, Max];
}
for (int x = 0; x < this.Columns; x++)
{
c.Elements[x, j] = Array[x];
}
// on trouve le coeficient pour mettre les termes en dessous du terme [j,j] à 0
for (int i = j+1; i <c.Lines; i++ )
{
k = c.Elements[j,i] / c.Elements[j,j];
for (int x = 0; x < c.Columns; x++)
{
c.Elements[x, i] -= k * c.Elements[x, j];
}
}
}
// calcul du determinant: le produit des termes diagonaux
for (int x = 0; x < c.Lines; x++ )
{
det *= c.Elements[x, x];
}
return det; //retourne le déterminant trouvé
}
public Object Trace() //methode de calcul pour calculer la trace d'une matrice carrée
{
double Trac = 0;
//on somme les termes diagonaux de la matrice
for (int i = 0; i < this.Lines; i++)
{
Trac += this.Elements[i, i];
}
return Trac ; //retourne la trace trouvée
}
public Object Norme() //une methode de calcul parmis d'autre pour trouver la norme (norme utilisée : Norme infinie)
{
double sum = 0;
double max = 0;
SquareMatrix N = this;
// on somme la valeur absolue tout les termes de la colonne
for (int j = 0 ; j < this.Columns; j++)
{
sum = 0;
for (int i = 0 ; i < this .Lines; i++)
{
sum += Math.Abs( N.Elements[j,i]);
}
// on prend le maximum
if ( max < sum)
{
max = sum;
}
}
return max; //on retourne le maximum qui est la norme infini
}
}
}