Skip to content

Commit 7318afc

Browse files
committed
Minor clean up for cooley tukey in C++
- rename c64 to complex - make pi a constant instead of function - fix complilation warnings about comparing signed int with unsigned int
1 parent 012042c commit 7318afc

File tree

1 file changed

+14
-17
lines changed
  • chapters/algorithms/cooley_tukey/code/c++

1 file changed

+14
-17
lines changed

chapters/algorithms/cooley_tukey/code/c++/fft.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ using std::swap;
1717

1818
using std::size_t;
1919

20-
using c64 = std::complex<double>;
21-
template <typename T>
22-
constexpr T pi() {
23-
return 3.14159265358979323846264338327950288419716;
24-
}
20+
using complex = std::complex<double>;
21+
static const double pi = 3.14159265358979323846264338327950288419716;
2522

2623
// `cooley_tukey` does the cooley-tukey algorithm, recursively
2724
template <typename Iter>
@@ -30,12 +27,12 @@ void cooley_tukey(Iter first, Iter last) {
3027
if (size >= 2) {
3128
// split the range, with even indices going in the first half,
3229
// and odd indices going in the last half.
33-
auto temp = std::vector<c64>(size / 2);
34-
for (size_t i = 0; i < size / 2; ++i) {
30+
auto temp = std::vector<complex>(size / 2);
31+
for (int i = 0; i < size / 2; ++i) {
3532
temp[i] = first[i * 2 + 1];
3633
first[i] = first[i * 2];
3734
}
38-
for (size_t i = 0; i < size / 2; ++i) {
35+
for (int i = 0; i < size / 2; ++i) {
3936
first[i + size / 2] = temp[i];
4037
}
4138

@@ -45,8 +42,8 @@ void cooley_tukey(Iter first, Iter last) {
4542
cooley_tukey(split, last);
4643

4744
// now combine each of those halves with the butterflies
48-
for (size_t k = 0; k < size / 2; ++k) {
49-
auto w = std::exp(c64(0, -2.0 * pi<double>() * k / size));
45+
for (int k = 0; k < size / 2; ++k) {
46+
auto w = std::exp(complex(0, -2.0 * pi * k / size));
5047

5148
auto& bottom = first[k];
5249
auto& top = first[k + size / 2];
@@ -83,11 +80,11 @@ void iterative_cooley_tukey(Iter first, Iter last) {
8380

8481
// perform the butterfly on the range
8582
auto size = last - first;
86-
for (size_t stride = 2; stride <= size; stride *= 2) {
87-
auto w = exp(c64(0, -2.0 * pi<double>() / stride));
88-
for (size_t j = 0; j < size; j += stride) {
89-
auto v = c64(1.0);
90-
for (size_t k = 0; k < stride / 2; k++) {
83+
for (int stride = 2; stride <= size; stride *= 2) {
84+
auto w = exp(complex(0, -2.0 * pi / stride));
85+
for (int j = 0; j < size; j += stride) {
86+
auto v = complex(1.0);
87+
for (int k = 0; k < stride / 2; k++) {
9188
first[k + j + stride / 2] =
9289
first[k + j] - v * first[k + j + stride / 2];
9390
first[k + j] -= (first[k + j + stride / 2] - first[k + j]);
@@ -103,7 +100,7 @@ int main() {
103100
std::mt19937 rng(random_device());
104101
std::uniform_real_distribution<double> distribution(0.0, 1.0);
105102

106-
std::array<c64, 64> initial;
103+
std::array<complex, 64> initial;
107104
std::generate(
108105
begin(initial), end(initial), [&] { return distribution(rng); });
109106

@@ -117,7 +114,7 @@ int main() {
117114
// Check if the arrays are approximately equivalent
118115
std::cout << std::right << std::setw(16) << "idx" << std::setw(16) << "rec"
119116
<< std::setw(16) << "it" << std::setw(16) << "subtracted" << '\n';
120-
for (int i = 0; i < initial.size(); ++i) {
117+
for (size_t i = 0; i < initial.size(); ++i) {
121118
auto rec = recursive[i];
122119
auto it = iterative[i];
123120
std::cout << std::setw(16) << i << std::setw(16) << std::abs(rec)

0 commit comments

Comments
 (0)