-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrice.c
252 lines (214 loc) · 5.54 KB
/
Matrice.c
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
#include "automate.h"
#include "table.h"
#include "ensemble.h"
#include "outils.h"
#include "Matrice.h"
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
struct _Matrice {
int** tab;
tree mot;
int taille;
};
/*************************************************************************/
/************************* Creation, Destruction *************************/
/*************************************************************************/
Matrice creer_matrice(int taille){
Matrice m = malloc(sizeof(*m));
m->tab = malloc(taille * sizeof(int*));
m->taille = taille;
int i;
for(i = 0; i<taille; i++)
m->tab[i] = malloc(taille * sizeof(int));
return m;
}
void detruire_matrice(Matrice m){
if(m == NULL)
return;
else{
int i;
for(i = 0; i<m->taille; i++){
free(m->tab[i]);
}
free(m->tab);
tree_destroy(m->mot);
free(m);
}
}
/*************************************************************************/
/****************************** Accesseurs *******************************/
/*************************************************************************/
int get_taille(Matrice m){
return m->taille;
}
tree get_mot(Matrice m){
return m->mot;
}
int ** get_tableau(Matrice m){
return m->tab;
}
/*************************************************************************/
/******************************* Affichage *******************************/
/*************************************************************************/
void print_matrice_in_R(Matrice m){
printf("M(");
print_tree(m->mot);
printf(")");
printf("\n\t");
int i, j;
for(i = 0; i<m->taille; i++){
printf("|");
for(j = 0; j<m->taille; j++){
switch(m->tab[i][j])
{
case OMEGA : printf(" %s ", "ω"); break;
case INFINI : printf(" %s ", "∞"); break;
default: printf(" %d ", m->tab[i][j]); break;
}
}
printf("|\n\t");
}
printf("\n");
}
/*************************************************************************/
/************************ Fonctions utilitaires **************************/
/*************************************************************************/
int equal_size (Matrice m1,Matrice m2){
return m1->taille==m2->taille;
}
int equal_matrice (Matrice m1, Matrice m2){
if (!equal_size(m1, m2))
return FALSE;
int i, j;
for(i = 0; i < m1->taille; i++){
for(j = 0; j < m1->taille; j++){
if (m1->tab[i][j] != m2->tab[i][j])
return FALSE;
}
}
return TRUE;
}
int min (int n1,int n2){
if(n1<n2)
return n1;
return n2;
}
int max (int n1,int n2){
if(n1>n2)
return n1;
return n2;
}
/*************************************************************************/
/************************* Fonctions principales *************************/
/*************************************************************************/
Matrice creer_matrice_transistions(Automate* a, char c){
Automate * abis = creer_automate_etats_0_n(a);
int n = taille_ensemble(get_etats(abis));
Matrice m = creer_matrice(n);
tree t=tree_creat();
tree_set(t, c);
m->mot = t;
int i, j;
for(i = 0; i < m->taille; i++){
for(j = 0; j< m->taille; j++){
m->tab[i][j] = INFINI;
}
}
Table_iterateur it1;
Ensemble_iterateur it2;
for ( it1 = premier_iterateur_table(get_transitions(abis));
! iterateur_ensemble_est_vide(it1);
it1 = iterateur_suivant_ensemble(it1)
){
Cle * cle = (Cle*) get_cle(it1);
Ensemble * fins = (Ensemble*) get_valeur(it1);
int tmp = get_lettre_cle(cle);
if(tmp == (int)c){
for( it2 = premier_iterateur_ensemble(fins);
! iterateur_ensemble_est_vide(it2);
it2 = iterateur_suivant_ensemble(it2)
){
int f = get_element(it2);
int cout = get_cout_cle(cle)==0 ? 0: 1;
m->tab[get_origine_cle(cle)][f] = cout;
}
}
}
liberer_automate(abis);
return m;
}
Matrice multiplication_in_MnR(Matrice m1,Matrice m2){
if(!equal_size(m1,m2)){
printf("sont pas de meme taille");
return NULL;
}
Matrice m3=creer_matrice(m1->taille);
m3->mot = tree_concatenation(m1->mot,m2->mot);
int i,j,k;
for(i=0;i<m1->taille;i++)
for(j=0;j<m1->taille;j++){
m3->tab[i][j]=-1;
}
for(i=0;i<m1->taille;i++)
for(j=0;j<m1->taille;j++)
for(k=0;k<m1->taille;k++){
if(m3->tab[i][j] == -1)
m3->tab[i][j]= max(m1->tab[i][k],m2->tab[k][j]);
else
m3->tab[i][j]=min(max(m1->tab[i][k],m2->tab[k][j]), m3->tab[i][j]);
}
return m3;
}
int est_idempotent(Matrice m){
Matrice tmp = multiplication_in_MnR(m, m);
int res = equal_matrice(m, tmp);
detruire_matrice(tmp);
return res;
}
//test si l'element de la matrice est stable(anchored);
int stable(int i,int j,Matrice m){
if(m->taille<=0){
printf("erreur\n");
return 0;
}
else{
int k;
for(k=0; k < m->taille; k++){
if(m->tab[k][k]==0 && m->tab[k][k] <= m->tab[i][j] && m->tab[i][j] == max(m->tab[i][k],m->tab[k][j]))
return TRUE;
}
return FALSE;
}
}
//test si la matrice est dans R;
int estMatriceR(Matrice m){
int i,j;
for(i=0;i<m->taille;i++)
for(j=0;j<m->taille;j++){
if(m->tab[i][j]>3)
return FALSE;
}
return TRUE;
}
// Calcule et renvoie la matrice # d'une matrice dans R
Matrice creer_matrice_diese(Matrice m){
if(!estMatriceR(m)){
printf("erreur");
return NULL;
}
Matrice ma=creer_matrice(m->taille);
ma->mot = tree_diese(m->mot);
int i,j;
for(i=0;i<m->taille;i++)
for(j=0;j<m->taille;j++){
if(m->tab[i][j]==1 && !stable(i,j,m))
ma->tab[i][j]=OMEGA;
else
ma->tab[i][j]=m->tab[i][j];
}
return ma;
}