-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
162 lines (153 loc) · 3.68 KB
/
Matrix.java
File metadata and controls
162 lines (153 loc) · 3.68 KB
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
package TP02;
/**
* NS444 - TP Analyse Numerique
* Class Matrix
* permet de manipuler les matrices sous JAVA
* @author B BOUALEM
*
*/
public class Matrix {
private int col;// nombre de colonnes
private int lin;// nombre de lignes
public double[][] val;// les valeurs
public Matrix (int x, int y)// constructeur
{
this.lin = x;
this.col = y;
this.val= new double [this.lin][this.col];
}
public int getLin()
{
return this.lin;
}
public int getCol()
{
return this.col;
}
// methode java qui retourne a valeur de l'element i,j de la matrice
public double get(int i, int j)
{
return this.val[i][j];
}
// methode qui affecte a a l'element i,j de la matrice
public void set (int i, int j, double a)
{
this.val[i][j]=a;
}
// methode qui additionne l'instance de Matrix avec la matrice M
public Matrix add (Matrix M)
{
Matrix out = new Matrix (this.lin,this.col);
for (int i =0; i< this.lin;i++)
for (int j =0;j<this.col;j++)
out.val[i][j]=this.val[i][j]+M.val[i][j];
return out;
}
// methode qui calcul le resultat de la soustraction entre l'instance de Matrix et la matrice M
public Matrix sub (Matrix M)
{
Matrix out = new Matrix (this.lin,this.col);
for (int i =0; i< this.lin;i++)
for (int j =0;j<this.col;j++)
out.val[i][j]=this.val[i][j]-M.val[i][j];
return out;
}
// methode qui calcul la multiplication entre l'instance de Matrix et la matrice M
public Matrix mul (Matrix M)
{
Matrix out = new Matrix (this.lin,M.col);
for (int i =0; i< this.lin;i++)
for (int k=0;k<M.col;k++)
for (int j =0;j<this.col;j++)
{
out.val[i][k]+=this.val[i][j]*M.val[j][k];
}
return out;
}
// methode qui decompose l'instance de Matrix en matrice L et U
public void LU(Matrix L,Matrix U)
{
int n = this.lin;
L.set(0, 0, 1);
for (int j=0;j<n;j++)
{
U.set(0, j, this.get(0, j)/L.get(0, 0));
}
for (int i=1;i<n;i++)
{
L.set(i, 0, this.get(i,0)/U.get(0, 0));
}
for (int p = 0; p<n-1;p++)
{
L.set(p+1, p+1,1);
for (int j=p+1;j<n;j++)
{
double somme=0.0;
for (int k=0;k<p+1;k++)
somme+=L.get(p+1, k)*U.get(k, j);
U.set(p+1, j, (this.get(p+1, j)-somme)/L.get(p+1, p+1));
}
for (int i=p+2;i<n;i++)
{
double somme=0.0;
for (int k=0;k<p+1;k++)
somme+=L.get(i, k)*U.get(k, p+1);
L.set(i, p+1, (this.get(i,p+1)-somme)/U.get(p+1, p+1));
}
}
}
// methode qui resouds le probleme L z = B
public static Matrix descente (Matrix L, Matrix B)
{
int n = L.lin;
Matrix z = new Matrix (n,1);
double somme;
for (int i=0;i<n;i++)
{
somme=0.0;
for (int j=0;j<i;j++)
{
somme+= L.get(i, j)*z.get(j, 0);
}
z.set(i, 0, (B.get(i, 0)-somme)/L.get(i, i));
}
return z;
}
// methode qui resouds le probleme U x = z
public static Matrix montee (Matrix U, Matrix z)
{
int n = U.lin;
Matrix x = new Matrix (n,1);
double somme;
for (int i=n-1;i>-1;i--)
{
somme=0.0;
for (int j=n-1;j>i;j--)
{
somme+= U.get(i, j)*x.get(j, 0);
}
x.set(i, 0, (z.get(i, 0)-somme)/U.get(i, i));
}
return x;
}
// methode qui retourne la norme de la matrice
public double norm()
{
double out=0.0;
for (int i =0; i< this.lin;i++)
for (int j =0;j<this.col;j++)
out+=Math.pow(this.val[i][j],2);
return Math.sqrt(out);
}
// methode qui convertit la matrice en chaine de caractere (tres utilise en affichage )
public String toString()
{
String out="";
for (int i =0; i< this.lin;i++)
{for (int j =0;j<this.col;j++) {
out+=this.val[i][j]+" ";}
out+="\n";
}
return out;
}
}