-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms.c
271 lines (217 loc) · 5.57 KB
/
algorithms.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "algorithms.h"
complex
complex_mul(complex a, complex b)
{
complex ans;
ans.r = a.r * b.r - a.i * b.i;
ans.i = a.r * b.i + a.i * b.r;
return ans;
}
complex
complex_add(complex a, complex b)
{
complex ans;
ans.r = a.r + b.r;
ans.i = a.i + b.i;
return ans;
}
complex
complex_sub(complex a, complex b)
{
complex ans;
ans.r = a.r - b.r;
ans.i = a.i - b.i;
return ans;
}
void
fast_fourier_transform(complex *a, complex *y, int n, int inv)
{
complex w, wn, twiddle;
complex *a0;
complex *a1;
complex *y0;
complex *y1;
int i, k;
/* Base Case */
if (n == 1)
{
y[0] = a[0];
return;
}
/* Calculate principal nth root of unity (i.e. exp(2*PI*i/n)) */
if (inv)
{
wn.r = cos(-2*M_PI/(double)n);
wn.i = sin(-2*M_PI/(double)n);
}
else
{
wn.r = cos(2*M_PI/(double)n);
wn.i = sin(2*M_PI/(double)n);
}
w.r = 1.0;
w.i = 0.0;
/* allocate memory for even/odd coefficients and corresponding FFTs */
a0 = (complex *) malloc((n/2) * sizeof(complex));
a1 = (complex *) malloc((n/2) * sizeof(complex));
y0 = (complex *) malloc((n/2) * sizeof(complex));
y1 = (complex *) malloc((n/2) * sizeof(complex));
/* Extract even and odd coefficients */
for (i = 0; i < (n/2); i++)
{
a0[i] = a[2*i];
a1[i] = a[2*i+1];
}
/* Calculate 2 FFTs of size n/2 */
fast_fourier_transform(a0, y0, n/2, inv);
fast_fourier_transform(a1, y1, n/2, inv);
/* Combine results from half-size FFTs */
for (k = 0; k < (n/2); k++)
{
twiddle = complex_mul(w, y1[k]);
y[k] = complex_add(y0[k], twiddle);
y[k+n/2] = complex_sub(y0[k], twiddle);
w = complex_mul(w, wn);
}
free(a0);
free(a1);
free(y0);
free(y1);
return;
}
int
*multiply_trivial(int P[], int Q[], int n)
{
int result_size = 2*n + 1;
int *result = (int *) calloc(result_size, sizeof(int));
for (int i = 0; i < n + 1; i++)
for (int j = 0; j < n + 1; j++)
result[i+j] += P[i] * Q[j];
return result;
}
int
*multiply_divide_conquer(int P[], int Q[], int n)
{
int result_size = 2*n + 1;
int *result = (int *) calloc(result_size, sizeof(int));
// Base case
if (n == 1)
{
result[0] = P[0] * Q[0];
return result;
}
// Divide
int d = n / 2;
if (n % 2 == 1)
d++;
int *pHigh = (int *) calloc(d, sizeof(int));
int *qHigh = (int *) calloc(d, sizeof(int));
int *pLow = (int *) calloc(d-n%2, sizeof(int));
int *qLow = (int *) calloc(d-n%2, sizeof(int));
for (int i = 0; i < d; i++)
{
pHigh[i] = P[i+d];
qHigh[i] = Q[i+d];
pLow[i] = P[i];
qLow[i] = Q[i];
}
int *auxP = (int *) calloc(d, sizeof(int));
int *auxQ = (int *) calloc(d, sizeof(int));
for(int i = 0; i < d; i++)
{
auxP[i] = pLow[i]+pHigh[i];
auxQ[i] = qLow[i]+qHigh[i];
}
// Conquer
int *lowPQ = multiply_divide_conquer(pLow,qLow,d);
int *middle = multiply_divide_conquer(auxP, auxQ, d);
int *highPQ = multiply_divide_conquer(pHigh,qHigh,d);
// Combine
for (int i = 0; i < n; i++)
{
result[i] += lowPQ[i];
result[i+d] += middle[i] - lowPQ[i] - highPQ[i];
result[i+2*d] += highPQ[i];
}
free(pHigh); free(qHigh); free(pLow); free(qLow);
free(lowPQ); free(middle); free(highPQ);
return result;
}
int
*multiply_fft(int P[], int Q[], int n)
{
int i, j;
/* Allocate space for result */
int result_size = 2*n + 1;
int *result = (int *) calloc(result_size, sizeof(int));
/* Determine the next biggest power of two */
int next_power_of_2 = 1;
while (next_power_of_2 < n)
next_power_of_2 <<= 1;
/* Allocate space for aux polynomials */
complex *a = (complex *)malloc(2 * next_power_of_2 * sizeof(complex));
complex *b = (complex *)malloc(2 * next_power_of_2 * sizeof(complex));
/* Read coefficients from stdin */
for (i = 0; i < n; i++)
{
a[i].r = (double) P[i];
a[i].i = 0.0;
b[i].r = (double) Q[i];
b[i].i = 0.0;
}
/* Pad the rest with zeros */
for (i = n; i < (2 * next_power_of_2); i++)
{
a[i].r = 0.0; a[i].i = 0.0;
b[i].r = 0.0; b[i].i = 0.0;
}
n = next_power_of_2;
/* Multiply polynomials */
complex *ya;
complex *yb;
n = 2 * n;
/* Allocate storage for fft results */
ya = (complex *)malloc(n * sizeof(complex));
yb = (complex *)malloc(n * sizeof(complex));
/* DFT of A and B */
fast_fourier_transform(a, ya, n, 0);
fast_fourier_transform(b, yb, n, 0);
/* Pointwise Multiplication */
for (j = 0; j < n; j++)
ya[j] = complex_mul(ya[j], yb[j]);
/* Inverse DFT (swapped input and output arrays) */
fast_fourier_transform(ya, a, n, 1);
/* Divide real part by n */
for (j = 0; j < (n-1); j++)
{
a[j].r = a[j].r/n;
if (j < result_size)
result[j] = (int) round(a[j].r);
}
free(ya);
free(yb);
free(a);
free(b);
return result;
}
int
read_input(char *filename, int **p, int **q)
{
int n;
FILE *file = fopen(filename, "r");
if (file)
{
fscanf(file, "%d", &n);
*p = (int *) malloc((n + 1) * sizeof(int));
for (int i = 0; i < n + 1; i++)
fscanf(file, "%d", *p+i);
*q = (int *) malloc((n + 1) * sizeof(int));
for (int i = 0; i < n + 1; i++)
fscanf(file, "%d", *q+i);
fclose(file);
}
return n;
}