-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlagrange.c
More file actions
143 lines (96 loc) · 2.88 KB
/
lagrange.c
File metadata and controls
143 lines (96 loc) · 2.88 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
#include <stdio.h>
#include <stdlib.h>
#include "lagrange.h"
#include "utils.h"
int lagrange_pols() {
int i, j, k, nb, premier = 0, error_symbols;
printf("Combien de valeurs ? : ");
scanf("%d", &nb);
int* numbers = malloc(nb * sizeof(int));
for (i = 0; i < nb; i++) {
printf("number[%d] : ", i + 1);
scanf("%d", &numbers[i]);
//printf("\n");
};
while (!(premier > 0 && premier > nb)) {
printf("Modulo k : ");
scanf("%d", &premier);
//printf("\n");
};
printf("Combien d'erreur à corriger ? : ");
scanf("%d", &error_symbols);
printf("\n");
printf("--------------------------------------\n\n");
error_symbols *= 2;
int** LagPols = malloc(nb * nb * sizeof(int));
int* up = calloc(nb, sizeof(int));
int* interPol = calloc(nb, sizeof(int));
// Iterate throug all the lagrange pols
for (i = 0; i < nb; i++) {
LagPols[i] = (int*)calloc(nb, sizeof(int));
//LagPols[i][nb - 1] = 1;
//LagPols[i][nb - 2] = 1;
int sum = 1;
// Calculate the value necessary for the inverse of the matrix
for (j = 0; j < nb; j++) {
if (i != j) {
sum = mod((i - j) * sum, premier);
}
}
// This value
//printf("Somme : %d", sum);
int value = mod(invMod(sum, premier) * numbers[i], premier);
//printf("A : %d\n", value);
for (j = 1; j < nb +1; j++) {
if (i + 1 != j) {
// Creating an upper matrix with everything being augmented of one
for (k = 0; k < nb - 1; k++) {
up[k] = LagPols[i][k + 1];
}
up[nb - 1] = 0;
//printArray(up, nb);
// Computing the calculations
// For the first one, it must be non 0
// There may be a faster way to go
if (vide(LagPols[i], nb)) {
LagPols[i][nb - 1] = -j;
LagPols[i][nb - 2] = 1;
} else for (k = 0; k < nb; k++) {
// Making the multiplication of two polynomials, one being a linear of the form x - a
LagPols[i][k] = mod((up[k] - j * LagPols[i][k]), premier);
};
//printArray(LagPols[i], nb);
}
}
// Multipling all with our inv found to get the value at the special index i + 1
for (j = 0; j < nb; j++) {
LagPols[i][j] = mod(LagPols[i][j] * value, premier);
};
};
//printMat(LagPols, nb, nb);
for (i = 0; i < nb; i++) {
for (j = 0; j < nb; j++) {
interPol[i] = interPol[i] + LagPols[j][i];
};
// This can have before the modulo, a maximum size of (premier - 1) * nb, which could be huge
interPol[i] = mod(interPol[i], premier);
}
printResult(interPol, nb);
printf("\nValeurs a renvoyer : ");
for (i = 0; i < nb; i++) {
printf("%d ", numbers[i]);;
};
for (i = 1; i < error_symbols + 1; i++) {
printf("%d ", applicationPol(interPol, nb, nb + i, premier));
};
/*printf("\n");
for (i = 1; i < nb + error_symbols + 1; i++) {
printf("%d ", applicationPol(interPol, nb, i, premier));
}*/
for (i = 0; i < nb; i++) free(LagPols[i]);
free(up);
free(numbers);
free(LagPols);
free(interPol);
return 0;
}