-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDiscreteFourierTransform.cpp
More file actions
142 lines (142 loc) · 2.59 KB
/
Copy pathDiscreteFourierTransform.cpp
File metadata and controls
142 lines (142 loc) · 2.59 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
#define ve vector
#define complex num
#define float2 long double
#define pb push_back
template <typename y>
ve<y> operator*(const ve<y> &A, const ve<y> &B)
{
ve<y> R(A);
for (int i = 0; i < B.size(); i++)
R[i] = A[i] * B[i];
return R;
}
struct complex
{
float2 re, im;
public:
complex() :re(0), im(0) {}
complex(float2 R, float2 I) :re(R), im(I) {}
complex operator* (const complex &nu) const
{
return complex(re*nu.re - im*nu.im, im*nu.re + re*nu.im);
}
complex const operator+(const complex &nu) const
{
return complex(re + nu.re, im + nu.im);
}
complex operator-(const complex &nu) const
{
return complex(re - nu.re, im - nu.im);
}
complex(float2 angle) :re(cos(angle)), im(sin(angle)) {}
complex& operator*=(const complex &n)
{
float2 re_cpy = (re*n.re - im*n.im), im_cpy = (im*n.re + re*n.im);
re = re_cpy;
im = im_cpy;
return *this;
}
complex& operator-=(const complex &n)
{
re -= n.re;
im -= n.im;
return *this;
}
complex& operator+=(const complex &n)
{
re += n.re;
im += n.im;
return *this;
}
};
template<typename my_type>
class DiscreteFourierTransform
{
int N;
int revised()
{
int temp = N - 1;
int ans = 1;
while (temp)
{
ans *= 2;
temp >>= 1;
}
return ans;
}
public:
ve<num> DFT(const ve<my_type> &v)
{
int R = revised();
ve<num> a(R);
for (int i = 0; i<N && i<v.size(); i++)
{
a[i].re = v[i];
}
return DFT(a);
}
ve<num> DFT(const ve<num> &a)
{
int n = a.size();
if (n == 1)
return a;
num wn(2 * pi / n);
num w(1, 0);
ve<num> a0, a1;
for (int i = 0; i<n;)
{
a0.pb(a[i++]);
a1.pb(a[i++]);
}
ve<num> y0 = DFT(a0);
ve<num> y1 = DFT(a1);
ve<num> y(n);
int n_2 = n / 2;
for (int k = 0; k<n_2; k++)
{
y[k] = y0[k] + w*y1[k];
y[k + n_2] = y0[k] - w*y1[k];
w = w*wn;
}
return y;
}
ve<num> inv_DFT(const ve<num> &y)
{
int n = y.size();
if (n == 1)
return y;
num wn(2 * pi / n);
wn.im = -wn.im;
num w(1, 0);
ve<num> y0, y1;
for (int i = 0; i<n;)
{
y0.pb(y[i++]);
y1.pb(y[i++]);
}
ve<num> a0 = inv_DFT(y0);
ve<num> a1 = inv_DFT(y1);
ve<num> a(n);
int n_2 = n / 2;
for (int k = 0; k<n_2; k++)
{
a[k] = (a0[k] + w*a1[k]);
a[k + n_2] = (a0[k] - w*a1[k]);
w = w*wn;
}
return a;
}
ve<my_type> poly_mult(const ve<my_type> &a, const ve<my_type> &b)
{
N = a.size() + b.size();
int val = revised();
ve<num> R(inv_DFT(DFT(a)*DFT(b)));
for (auto &x : R)
x.re /= val, x.im /= val;
ve<long long> Res;
for (int i = 0; i < N - 1; i++)
Res.pb(static_cast<long long>(R[i].re + 0.5));
return Res;
}
};
DiscreteFourierTransform<int> D;