-
Notifications
You must be signed in to change notification settings - Fork 4
/
fft.cpp
63 lines (63 loc) · 1.6 KB
/
fft.cpp
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
/// Name: FFT
/// Description: Fast Fourier Transform
/// Detail:
/// Guarantee: struct FFT {
/// Dependencies:
/// Parent:
template <typename R> struct FFT {
struct C {
C(R a = 0, R b = 0):a(a),b(b){}
friend C operator+(const C& x, const C& y) { return C(x.a + y.a, x.b + y.b); }
friend C operator-(const C& x, const C& y) { return C(x.a - y.a, x.b - y.b); }
friend C operator*(const C& x, const C& y) { return C(x.a * y.a - x.b * y.b, x.a * y.b + x.b * y.a); }
friend C operator/(const C& x, const R& y) { return C(x.a / y, x.b / y); }
friend C& operator*=(C& x, const C& y) { return x = x * y; }
friend C& operator/=(C& x, const R& y) { return x = x / y; }
const R& real() const { return a; }
R a, b;
};
FFT(int n1, int n2):n(1){
while (n < n1+n2-1)
n *= 2;
}
template <bool invert> void dft(vector<C>& a) {
a.resize(n, 0);
for (auto i=1, j=0; i<n; ++i) {
auto bit = n >> 1;
for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;
if (i < j)
swap(a[i], a[j]);
}
auto pi = acos(R(-1));
for (auto l=2; l<=n; l<<=1) {
auto angle = (invert ? -1 : 1) * 2 * pi / l;
auto wl = C(cos(angle), sin(angle));
for (auto i=0; i<n; i+=l) {
auto w = C(1);
for (auto j=0; j<l/2; ++j) {
auto y0 = a[i+j];
auto y1 = a[i+j+l/2] * w;
a[i+j] = y0 + y1;
a[i+j+l/2] = y0 - y1;
w *= wl;
}
}
}
if (invert)
for (auto& ai : a)
ai /= n;
}
void fft(vector<C>& a, vector<C>& b) {
dft<false>(a);
dft<false>(b);
hadamard(a, b);
dft<true>(a);
}
void hadamard(vector<C>& a, const vector<C>& b) {
for (auto i=0; i<n; ++i)
a[i] *= b[i];
}
int n;
};